Chapter 1. Getting Started With EJBs

In this chapter, we will cover:

  • Creating a simple session EJB
  • Accessing a session bean using dependency injection
  • Accessing the session bean using JNDI
  • Creating a simple message-driven bean
  • Sending a message to a message-driven bean
  • Accessing an EJB from a web service (JAX-WS)
  • Accessing an EJB from a web service (JAX-RS)
  • Accessing an EJB from an Applet
  • Accessing an EJB from JSP
  • Calling an EJB from JSF
  • Accessing an EJB from a Java Application using JNDI
  • Accessing an EJB from a Java Application using an embeddable container
  • Accessing the EJB container

Introduction

Creating and using Enterprise Java Beans (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. The examples used throughout the book were developed and tested using NetBeans 6.9.1 and GlassFish Server Open Source Edition v3.0.1. NetBeans and GlassFish can be downloaded bundled from http://netbeans.org/downloads/index.html. On this page are several bundle combinations. Use the one title Java. The GlassFish Enterprise Server v3 Prelude also supports EJB 3.1.

In addition, Windows 7 Professional 64 bit edition, service pack 1, was used to develop the applications. The Mozilla Firefox v3.6.15 browser was used to display web pages.

EJBs can be used in many different contexts and called from many different types of applications. In presenting EJBs, a fundamental question is this: how do we go about illustrating the use of an EJB before we know what an EJB is? How do we learn about EJBs unless we know how to call them? The approach taken in this chapter is to first demonstrate how to create simple Session Beans and Message-Driven Beans. Knowing how to create these beans will then allow us to use them from different contexts. The details of session and message bean construction and their use are covered in subsequent chapters. We are concerned with identifying some of the more common clients and then seeing how they call and use EJBs. EJBs can be used within a number of different clients including servlets, JSP, JSF, applets, Java SE type applications, and other EJBs.

From the client perspective, it is accessing an EJB as if the EJB existed in the same Java Virtual Machine (JVM). Depending on the client, this may be true. Regardless, EJBs are managed by an EJB container that provides support not readily available to other objects. This support can be in the form of security, transaction processing or concurrency management.

Using a bean involves declaring a reference to the bean, creating an instance of the bean and then calling the methods of the bean. There are two techniques for gaining access to an EJB: Dependency Injection (DI) and the Java Naming and Directory Service (JNDI). DI is the easiest when we can use it, but JNDI can be used in places where DI is not supported. We will look at both of these approaches. However, EJBs should not be created using the Java new keyword. If the EJB is created using this keyword, then it will no longer be an EJB but rather a regular object. It will not be able to take advantage of the support provided by the EJB container.

When an application is created using NetBeans, it will normally consist of an application-ejb and an application-war module. Other development environments may take a different approach. In addition, Java EE applications are normally deployed as an .ear file.

An important element of Java EE applications is entities which support the persistence of application data. The Java Persistence API (JPA) supports the use of entities in an EE application and the traditional Java application. While they are not introduced here, they are typically called indirectly through a session bean. This topic is covered in Chapter 4,

Some of the recipes are dependent on earlier recipes. For example, the session EJB developed in the first recipe is reused in the second recipe. This approach permits the reuse of code which is always a good development practice.

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

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