This blog has been in my Drafts folder for quite some time now, needed some final tweaks, and finally pushing it out.
Chapter 22 of Enterprise JavaBeans 3.1 specification (released as part of Java EE 6) describes "Embeddable Usage" as:
Unlike traditional Java EE server-based execution, embeddable usage allows client code and its corresponding enterprise beans to run within the same JVM and class loader. This provides better support for testing, offline processing (e.g. batch), and the use of the EJB programming model in desktop applications.
Earlier blogs already described the steps in detail but I had to try this stuff myself
And moreover this Tip Of The Day (TOTD) shows, as always, complete detailed steps to get you going from scratch.
Lets see how such an embeddable EJB application can be easily created.
- Create a Maven project as:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.glassfish.embedded.samples -DartifactId=ejb31 - Add the following fragments to "pom.xml" to ensure right set of JARs are pulled in:
<repositories> <repository> <id>download.java.net</id> <name>Java.net Maven Repository</name> <url>http://download.java.net/maven/2</url> </repository> </repositories> . . . <dependency> <groupId>org.glassfish.extras</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>3.0</version> <scope>compile</scope> </dependency> . . . <build> <defaultGoal>install</defaultGoal> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> - Change the generated "src/main/java/org/glassfish/embedded/samples/App.java" to:
package org.glassfish.embedded.samples; import javax.ejb.Stateless; /** * Hello world! */ @Stateless public class App { public static String sayHello(String name) { return "Hello " + name; } }This creates our trivial Enterprise JavaBean.
- Change the generated "src/test/java/org/glassfish/embedded/samples/AppTest.java" to (taking the code snippet from Adam’s blog and slightly altering it):
public void testEJB() throws NamingException { EJBContainer ejbC = EJBContainer.createEJBContainer(); Context ctx = ejbC.getContext(); App app = (App) ctx.lookup("java:global/classes/App"); assertNotNull(app); String NAME = "Duke"; String greeting = app.sayHello(NAME); assertNotNull(greeting); assertTrue(greeting.equals("Hello " + NAME)); ejbC.close(); }This is a simple test that looks up the bean using portable JNDI name, more explanation on JNDI name below.
- Run the tests by giving the following standard command in the Maven project:
~/samples/v3/embedded/ejb31 >mvn clean test [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building ejb31 [INFO] task-segment: [clean, test] [INFO] ------------------------------------------------------------------------ [INFO] [clean:clean {execution: default-clean}] [INFO] Deleting directory /Users/arungupta/samples/v3/embedded/ejb31/target . . . ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.glassfish.embedded.samples.AppTest Apr 9, 2010 3:48:16 PM org.glassfish.ejb.embedded.EJBContainerProviderImpl getValidFile SEVERE: ejb.embedded.location_not_exists Apr 9, 2010 3:48:19 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: GlassFish v3 (74.2) startup time : Embedded(1180ms) startup services(1523ms) total(2703ms) Apr 9, 2010 3:48:20 PM com.sun.enterprise.transaction.JavaEETransactionManagerSimplified initDelegates INFO: Using com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate as the delegate Apr 9, 2010 3:48:21 PM org.glassfish.admin.mbeanserver.JMXStartupService$JMXConnectorsStarterThread run INFO: JMXStartupService: JMXConnector system is disabled, skipping. Apr 9, 2010 3:48:21 PM AppServerStartup run INFO: [Thread[GlassFish Kernel Main Thread,5,main]] started Apr 9, 2010 3:48:30 PM com.sun.enterprise.security.SecurityLifecycle INFO: security.secmgroff Apr 9, 2010 3:48:31 PM com.sun.enterprise.security.ssl.SSLUtils checkCertificateDates SEVERE: java_security.expired_certificate Apr 9, 2010 3:48:31 PM com.sun.enterprise.security.SecurityLifecycle onInitialization INFO: Security startup service called Apr 9, 2010 3:48:31 PM com.sun.enterprise.security.PolicyLoader loadPolicy INFO: policy.loading Apr 9, 2010 3:48:32 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. Apr 9, 2010 3:48:32 PM com.sun.enterprise.security.auth.realm.Realm doInstantiate INFO: Realm file of classtype com.sun.enterprise.security.auth.realm.file.FileRealm successfully created. Apr 9, 2010 3:48:32 PM com.sun.enterprise.security.auth.realm.Realm doInstantiate INFO: Realm certificate of classtype com.sun.enterprise.security.auth.realm.certificate.CertificateRealm successfully created. Apr 9, 2010 3:48:32 PM com.sun.enterprise.security.SecurityLifecycle onInitialization INFO: Security service(s) started successfully.... Apr 9, 2010 3:48:33 PM com.sun.ejb.containers.BaseContainer initializeHome INFO: Portable JNDI names for EJB App : [java:global/classes/App!org.glassfish.embedded.samples.App, java:global/classes/App] Apr 9, 2010 3:48:34 PM org.glassfish.admin.mbeanserver.JMXStartupService shutdown INFO: JMXStartupService and JMXConnectors have been shut down. Apr 9, 2010 3:48:34 PM com.sun.enterprise.v3.server.AppServerStartup stop INFO: Shutdown procedure finished Apr 9, 2010 3:48:34 PM AppServerStartup run INFO: [Thread[GlassFish Kernel Main Thread,5,main]] exiting Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 18.61 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0The key log messages are highlighted in bold and are described:
- "mvn clean test" builds the project and runs the test.
- The server started in under 3 seconds, 2703 ms to be precise.
- The portable JNDI name for the EJB is "java:global/classes/App" (convenience name for a bean with one view) and the fully-qualified notation is "java:global/classes/App!org.glassfish.embedded.samples.App". The convenience name is constructed in the "global" namespace + the unqualified name of the directory + bean name. The later is created by adding the fully-qualified name of the interface to the former.
- The server shuts down after the tests are run, shows that 2 tests (one default + one newly added) ran, and both passed.
So no explicit GlassFish downloading and/or configuring, just deployed a simple EJB, and ran the tests – everything within one VM.
A future blog will show how to add other Java EE functionality to this app.
Technorati: totd embedded ejb glassfish javaee v3
Related posts:- TOTD #132: Servlets 3.0 in Embedded GlassFish Reloaded – lightweight Java EE 6
- TOTD #133: JPA2 (JPQL & Criteria), JavaDB, and embedded GlassFish – perfect recipe for testing
- TOTD #75: Getting Started with Grails using GlassFish v3 Embedded
- Grails on GlassFish v3 – Embedded or Stand-alone
- TOTD #100: Getting Started with Scala Lift on GlassFish v3
Watch out for issues like http://forums.java.net/jive/thread.jspa?messageID=396981. Nice writeup.
Best,
Laird
Comment by Laird Nelson — April 14, 2010 @ 11:51 am
And Maven users: make sure your maven-surefire-plugin configuration includes <useManifestOnlyJar>false</useManifestOnlyJar>.
Comment by Laird Nelson — April 14, 2010 @ 11:53 am
Thanks Laird!
I plan to address that issue in a subsequent blog.
Comment by Arun Gupta — April 14, 2010 @ 11:53 am
Laird,
I did not have to make that change in my pom.xml, guess will need to understand that attribute better now
Comment by Arun Gupta — April 14, 2010 @ 11:56 am
Dear Arun,
Thanks for your post, but for people who participated of Java EE 6 Code Camp, there is no news.
One thing that still puzzles me is how to make tests that rely on databases or other resources. In a previous project we used to create initial database with some data (pre condition setup) and then we tested some algorithms, it was easy to do because of AbstractTransactionalJUnit4SpringContextTests, it worked like a charm.
Now in my current (part time) project we decided to follow the Java EE path, trying to avoid including any new dependencies other than glassfish and junit, but we found a bit painful to create similar test cases.
Do you have any samples on how to create test cases with EJBs, JPA and DataSources ?
Best Regards,
Alessandro
Comment by Alessandro Oliveira — April 15, 2010 @ 5:03 pm
Right; that’s because maven-surefire-plugin’s default <forkMode> is "once". When that is true, Maven doesn’t ever try to use a manifest-only jar. Try setting it to "always" and watch what happens.
Comment by Laird Nelson — April 17, 2010 @ 10:02 am
This is the right repository-url: "http://download.java.net/maven/glassfish"
Comment by Claus Polanka — April 18, 2010 @ 1:52 pm
Laird,
Ah, that might be it.
Claus,
Worked with http://download.java.net/maven/2 as well.
Comment by Arun Gupta — April 19, 2010 @ 3:07 pm
Alessandro,
I do plan to publish a blog highlighting embeddable EJB with database in next few days, just looking for some dedicated time to do it
Comment by Arun Gupta — April 19, 2010 @ 3:10 pm
Hi Arun, Thank you so much for posting this information. Although it helped me some, I am still encountering some issues. I’ve done as you suggested and am having difficulty accessing the maven embeddable EJB. It installs but one I run the the test, I get the following error (captured from surfire-reports):
——————————————————————————-
Test set: org.glassfish.embedded.samples.AppTest
——————————————————————————-
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 178.453 sec <<< FAILURE!
testEJB(org.glassfish.embedded.samples.AppTest) Time elapsed: 178.437 sec <<< ERROR!
javax.ejb.EJBException: No EJBContainer provider available
The following providers:
org.glassfish.ejb.embedded.EJBContainerProviderImpl
Returned null from createEJBContainer call.
at javax.ejb.embeddable.EJBContainer.reportError(EJBContainer.java:200)
at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:135)
at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:92)
at org.glassfish.embedded.samples.AppTest.testEJB(AppTest.java:21)
Any suggestions? I tried both url’s listed and a couple of others. Thank you!!
Comment by Teresa — April 21, 2010 @ 4:20 pm
Teresa, I would like to ask, have you solved your problem? I have same issue, and I don’t know how to fix it. If you have fixed issue with container, please help us
Comment by devalentino — August 19, 2011 @ 4:30 pm