views:

608

answers:

3

I am currently trying to wrap my mind around Java EE 5. What I'd like to do is create a sample application that

  • offers a simple stateless EJB (e. g. a simple calulator with an add() method)
  • expose this add method as a webservice
  • consume this webservice from another EJB

The first two steps are easy and I can deploy and test this bean to Glassfish v2.1 already and test it with a standalone client:

@WebService
@Stateless
public class CalculatorWS {

    @WebMethod
    public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
        int k = i + j;
        return k;
    }
}

What I do not get however, is how to consume a webservice like this from a second EJB. While not strictly useful in this example, I will have to write some EJBs soon that will wrap external webservices as to keep my internal clients from having to deal with those external resources.

From what I understand, I should be able to have the container inject me a webservice into a field of my EJB? I did not find an example for this, however. I'd welcome any hints to tutorials covering this - or even better an example right here :-)

For what it's worth, I am using Eclipse 3.5.

A: 

The Web Service client isn't really EJB specific. So I think you'd use JAX-WS client techniques, your EJB environment being a managed envirnment (JNDI etc all nicely available).

I know you're not using WebSphere but I hope the techniques explained here are generally applicable.

djna
+2  A: 

From the official Java EE tutorial

Consuming a Web service

kazanaki
A: 

I would just inject this Calculator Bean to another stateless Bean with @EJB. Just make sure your bean implements some interface to be able to inject.

Mykola Golubyev