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

Creating a Joke Application in React

Introduction I've recently started learning React. I've been a backend developer for a long time, but have started to love doing client side work. As they say, the best way to learn is to practise. So here is a simple joke application that I've written in React. The source code for the application can be found on GitHub at: https://github.com/doobrie/react-joke Creating the project Whilst practising, I've quite often created projects from scratch, but this is quite tedious as the same steps need to be taken for each project. I needed to create the project structure, configure Babel, write some control scripts etc. Instead of doing that this time, I've used the create-react-app tool to scaffold the basics of an application. npx create-react-app This sets up everything that you need to get started with a React app. Coding As this is a simple project, I've created one react component, function app() . I've created this as a functional c...

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.

The new Eclipse logo

In a blog post today, Ian Skerrett has announced the new logo for Eclipse. Last fall we [The Eclipse Foundation] started a process to  update the Eclipse logo . The existing logo had not been change since Eclipse was launched and it was showing its age. I am happy to announce we have finished the process and am pleased to introduce the updated Eclipse logo. The new logo has a fresh modern look to it, which I think is a good improvement and will stand Eclipse in good stead for the future. The new logo will be included with the next Eclipse release train, Luna, and gradually rolled out across the Eclipse site and projects. What do you think?  Do you like the new logo?  Add your comments below.