6/26/2007

A simple EJB 3 application, hand-made

This is a simple EJB 3 app that is written, deployed and run from command line and vim, with no IDE, no ant, no maven, no admin console. The target appserver is glassfish.

1. project directory structure:

C:\simple-ejb3> tree /A /F
+---classes
| \---foo
\---src
\---foo
Client.java
FooBean.java
FooRemote.java
2. create java src files under src\foo:
package foo;
import javax.ejb.*;

@Remote
public interface FooRemote {
public String echo(String s);
}

package foo;
import javax.ejb.*;

@Stateless
public class FooBean implements FooRemote {
public String echo(String s) {
return s;
}
}

package foo;
import javax.ejb.*;
import javax.naming.*;

public class Client {
public static void main(String[] args) throws Exception {
Context ic = new InitialContext();
Object obj = ic.lookup(FooRemote.class.getName());
System.out.println("lookup returned " + obj);

FooRemote foo = (FooRemote) obj;
String input = (args.length > 0) ? args[0] :
"No application arg specified.";
String s = foo.echo(input);
System.out.println("foo.echo returned " + s);
}
}
3. Compile java src. An environment variable JAVAEE_HOME is set for convenience but it's not required.
C:\simple-ejb3\classes> set JAVAEE_HOME=C:\glassfish
C:\simple-ejb3\classes> javac -d . -classpath %JAVAEE_HOME%\lib\javaee.jar;. ..\src\foo\*.java
4. start the appserver:
C:\simple-ejb3\classes> %JAVAEE_HOME%\bin\asadmin.bat start-domain
5. package and autodeploy ejb-jar. We can combine the 2 steps in 1 by using %JAVAEE_HOME%\domains\domain1\autodeploy as the destdir:
C:\simple-ejb3\classes> jar cvf %JAVAEE_HOME%\domains\domain1\autodeploy\foo-ejb.jar foo\FooBean.class foo\FooRemote.class
6. run the standalone java client:
C:\simple-ejb3\classes> java -cp %JAVAEE_HOME%\lib\javaee.jar;%JAVAEE_HOME%\lib\appserv-rt.jar;. foo.Client

lookup returned foo._FooRemote_Wrapper@e9738564
foo.echo returned No application arg specified.
7. undeploy the ejb module:
del %JAVAEE_HOME%\domains\domain1\autodeploy\*.jar
It's pretty easy, isn't it? No deployment descriptors, no client stubs, no jndi.properties file.

11 comments:

Awais said...

I am running an ejb3 applicatino using javaws from the command line. Applicatin is deployed on jboss4.2.1. However I recieve this exception:

class javax.naming.Reference
java.lang.ClassCastException: javax.naming.Reference
at com.nwi.asa.common.communication.CommDelegate.createSession(CommDelegate.java:54)
at com.nwi.asa.common.communication.CommDelegate.init(CommDelegate.java:42)
at com.nwi.asa.common.login.LoginManager.login(LoginManager.java:163)
at com.nwi.asa.common.login.LoginManagerActionListener$1.actionPerformed(LoginManagerActionListener.java:40)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

This exception is thrown when a session bean is looked up and casted into corresponding session bean class.
However when i run the application directly from command line using java(as i have code on my machine), the application runs perfectly fine. Can you tell me how to resolve this issue. Is it because of class loader or some policy? If so then please tell me the way out

Uday Kumar said...

same problem here too. when i try to cast the intial Context object to session bean interface, i'm getting the class cast exception.

help me in this.

javahowto said...

Do not case InitialContext to the bean's business interface. What you want to do is cast the lookup result into bean's business interface:

try {
Foo foo = (Foo) initialContext.lookup("ejb/foo");
} catch (NamingException e) {
...
}

animeworld said...

It's easy and straightforward!! Thank you friend!

Do you know why @EJB
private static FooRemote foo; doesn't work in this case?

javahowto said...

It's a Java SE client so you cannot use @EJB. @EJB can only be used in Java EE component classes.

Anonymous said...

hen I run the client, I am getting the following error. Any help?

Dec 30, 2011 7:35:01 PM com.sun.enterprise.v3.server.CommonClassLoaderServiceImp
l findDerbyClient
INFO: Cannot find javadb client jar file, derby jdbc driver will not be availabl
e by default.

javahowto said...

This is just an INFO log, and shouldn't affect your application running.

Anonymous said...

I tried this example with glassfish 3.1.1 and it works !
Thanks

Anonymous said...

"I tried this example with glassfish 3.1.1 and it works !
Thanks"

How did you make it work with glassfish?

for me it wont deploy as i'm not adding any "WEB-INF" folder nor a deploy descriptor to the jar file as it's not mentioned to be needed for deployment.

After compiling the source successfully then jar the .class files with the command line, i copy/paste foo-ejb.jar to autodeploy folder but the deployment fails. anyone got an idea about this?

Steve Smith said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Course in Chennai

Java Online Training India

LAZYONE said...

One of the best and efficient tutorial I have encountered on the web. Thanks.