Tuesday, September 22, 2009

EJB 3.0 and JAX-WS Web Services

I was in a discussion with a number of my colleagues yesterday and the topic came up of using EJB 3.0 with JAX-WS web services. Essentially, they weren't sure if the @EJB annotation (which injected a link to an EJB 3.0 bean) could be used with JAX-WS endpoints. The question comes up since they only though injection worked on servlets (but this wasn't true). This is something that we think will be a typical usage scenario in that users can invoke existing services downstream on an EJB 3.0 bean. These scenarios may not necessarily be exposing EJB 3.0 beans as web services but merely using an EJB as part of your business logic to obtain/determine information needing to use or share back to a customer.


The general scenario looks a little something like the following image below:

The code to develop it is very straight forward. It's basically starting out with an EJB 3.0 bean like the code below:

import javax.ejb.Stateless;

@Stateless
public class SampleBean implements SampleBeanLocal {
public SampleBean() {}

public String echo(String s) {
return s;
}
}


with a simple local interface...

import javax.ejb.Local;

@Local
public interface SampleBeanLocal {
public String echo (String s);
}


it becomes easy to then inject a reference into a JAX-WS bean in order to get this to work... The code is also pretty trivial...

import javax.ejb.EJB;
import sample.ejb.SampleBeanLocal;

@javax.jws.WebService
public class SampleService{
@EJB(mappedName = "sample.ejb.SampleBeanLocal")
private SampleBeanLocal sample;

public String echo(String arg0) {
return sample.echo(arg0);
}
}


and that's about it. This is supported in IBM WebSphere Application Server v7.0. Let us know if you have any questions.

4 comments:

  1. We have tried this out in our company, and it works really good. Together with a Top-Down approach for the WS and only use the WS bean to map to our business logic we have gotten a really nice clean and simple design for our WS.

    But, is it really possible (or if possible, wise) to use/inject a Statful bean into an WebService? Since the WS bean by nature is Stateless, must not the injected beans also be stateless?

    ReplyDelete
  2. Daniel,
    While one can employ the use of stateful session EJBs as part of a web services implementation, and WebSphere Application Server even has mechanisms to maintain request affinity for these web services, use of stateless session EJBs is always a better architecture. It's more scaleable going forward. You are better off keeping your state in a separate store. If you have specific reasons to go down this path, I would suggest removing the usage of a local interface, and instead use a remote interface, and locate your stateful bean in another location (where you can then scale your stateless facade services independently).
    Good luck...

    ReplyDelete
  3. Hi again...

    The reason I asked was because in the sample above you are using a Stateful bean in your WS :)

    ReplyDelete
  4. Thanks Daniel for pointing that out... I didn't catch that. I've since updated my example to reference a proper way of doing this...

    ReplyDelete