Book Image

EJB 3.1 Cookbook

By : Richard M. Reese
Book Image

EJB 3.1 Cookbook

By: Richard M. Reese

Overview of this book

<p>Enterprise Java Beans enable rapid and simplified development of secure and portable applications based on Java technology.Creating and using EJBs can be challenging and rewarding. Among the challenges are learning the EJB technology itself, learning how to use the development environment you have chosen for EJB development, and the testing of the EJBs.<br /><br />This EJB 3.1 Cookbook addresses all these challenges and covers new 3.1 features, along with explanations of useful retained features from earlier versions. It brings the reader quickly up to speed on how to use EJB 3.1 techniques through the use of step-by-step examples without the need to use multiple incompatible resources. The coverage is concise and to the point, and is organized to allow you to quickly find and learn those features of interest to you.<br /><br />The book starts with coverage of EJB clients. The reader can choose the chapters and recipes which best address his or her specific needs. The newer EJB technologies presented include singleton beans which support application wide needs and interceptors to permit processing before and after a target method is invoked. Asynchronous invocation of methods and enhancements to the timer service are also covered. <br /><br />The EJB 3.1 CookBook is a very straightforward and rewarding source of techniques supporting Java EE applications.</p>
Table of Contents (19 chapters)
EJB 3.1 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Accessing an EJB from a Java Application using JNDI


It would be nice if we could use dependency injection outside of a server container. However, this is not possible from a Java SE application unless we use an embeddable container. Using an embeddable container is covered in the next recipe. Here we need to use JNDI. Accessing an EJB from a Java SE application using JNDI is similar to using JNDI in other types of applications.

Getting ready

To use this approach we need:

  1. A supporting EJB

  2. JNDI code to obtain a reference to the EJB

We will be using the CapitalApplication developed in the Accessing an EJB from an Applet recipe found in this chapter. This recipe uses a CapitalBean to return the name of the capital given a state. Make sure the server and this application are executing before testing the Java application.

How to do it...

The EJB used here is the CapitalBean. Create a Java SE application using an IDE of your choice. It does not have to be the same one you used to develop the CapitalApplication. In the main method add:

  try {
    InitialContext context = new InitialContext();
    String name = "java:global/CapitalApplication/CapitalBean";
    CapitalBeanRemote bean = (CapitalBeanRemote)context.lookup(name);
    System.out.println(bean.getCapital("India"));
  } 
  catch(javax.naming.NoInitialContextException e) {
    e.printStackTrace();
  }
  catch (NamingException e) {
    e.printStackTrace();
  }

Make sure the application's classpath contains .jar files for the CapitalBean and the appserv-rt.jar file.

When executed, the output should appear as:

New Delhi

How it works...

The application JAR file was needed to resolve the class names and the appserv-rt.jar file was needed so JNDI could function properly. This file provided the necessary information for JNDI to locate the server and look up the name correctly.

See also

The Accessing the session bean using JNDI recipe provides more detail on the use of JNDI.