Accessing an EJB from a Java Application using an embeddable container

The embeddable EJB container allows EJBs to be executed outside of a Java EE environment. Standalone Java SE applications can use the embeddable container to execute EJBs. In addition, it can be used for unit testing of EJBs.

The embeddable container does not require the installation of a server. As a result it has a smaller footprint and will start faster. However, MDBs and inbound RMI over IIOP (RMI/IIOP) (Remote Method Invocation (RMI) (Internet Inter-Orb Protocol (IIOP)) calls are not supported. Efficiency features found in the Java EE environment, like clustering, are not available.

How to do it...

Create a standard Java SE application with the following main method.

public class Main {
public static void main(String[] args) {
try {
Map properties = new HashMap();
properties.put(EJBContainer.MODULES, new java.io.File( "E:\Packt\Projects\CapitalApplication\ build\classes"));
properties.put(EJBContainer.APP_NAME,"CapitalApplication");
EJBContainer ejbC = EJBContainer.createEJBContainer(properties);
Context context = ejbC.getContext();
String name = "java:global/CapitalApplication/CapitalBean";
CapitalBeanRemote bean = (CapitalBeanRemote)context.lookup(name);
System.out.println(bean.getCapital("Japan"));
} catch (NamingException e) {
e.printStackTrace();
}
}
}

The Java SE application requires a few JAR files be included in its classpath. These include:

  • The embedded EJB container supplied by the server. In the case of GlassFish it is the glassfish-embedded-static-shell.jar file.
  • The javax.ejb.jar file also provided by the server.
  • The JAR file containing the CapitalBean class.

Use the property window of the application to add these files before executing the application.

How it works...

We declared a Map variable called properties and assigned to it a new HashMap. The variable was used to initialize the EJBContainer. This container has a property, MODULES, specifying the modules to be used by the container. The Map object's put method was used to assign the location of the directory containing the CapitalApplication's classes.

The EJBContainer.APP_NAME field was used to specify the name of the application. Next, the EJBContainer class's static method, createEJBContainer, was called with the properties variable as an argument. The createEJBContainer method returns an EJBContainer object. This class's getContext method was used to get a Context object. The Context object represents the environment needed to find and use the CapitalBean.

Next, a portable JNDI name for CapitalBean was used as an argument to the lookup method. This returned an effective reference to the CapitalBean.

The bean variable was used to invoke the getCapital method and to display the results. The last step caught any exceptions thrown.

The output should appear as:

Tokyo

Note

The embeddable container is a new EJB 3.1 feature. Not all development environments or servers support this feature. Using NetBeans 6.91 with GlassFish 3.0.1 may not work consistently. However, NetBeans 7 Beta does provide better support.

See also

The previous Accessing an EJB from a Java Application using JNDI recipe, provides an alternative technique for accessing EJBs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.106