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.
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:
glassfish-embedded-static-shell.jar
file. javax.ejb.jar
file also provided by the server. CapitalBean
class.Use the property window of the application to add these files before executing the application.
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.
Tokyo
3.129.194.106