12/08/2009

A sample EJB 3.1 Lite client

EJB Lite is a lightweight subset of EJB 3.1, including the support for running EJB container embedded in the current java application. So one can run EJB without appserver. The following sample contains a no-interface stateless EJB, and a java application that looks up and invokes the EJB.

1. project structure:

mkdir -p ejblite/src/test
mkdir -p ejblite/testApp

2. go to ejblite/src/test, and edit 2 java files:
-------------
TestBean.java
-------------

package test;
import javax.ejb.Stateless;

@Stateless
public class TestBean {
public String hello(String name) {
return "Hello, " + name;
}
}

-----------
Client.java
-----------

package test;

import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;
import javax.naming.NamingException;

public class Client {
public static void main(String args[]) throws NamingException {
EJBContainer container = null;
try {
container = EJBContainer.createEJBContainer();
Context namingContext = container.getContext();
TestBean testBean = (TestBean) namingContext.lookup(
"java:global/testApp/TestBean");
String hi = testBean.hello("client");
System.out.println("testBean.hello method returned: " + hi);
} finally {
if(container != null) {
container.close();
}
}
}
}
3. compile and run from src/test directory, using GlassFish v3:
javac -d ../../testApp/ -cp $GLASSFISH_HOME/lib/embedded/glassfish-embedded-static-shell.jar:../../classes Client.java TestBean.java

java -cp $GLASSFISH_HOME/lib/embedded/glassfish-embedded-static-shell.jar:../../testApp test.Client
You will see the following output, when running with GlassFish v3:
Dec 8, 2009 3:47:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: GlassFish v3 (74.2) startup time : Embedded(1246ms) startup services(511ms) total(1757ms)
Dec 8, 2009 3:47:45 PM org.glassfish.admin.mbeanserver.JMXStartupService$JMXConnectorsStarterThread run
INFO: JMXStartupService: JMXConnector system is disabled, skipping.
Dec 8, 2009 3:47:46 PM com.sun.enterprise.transaction.JavaEETransactionManagerSimplified initDelegates
INFO: Using com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate as the delegate
Dec 8, 2009 3:47:46 PM AppServerStartup run
INFO: [Thread[GlassFish Kernel Main Thread,5,main]] started
Dec 8, 2009 3:47:47 PM com.sun.enterprise.security.SecurityLifecycle <init>
INFO: security.secmgroff
Dec 8, 2009 3:47:47 PM com.sun.enterprise.security.SecurityLifecycle onInitialization
INFO: Security startup service called
Dec 8, 2009 3:47:47 PM com.sun.enterprise.security.PolicyLoader loadPolicy
INFO: policy.loading
Dec 8, 2009 3:47:47 PM com.sun.enterprise.security.auth.realm.Realm doInstantiate
INFO: Realm admin-realm of classtype com.sun.enterprise.security.auth.realm.file.FileRealm successfully created.
Dec 8, 2009 3:47:47 PM com.sun.enterprise.security.auth.realm.Realm doInstantiate
INFO: Realm file of classtype com.sun.enterprise.security.auth.realm.file.FileRealm successfully created.
Dec 8, 2009 3:47:47 PM com.sun.enterprise.security.auth.realm.Realm doInstantiate
INFO: Realm certificate of classtype com.sun.enterprise.security.auth.realm.certificate.CertificateRealm successfully created.
Dec 8, 2009 3:47:47 PM com.sun.enterprise.security.SecurityLifecycle onInitialization
INFO: Security service(s) started successfully....
Dec 8, 2009 3:47:48 PM com.sun.ejb.containers.BaseContainer initializeHome
INFO: Portable JNDI names for EJB TestBean : [java:global/testApp/TestBean!test.TestBean, java:global/testApp/TestBean]
testBean.hello method returned: Hello, client
Dec 8, 2009 3:47:48 PM org.glassfish.admin.mbeanserver.JMXStartupService shutdown
INFO: JMXStartupService and JMXConnectors have been shut down.
Dec 8, 2009 3:47:48 PM com.sun.enterprise.v3.server.AppServerStartup stop
INFO: Shutdown procedure finished
Dec 8, 2009 3:47:48 PM AppServerStartup run
INFO: [Thread[GlassFish Kernel Main Thread,5,main]] exiting
It's a good practice to close the EJB container before program exists, to avoid any resource leaking. It's more easily handled in JUnit tearDown method. See my follow-up post EJB Lite testing with JUnit and embeddable container

2 comments:

vitamine a said...

Writing enterprise applications using EJB 2.1, EJB 3.0 or Spring always promoted the idea of writing component interfaces. In many cases, you could do with just a POJO without an interface. This new version of EJB allows you to write Session beans with no interfaces; just like you write your JPA entities and Message Driven beans.

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