TOTD #128: EJBContainer.createEJBContainer: Embedded EJB using GlassFish v3

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.

  1. Create a Maven project as:
    mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.glassfish.embedded.samples -DartifactId=ejb31
    
    
  2. 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>
    

  3. 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.
  4. 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.
  5. 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: 0
    
    
    
    The key log messages are highlighted in bold and are described:
    1.  "mvn clean test" builds the project and runs the test.
    2. The server started in under 3 seconds, 2703 ms to be precise.
    3. 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.
    4. 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

Comments:

Watch out for issues like http://forums.java.net/jive/thread.jspa?messageID=396981. Nice writeup.

Best,
Laird

Posted by Laird Nelson on April 14, 2010 at 04:51 AM PDT #

And Maven users: make sure your maven-surefire-plugin configuration includes <useManifestOnlyJar>false</useManifestOnlyJar>.

Posted by Laird Nelson on April 14, 2010 at 04:53 AM PDT #

Thanks Laird!

I plan to address that issue in a subsequent blog.

Posted by Arun Gupta on April 14, 2010 at 04:53 AM PDT #

Laird,

I did not have to make that change in my pom.xml, guess will need to understand that attribute better now :-)

Posted by Arun Gupta on April 14, 2010 at 04:56 AM PDT #

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

Posted by Alessandro Oliveira on April 15, 2010 at 10:03 AM PDT #

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.

Posted by Laird Nelson on April 17, 2010 at 03:02 AM PDT #

This is the right repository-url: "http://download.java.net/maven/glassfish"

Posted by Claus Polanka on April 18, 2010 at 06:52 AM PDT #

Laird,

Ah, that might be it.

Claus,

Worked with http://download.java.net/maven/2 as well.

Posted by Arun Gupta on April 19, 2010 at 08:07 AM PDT #

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 :-)

Posted by Arun Gupta on April 19, 2010 at 08:10 AM PDT #

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!!

Posted by Teresa on April 21, 2010 at 09:20 AM PDT #

Does anybody solver problem with "No EJBContainer provider available" exception? Give us solution if you have it

Posted by devalentino on August 19, 2011 at 12:01 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today