Showing posts with label injection. Show all posts
Showing posts with label injection. Show all posts

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.