The general scenario looks a little something like the following image 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.
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.
ReplyDeleteBut, 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?
Daniel,
ReplyDeleteWhile 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...
Hi again...
ReplyDeleteThe reason I asked was because in the sample above you are using a Stateful bean in your WS :)
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