How to make EJB3 remote interface available to cli

2019-09-08 15:13发布

问题:

I am trying to access a remote EJB3 bean from a client which runs on a separate JVM using JNDI lookup. Following are my bean classes:

@Path("/payment/")
@Consumes({ "application/xml", "application/json" })
@Produces({ "application/xml", "application/json" })
@Local({ PaymentWebServiceLocal.class })
@Remote({ PaymentWebServiceRemote.class })
@Stateless(mappedName = "ejb/PaymentWebService")
public class PaymentWebServiceImpl implements PaymentWebServiceLocal,PaymentWebServiceRemote {

private static final Logger logger = Logger.getLogger(PaymentWebServiceImpl.class);


@POST
@Path("/processpayment")
public PaymentResponse processPayment(PaymentRequest request) {
   //do something
}

The remote interface of the Bean is as follows

@Remote
public interface PaymentWebServiceRemote {
    public PaymentResponse processPayment(PaymentRequest request);
}

Now, I lookup the EJB using JNDI to invoke the processPayment() method from the remote client as follows

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    env.put(Context.PROVIDER_URL, t3:\\);
    Context ctx = new InitialContext(env);
    return ctx.lookup("ejb.PaymentWebService#test.webservice.payment.service.PaymentWebServiceRemote");

What I don't understand is that the client do not know about the remote PaymentWebServiceRemote interface. It is in another server with the EJB Bean code. Do I have to take a copy of this interface to client code as well? Or is there some way of creating a client EJB jar with the remote interface for client to use in EJB3 (like we did in EJB2)?

Any help is appreciated.

回答1:

You have to create a client jar which contains all necessary interfaces and also the parameter classes which might be passed to the methods of the interface.

In your example you need to put

  • VoidRemittanceService
  • VoidRemittanceServiceRemote
  • PaymentResponse

into the client jar.

The implementing classes of the remote interfaces are not necessary within the client jar.

Of course you can also make a copy and put them into the client project. But this is not good practice since you have to make changes of the interface always twice. Additionally you must be sure that the client interfaces are compiled with the same JRE Version as the server jar. Else there could be problems.

Good practice is to create an additional project which contains shared code means which will be used by server and client. This project can be referenced by the server project and the client project. In this way changes of the shared code influence directly the server and the client side.