Skip to main content

How to call EJB 3.0 from Java class?

1 reply [Last post]
mtbittle
Offline
Joined: 2008-03-18

I am wondering if it's possible to call a local or remote method of a stateless session bean from a regular java class. I created an example in NetBeans to try it, but can't get it working. I have an EJB named HelloBean, with local and remote interfaces (HelloLocal, HelloRemote). The Session bean has two methods (one from each interface), hello_local() and hello_remote(). In the same package as the stateless session bean, I created a java class called HelloHelper. I call the init() method from a servlet (which works fine) to start the process.

(Disregard any spelling errors in the code...I'm hand-typing)

HelloHelper.java code:

package test;

import javax.naming.*;

public class HelloHelper {

public HelloHelper() { } //empty constructor

public void init() {
callHelloBean();
}

private static void callHelloBean() {
HelloRemote hi = lookupHelloBeanRemote();
hi.hello_remote();
}

//method from NetBeans ->Right click->Enterprise Resources
//->Call Enterprise Bean
private static HelloRemote lookupHelloBeanRemote () {
try {
Context c = new InitialContext();
return (HelloRemote) c.lookup("java:comp/env/HelloBean");
} catch (NamingException ne) {
throw new RuntimeException(ne);
}

}

HelloBean code:

package test;

import javax.ejb.*;

@Stateless
public class HelloBean implements HelloRemote, HelloLocal {

public void hello_local () {
System.out.println("Hello from LOCAL!");
}

public void hello_remote() {
System.out.println("Hello from REMOTE!");
}

}

During the lookup method, I receive the NamingException 'NameNotFoundException'. I have everything inside a single Enterprise Application (EJB and WAR). The java class and the session bean are in the same package (test). I tried adding an ejb-jar.xml file and then tried adding the EJB reference in the web.xml file. I could have possibly done something wrong with the xml files, but nothing works. I even added annotation to the session bean (@EJB(name="...", mappedName="...").

Final question: Can you call a local or remote method from a pure java class? If so, what am I missing? Thanks in advance for any help. I appreciate the time!

-MTB

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mtbittle
Offline
Joined: 2008-03-18

Well, I have finally found the answer. Hopefully this will help others who may be in the same boat.

You can call a method exposed in an stateless session bean's REMOTE interface from a Java class. Here's what I've done:

Within the java class, you look up the REMOTE interface of the EJB:
[code]
//Create a new context
Context c = new InitialContext();

//use the context to lookup the remote interface.
//Inside the lookup( ) method is the name of the remote interface you want.
HelloRemote helloRemote = (HelloRemote) c.lookup("HelloRemote");

//now you can call any method exposed in the remote interface.
helloRemote.hello_remote();
[/code]

I've just tested this and it works for a [b]REMOTE[/b] interface only. I think I read somewhere that the local interface can only be accessed by other EJBs or Web components because it uses local reference. The Java class (HelloHelper.java) is inside the same EJB container as the EJB I'm calling.

The whole time I've been struggling with this, the only change I had to make was putting the class name of the remote interface inside the c.lookup() method. Geesh.