Enterprise JavaBeans

Enterprise JavaBeans

4.3 3
by Richard Monson-Haefel

View All Available Formats & Editions

Enterprise JavaBeans, Fourth Edition, is the definitive guide to EJB 2.1. It shows you how to build complex, mission-critical systems using snap-together software components that model business objects and processes. EJB 2.1 makes several important steps forward in EJB technology: message-driven beans are much more flexible, a time service has been added, and EJBs


Enterprise JavaBeans, Fourth Edition, is the definitive guide to EJB 2.1. It shows you how to build complex, mission-critical systems using snap-together software components that model business objects and processes. EJB 2.1 makes several important steps forward in EJB technology: message-driven beans are much more flexible, a time service has been added, and EJBs have been integrated with web services. Enterprise JavaBeans delivers on a promise that was astonishing a few years ago: not only can EJBs run without modification on any operating system, they can run on any J2EE application server. However, after writing EJBs, you have to deploy them in an application server, and deploying EJBs can be a painful task. This edition includes the JBoss Workbook, which shows you how to deploy the examples on the open source JBoss Application Server. If you've done any enterprise software development in the past few years, you already know the extent to which EJB has changed the field. Use this book to catch up on the latest developments. If you're new to enterprise software development, or if you haven't been working with EJB, this book will bring you up to speed on this exciting technology for building business systems.

Editorial Reviews

The Barnes & Noble Review
With the new EJB 2.0 spec, Enterprise JavaBeans becomes an even powerful platform for enterprise and web development. Richard Monson-Haefel has just updated his #1 EJB guide to reflect the new spec. But whether you're on the bleeding edge or involved in projects based on EJB 1.1, this book is just your ticket.

Monson-Haefel offers expert insight into EJB architecture, design, and coding. You'll find detailed coverage of EJB primary services, entity relationships, queries, session beans, transactions, and more. Wherever possible, Monson-Haefel provides code that's downward compatible, and he carefully explains the key differences between 1.1 and 2.0.

Sun has thoroughly overhauled container-managed persistence, simplifying the development of portable, database-independent applications. Monson-Haefel covers the 2.0 model in detail, while also covering EJB 1.1's approach. EJB 2.0 thoroughly integrates Java Message Service (JMS), helping EJBs participate more fully in loosely-connected web apps. Monson-Haefel, who coauthored O'Reilly's recent JMS guide, offers a full chapter on EJB 2.0's JMS support.

No EJB book has been honored as widely as Enterprise JavaBeans. The new edition is as indispensable as its predecessors. (Bill Camarda)

Bill Camarda is a consultant, writer, and web/multimedia content developer with nearly 20 years' experience in helping technology companies deploy and market advanced software, computing, and networking products and services. He served for nearly ten years as vice president of a New Jersey–based marketing company, where he supervised a wide range of graphics and web design projects. His 15 books include Special Edition Using Word 2000 and Upgrading & Fixing Networks For Dummies®, Second Edition.

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

Product Details

O'Reilly Media, Incorporated
Publication date:
Edition description:
Third Edition
Product dimensions:
6.86(w) x 9.00(h) x 1.09(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...

Meet the Author

Richard Monson-Haefel is one of the world's leading experts on Enterprise JavaBeans. He is the architect of OpenEJB, an open source EJB container, and has consulted as an architect on Enterprise JavaBeans, CORBA, Java RMI and other distributed computing projects over the past several years.

Richard is the co-author of O'Reilly's Java Message Service, as well as author of numerous articles. He maintains jMiddleware.com , a website with resources and articles dedicated to the discussion of EJB, JMS, J2EE and other Java middleware technologies.

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >

Enterprise JavaBeans 4 out of 5 based on 0 ratings. 4 reviews.
Guest More than 1 year ago
A REVIEW OF THE FOURTH EDITION] Each edition of this book seems to grow visibly thicker. Perhaps indicative of the still increasing functionality being put into EJBs. The book covers what is currently the latest version, EJB 2.1. Plus also EJB 2.0. Comprehensive. Detailed explanations of Container Managed Persistence and Bean Managed Persistence. Plus how to use Message-Driven Beans and JMS to help put together a loosely coupled distributed system. MDBs and JMS did not exist in the original EJBs. But a clear sense of the need for such emerged soon after, and MDB and JMS were the results. With the flurry of interest on Web Services, we see how EJB 2.1 is compatible with these proposed standards. In future editions, this section may well be heavily expanded. Right now, Web Services are still nascent and more experience is needed with fleshing out optimal standards. Kudos to the authors for including a lengthy section showing how to use JBoss. It is a free application server that supports most EJB 2.1 features. Its zero cost and advanced functionality will appeal to many programmers on a tight budget. This section walks you through combining JBoss with EJBs.
Guest More than 1 year ago
This is an excellent book if you want to gain thorough knowledge on EJBs. It is organized very well and the current edition fully covers EJB 1.1. Go for it!!!
Guest More than 1 year ago
good treatment to the subject. Not very verbose. differences between 1.0 and 1.1 are clarified. Good examples.
Guest More than 1 year ago
This EJB book is very popular and the first one I read. I brought this book because I need to use ejb next weeks. But this book does not help me a lot. It has almost all the import stuff, but very badly organized. It is hard to understand the concept since it always give new and not well defined concept I had three days' hard time until my collegue give another book, ' Enterprise Javabeans' by Tom Valesky, published by Addison-Wesley. Valesky's book is great. Very well organized, concept explained very clear. And it starts with very simple example. So I strongly recommend Tome Valesky's book not this one