Can an EJB be called from a desktop application?

戏子无情 提交于 2020-01-15 05:00:27

问题


I am new in Java EJB 3.0. It is possible to call a (session) bean—deployed on JBoss—from a desktop application client?

Thanks in advance.


回答1:


Yes you can. Some specifics are here (references EJB2 but it the same for EJB3 when it comes to remote clients): http://www.theserverside.com/discussions/thread.tss?thread_id=9197

Paraphrased:

Hashtable env = new Hashtable();
env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.provider.url", "jnp://localhost:1099");
env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
Context ctx = new InitialContext(env); 
// name is whatever JNDI name you gave it 
Object o = ctx.lookup("home name"); 
EJBHome ejbHome = (EJBHome) PortableRemoteObject.narrow(o,EJBHome.class); 
// This is userID should be the one passed. 
EJB ejb = ejbHome.create(..); 



回答2:


Yes.

public static void main(String args[]) throws Exception {

   InitialContext ctx = new InitialContext();
   YourService yourService = (YourService) ctx.lookup("com.example.session.YourService");
   String time = yourService.getTime();
   System.out.println("Time is: " + time);
}

For client configuration you must provide jndi.properties file with contents

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost

If you are looking for working examples on JBoss try download source code of Enterprise JavaBeans 3.0, Fifth Edition




回答3:


Let's assume you have the following remote interface:

@Remote
public interface HelloBeanRemote {
    public String sayHello();
}

And a session bean implementing it:

@Stateless
public class HelloBean implements HelloBeanRemote {
    ...
}

And that this EJB is correctly packaged and deployed on JBoss.

On the client side, create a jndi.properties with the following content and put it on the classpath:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

Then use the following code to call your EJB:

Context context;
try {
    context = new InitialContext();
    HelloBeanRemote beanRemote = (HelloBeanRemote)context.lookup("HelloBean/remote"); 
    beanRemote.test(); 
} catch (NamingException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
}

Alternatively, if you don't want to provide a jndi.properties file, you can explicitly setup the JNDI environment in the code and create the context like this:

Properties properties = new Properties();
properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
properties.put("java.naming.provider.url","localhost:1099");
Context context = new InitialContext(properties);

But I'd recommend using the jndi.properties for the sake of portability.




回答4:


You can also expose the bean as a web service. I believe this is available as of EJB 3. It is quite nice considering you can do it with annotations. You may wish to consider using this option to decrease coupling. Here is a link to a tutorial.



来源:https://stackoverflow.com/questions/1862445/can-an-ejb-be-called-from-a-desktop-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!