Reputation: 712
What is the correct way to implement a stateless EJB 3.1 for invoking a web service. My client works as a Servlet, but I want to move the invocation into a EEJ bean. I have to add username and password in the SOAP header envelop to access the WS, which is working fine.
The service the the servlet is using looks like this;
@WebServiceClient(name = "MessageService", targetNamespace = "http://...", wsdlLocation = "...wsdl")
public class MessageService
extends Service
Can I wrap MessageService
in a Stateless EJB or should the bean itself use @WebServiceRef
(as in the tutorial) without wrapping the MessageService ?
Upvotes: 0
Views: 2333
Reputation: 42010
If the client and the provider lives in same EAR or WAR on the application server, can be invoked like a ordinal EJB. e.g.
@WebService
@Stateless
public class CalculatorBean implements Calculator {
public int add(int a, int b) {
return a + b;
}
}
The CalculatorBean
is threadsafe. All business logic that occurs within the add
method
is part of a container-managed transaction and not participate in any global transaction.
Alternatively, the client code can look up in the JNDI namespace.
The runtime can inject a service object or a port object into a member variable annotated with javax.xml.ws.WebServiceRef
.
@WebServiceRef(CalculatorService.class)
private Calculator port;
The CalculatorService
class is annotated with the javax.xml.ws.WebServiceClient
annotation (the client of the service), which has a wsdlLocation
attribute.
If you want to wrap the WebService into the EJB, see this answer. For read a discussion about this, see EJB and Web Services: getting the best of both worlds.
Upvotes: 4