1. project structure:
mkdir -p ejblite/src/test
mkdir -p ejblite/testApp
2. go to ejblite/src/test, and edit 2 java files:
-------------3. compile and run from src/test directory, using GlassFish v3:
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();
}
}
}
}
javac -d ../../testApp/ -cp $GLASSFISH_HOME/lib/embedded/glassfish-embedded-static-shell.jar:../../classes Client.java TestBean.javaYou will see the following output, when running with GlassFish v3:
java -cp $GLASSFISH_HOME/lib/embedded/glassfish-embedded-static-shell.jar:../../testApp test.Client
Dec 8, 2009 3:47:45 PM com.sun.enterprise.v3.server.AppServerStartup runIt'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
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