Skip to main content

Quick Look at EJB 3.1 on JBoss AS 6

Now that JBoss AS 6.0 has been released with full support for the Java EE 6 Web Profile, lets take a quick look at some of the new features of EJB 3.1 that are available.

Three of the main features of EJB 3.1 are:


  1. EJB’s can be deployed as part of a WAR file and do not need to be is a separate EJB JAR file.
  2. EJB’s can be developed with no business interface.
  3. EJB’s can be deployed as Singletons.

Let’s take a look at each of these in turn.

1. EJB’s can be deployed as part of a WAR file.

Not much to look at here I’m afraid! The thing to notice is that now with NetBeans (I’m using NB 7 Beta), you can create a simple Java EE 6 Web Project and create EJB’s within the web project. If you are developing a web project, there is no longer any need for an additional JAR to deploy any EJB’s that you use.

2. EJB’s can be developed with no Business Interface

Creating an EJB without a Business Interface is as simple as creating a POJO, which in fact the EJB is! In it’s simplest form, an EJB could be defined as:

package com.davidsalter.ejb31test; 
import javax.ejb.Stateless; 
@Stateless
public class HelloSessionBean { 
    public String sayHello() { 
    return "Hi there !!"; 
    } 
}

The only difference here from a POJO is that the class is annotated with @Stateless, declaring it as a Stateless Session Bean.

When the WAR file containing this EJB is deployed to JBoss, we get a message on the JBoss console stating that the EJB has been deployed:

cl21:08:29,653 INFO  [org.jboss.ejb3.nointerface.impl.jndi.AbstractNoInterfaceViewBinder] Binding the following entry in Global JNDI for bean:HelloSessionBean

HelloSessionBean/no-interface -> EJB3.1 no-interface view

Invoking an EJB within a web project is similarly straightforward. The EJB can be injected into a Servlet using the @EJB annotation.

public class HelloServlet extends HttpServlet { 
    @EJB
    HelloSessionBean helloSessionBean; 
    ... 

    PrintWriter out = response.getWriter(); 
    out.println(helloSessionBean.sayHello());

3. EJB’s can be deployed as Singletons

Finally, EJB’s can easily be declared as Singletons via use of the @Singleton annotation. Used in conjunction with the @PostConstruct and @PreDestroy annotations, any initialization and clean up can easily be performed on the Singleton. The method annotated with @PostConstruct is invoked after the singleton is first constructed, for example when a calling servlet is first loaded or when a JSF backing bean that calls the singleton is first instantiated. The method annotated with @PreDestroy is called immediately before the Singleton is destroyed and is therefore a perfect place to perform any tidy up operations.

Defining Singletons in this way can be a useful technique for storing data that may be required across multiple clients of an application.

package com.davidsalter.ejb31test; 

import java.util.Date; 
import javax.annotation.PostConstruct; 
import javax.annotation.PreDestroy; 
import javax.ejb.Singleton; 

@Singleton
public class SingletonSessionBean { 

    private String creationDate; 

    @PostConstruct
    public void init() { 
        System.out.println("Initialize singleton"); 
        creationDate = new Date().toString(); 
    } 

    @PreDestroy
    public void shutdown() { 
        System.out.println("Shutting down singleton"); 
    } 

    public String getDate() { 
        return creationDate; 
    } 
}

Again, invoking the Singleton is as easy as invoking the Session Bean we defined earlier.

@EJB
SingletonSessionBean singletonSessionBean;

The sample code and application used for this post are available from my Subversion repository on Google Code at:

http://code.google.com/p/java-ee-samples/

To run the sample application, check the code out from subversion, build and deploy in NetBeans to JBoss AS 6 and access http://localhost:8080/EJB31Test/HelloServlet

Comments

Popular posts from this blog

A Simple Makefile for a GTK/GTKMM Project

When compiling small applications, its fairly easy just to compile using g++ from the command line. If you’re compiling anything more complex than a single file, its probably easier to use a Makefile. This example Makefile demonstrates how to compile an application that uses the GTKMM library. NAME=my-app CFLAGS=-g -Wall -o $(NAME) GTKFLAGS=`pkg-config --cflags --libs gtkmm-3.0` SRCS=main.cc myapp.cc CC=g++ # Do all all: main # Compile main: $(SRCS) $(CC) $(CFLAGS) $(SRCS) $(GTKFLAGS) # Clean clean: rm -f $(NAME) rm -f *.h~ rm -f *.cc~ rm -f Makefile~ rm -f *.glade~

Changing Default Search Provider in Firefox on Linux Mint

On Linux Mint, the default version of Firefox is installed and configured to allow the following search engines to be queried directly from the address bar: Yahoo! Startpage DuckDuckGo Wikipedia Mint defines these as the default available set of search engines based upon 3 criteria (funding to Linux Mint, privacy support and whether the search engine is non-commercial). Other search engines such as Google, Bing or Twitter, etc. can easily be added into the default version of Firefox however. To add a different search provider, browse to Search Engines At the bottom of the page, click on the icon of the requested search engine, then click on the ... button in the URL bar and select the Add Search Engine option. You then have the option to change the default search engine within Firefox preferences to your new choice.

Eclipse releases Ganymede

The Eclipse Foundation has  released  Ganymede, the latest annual release this time containing 23 Eclipse projects including the Eclipse IDE version 3.4. This latest version of the annual Eclipse release contains many new features including: Equinox P2  - a system to make installations and updated to Eclipse easier JSDT  - A new JavaScript editor Improved JavaScript support for the Business Intelligence and Reporting Tools,  BIRT Graphical database query tools Improved support for Java EE 5 SOA support and many other features. Further information, inclusing webinars and demos can be found on the  project website  . This new release can be downloaded from the  Eclipse web site  .