call ejb remote

2019-07-07 13:12发布

Is it possible to access an EJB remote interface method from a GWT client module write with RPC? The gwt application is on an server with Tomcat and the EJB is deployed in a Jboss server. If is it possibile, where i can find example code?

1条回答
\"骚年 ilove
2楼-- · 2019-07-07 13:26

The tutorial you've provided looks fine, and though it's for a command line app, the same concept should work for an application deployed on Tomcat. What problems have you found with it?

Here you've a simpler example: let's suppose you've an EJB with this simple interface deployed on JBoss:

package ejb.example;
import javax.ejb.Remote;
@Remote
public interface Example {
    public String hello (String nom);
}

The code to access to the EJB remotely should be something similar to:

// Simple EJB Client example
package ejbclient.example
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ejb.example.Example;   // Need to import the remote interface of the bean
public class ClientEJB {
   public static void main(String[] args) {
      try {
         // Set the properties to JBoss access
         Properties environment = new Properties();
         environment.put(Context.INITIAL_CONTEXT_FACTORY,      
                              "org.jnp.interfaces.NamingContextFactory");
         environment.put(Context.PROVIDER_URL,"yourjboserver.com:1099" );
         InitialContext context = new InitialContext(environment);

         // Once the proper context is set, we can obtain the dynamic proxy 
         Example accessEJB = (Example)
                                     context.lookup("ExampleBean/remote");
         // And finally we're done! We can access the EJB as if it was a regular object
         String result = accessEJB.hello("Kate"));
      } catch (NamingException e) {
         e.printStackTrace();
      }
  }
}

Things to take in mind:

A. As it's said in the tutorial, instead of hardcode the the context properties in the source code, you could define them in a jndi.properties file, something like this:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=yourJBossServer.com:JBossJNPPort

this file should be placed in the classpath, and so, in the code you'd only have to call:

InitialContext context = new InitialContext();

this solution is prefered and more elegant (it allows you to change the values without recompiling the client)

B. Pay attention to the context.lookup("ExampleBean/remote") statement: by default JBoss assigns the JNDI of the interfaces as the name of the class Bean (the implementation) with the sufix "/remote" or "/local" depending on the kind of the interface. That's for EJB deployed directly in a jar file, in case the EJB it's placed inside an EAR it adds the name of the ear file as a prefix (for example you've your EJB-jar inside an ear called myapp.ear the name you should lookup for would be: "myapp/ExampleBean/remote"). And of course, you may have changed the JNDI name in the EJB (with anotations or using its deployment descriptors), in that case, you'll have to use these names.

C. On the other hand, you'll also need to have the JBoss client libraries, which are also listed in the tutorial, in the classpath (you could place them in the wEB-INF/lib folder of your war).

D. And finally, you'll also need the remote interface in your classpath.

I hope it helps!

查看更多
登录 后发表回答