Enterprise JavaBeans
  • Enterprise JavaBeans
  • Enterprise JavaBeans

Enterprise JavaBeans

4.3 3
by Richard Monson-Haefel, Bill Burke, Sacha Labourey

View All Available Formats & Editions

This new edition introduces developers to the newest EJB specifications, and extends support for Web services and the Java Web services APIs.See more details below


This new edition introduces developers to the newest EJB specifications, and extends support for Web services and the Java Web services APIs.

Editorial Reviews

Gregory V. Wilson

Enterprise JavaBeans (EJBs) may be the clumsiest name that our industry has come up with recently -- but the technology is anything but. Enterprise JavaBeans combines distributed components with transaction monitors to produce an infrastructure that can manage database-style transactions, security, object persistence, and resource management in a standardized way. Enterprise JavaBeans has nothing in common with plain ol' JavaBeans except its name, and it is much more similar to the Microsoft Transaction Server (MTS).

Richard Monson-Haefel's Enterprise JavaBeans starts by describing the historical and technological background to EJB, including the development of transaction monitors over the last 30 years, and the underpinnings of distributed-object technology. Chapter 2 then looks at the EJB architecture, while Chapter 3 discusses resource management. As part of this discussion, Monson-Haefel looks at how EJB handles the six primary services defined by the OMG: concurrency, transactions, persistence, distributed objects, naming, and security. This discussion is informative, but noncritical -- the book never suggests that EJB has weaknesses, or that a different approach to some technical problem (or more rigorous standardization) might have been more flexible or easier to implement.

Chapter 4 describes how to implement some simple EJBs. As with other examples in the book, most of this material is taken from a system to manage bookings for a holiday cruise line. Chapters 5 through 8 then discuss how EJBs are used by client-size applications, the development of entity beans (that is, components that describe real-world objects such as cruise ships), session beans (which describe business logic such as the booking of a cruise) and EJB's support for transaction management. The last chapter, "Design Strategies," then gives some tips on how to go about developing EJB-based applications. The book closes with three appendices: One on the EJB API, one (which I found very helpful) that gives UML state and sequence diagrams for various operations, and one (which is already out of date) that provides information about EJB vendors.

Enterprise JavaBeans is well written, and well edited: I did not notice any awkward sentences, typographical errors, or ugly diagrams. In addition, Monson-Haefel's examples are easy to follow. His discussion of technical issues is clear, but would have been better, in my opinion, if it had been a little more critical, or if it made more comparisons between EJB and other systems. (One of the things that makes Szyperski's Component Software such an excellent book is the way its author does precisely these things.) Overall, Enterprise JavaBeans is a good starting point for anyone who is interested in the subject, and a reasonable reference for anyone who is already in the middle of an EJB project.
Electronic Review of Computer Books

Read More

Product Details

O'Reilly Media, Incorporated
Publication date:
Edition description:
Fourth Edition
Product dimensions:
6.96(w) x 9.16(h) x 1.42(d)

Read an Excerpt

Chapter 13: Message-Driven Beans

This chapter is divided into two subsections: "JMS as a Resource" and "Message-Driven Beans." The first section describes the Java Message Service (JMS) and its role as a resource that is available to any enterprise bean (session, entity, or message-driven). Readers unfamiliar with JMS should read the first section before proceeding to the second section.

The second section in this chapter provides an overview of the new enterprise bean type--the message-driven bean. A message-driven bean is an asynchronous bean activated by message delivery. In EJB 2.0, vendors are required to support JMS-based message-driven bean, that receive JMS messages from specific topics or queues and process those messages as they are delivered.

All EJB 2.0 vendors must, by default, support a JMS provider. Most EJB 2.0 vendors have a JMS provider built in, but some may also support other JMS providers. Regardless of how the EJB 2.0 vendor provides the JMS service, having one is a requirement if the vendor expects to support message-driven beans. The advantage of this forced adoption of JMS is that EJB developers can expect to have a working JMS provider on which messages can be both sent and received.

JMS as a Resource

JMS is a standard vendor-neutral API that is part of the J2EE platform and can be used to access enterprise messaging systems. Enterprise messaging systems (a.k.a. message-oriented middleware) facilitate the exchange of messages among software applications over a network. JMS is analogous to JDBC: whereas JDBC is an API that can be used to access many different relational databases, JMS provides the same vendor-independent access to enterprise messaging systems. Many enterprise messaging products currently support JMS, including IBM's MQSeries, BEA's WebLogic JMS service, Sun Microsystems' iPlanet Message Queue, and Progress' SonicMQ, to name a few. Software applications that use the JMS API for sending or receiving messages are portable across brands of JMS vendors.

Java applications that use JMS are called JMS clients, and the messaging system that handles routing and delivery of messages is called the JMS provider. A JMS application is a business system composed of many JMS clients and, generally, one JMS provider.

A JMS client that sends a message is called a producer, while a JMS client that receives a message is called a consumer. A single JMS client can be both a producer and a consumer. When we use the term consumer or producer, we mean a JMS client that receives messages or sends messages, respectively.

In EJB, enterprise beans of all types can use JMS to send messages to various destinations. Those messages are consumed by other Java applications or message-driven beans. JMS facilitates sending messages from enterprise beans by using a messaging service, sometimes called a message broker or router. Message brokers have been around for a couple of decades--the oldest and most established being IBM's MQSeries--but JMS is fairly new and is specifically designed to deliver a variety of message types from one Java application to another.

Reimplementing the TravelAgent EJB with JMS

We can modify the TravelAgent EJB developed in Chapter 12 so that it uses JMS to alert some other Java application that a reservation has been made. The following code shows how to modify the bookPassage( ) method so that the TravelAgent EJB will send a simple text message based on the description information from the TicketDO object:

public TicketDO bookPassage(CreditCardDO card, double price)
    throws IncompleteConversationalState {
    if (customer == null || cruise == null || cabin == null) {
        throw new IncompleteConversationalState(  );
    try {
        ReservationHomeLocal resHome = (ReservationHomeLocal)
        ReservationLocal reservation =
            resHome.create(customer, cruise, cabin, price, new Date(  ));
        Object ref = jndiContext.lookup
        ProcessPaymentHomeRemote ppHome = (ProcessPaymentHomeRemote)
            PortableRemoteObject.narrow(ref, ProcessPaymentHomeRemote.class);
        ProcessPaymentRemote process = ppHome.create(  );
        process.byCredit(customer, card, price);
        TicketDO ticket = new TicketDO(customer,cruise,cabin,price);
        String ticketDescription = ticket.toString(  );
        TopicConnectionFactory factory = (TopicConnectionFactory)
        Topic topic = (Topic)
        TopicConnection connect = factory.createTopicConnection(  );
        TopicSession session = connect.createTopicSession(true,0);
        TopicPublisher publisher = session.createPublisher(topic);
        TextMessage textMsg = session.createTextMessage(  );
        connect.close(  );
        return ticket;
    } catch(Exception e) {
        throw new EJBException(e);

To be able to send a message, we needed to add a lot of new code. However, while it may look a little overwhelming at first, the basics of JMS are not all that complicated.

TopicConnectionFactory and Topic

In order to send a JMS message we need a connection to the JMS provider and a destination address for the message. The connection to the JMS provider is made possible by a JMS connection factory; the destination address of the message is identified by a Topic object. Both the connection factory and the Topic object are obtained from the TravelAgent EJB's JNDI ENC:

TopicConnectionFactory factory = (TopicConnectionFactory)
Topic topic = (Topic)

The TopicConnectionFactory in JMS is similar in function to the DataSource in JDBC. Just as the DataSource provides a JDBC connection to a database, the TopicConnectionFactory provides a JMS connection to a message router.1

The Topic object itself represents a network-independent destination to which the message will be addressed. In JMS, messages are sent to destinations--either topics or queues--instead of directly to other applications. A Topic is analogous to an email list or newsgroup; any application with the proper credentials can receive messages from and send messages to a Topic. When a JMS client receives messages from a Topic, the client is said to subscribe to that Topic. JMS decouples applications by allowing them to send messages to each other through a destination, which serves as virtual channel. This example uses a Topic type destination, but JMS also supports Queue type destinations. The difference between these types is explained in more detail later.

TopicConnection and TopicSession

The TopicConnectionFactory is used to create a TopicConnection, which is an actual connection to the JMS provider...

Read More

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >