C H A P T E R 7 |
Developing Message-Driven Beans |
The EJB Builder in the Sun ONE Studio 5 IDE enables you to develop the message-driven beans that you need to support an application client's requests for asynchronous processes. This chapter discusses the process of creating and working with message-driven beans. These beans' transactions are normally managed by the EJB container, but you can provide the transaction-management code yourself, if you prefer.
There are several reasons to use a message-driven bean:
However, message-driven beans aren't always the right answer. For example, an alternative would probably work better in the following cases:
For more pros and cons, see Understanding Message-Driven Beans.
The IDE provides a wizard that lets you create the single bean class required for a message-driven bean. Because a message-driven bean merely takes messages from a client and uses them to start other bean processes, no interface classes are needed. The wizard automates much of the task of creating a message-driven bean, and you finish the task using the IDE's Source Editor and property sheets.
When programming message-driven beans, you have options besides those described in this chapter. Although the Sun ONE Studio 5 IDE is designed to take care of much of your coding work, the IDE also supports those options flexibly and leaves many decisions up to you. For more information, refer to the resources listed in Before You Read This Book, or to one of the many excellent texts on programming enterprise beans.
The EJB Builder is a collection of wizards, property sheets, and editors with which you can build enterprise beans consistently and easily. To see if the EJB Builder is installed, go to the main window and choose Tools Options IDE Configuration System Modules J2EE Support. If you see EJB 2.0 Builder in the list of modules, and the Enabled field in the property sheet is set to True, the EJB Builder is ready for use.
You can take several approaches to creating message-driven beans in the IDE. However, you get the most comprehensive support and, in general, the fastest path to bean completion, if you use the approach recommended in this chapter. The methodology described here takes full advantage of the IDE's ability to ensure consistency and its adherence to the J2EE standard.
For best results, use the EJB Builder to program message-driven beans by:
From a message-driven bean's logical node, you can validate the bean's code.
Before you begin creating a message-driven bean, first consider whether to have the EJB container manage any transactions that your bean will do, or whether to write that code yourself. You use different processes in the IDE's EJB Builder to create the two kinds of bean. TABLE 7-1 highlights the design considerations.
For more information on these selections, refer to the chapter on transactions in the book Building J2EE Applications.
The rest of this chapter addresses how to create message-driven beans of each kind and the issues to consider during development.
The EJB Builder wizard automates much of the task of creating the one bean class that your message-driven bean requires. To define a message-driven bean, you take the following steps:
1. Select or create a package to contain the bean.
2. Use the EJB Builder wizard to generate the infrastructure of your message-driven bean.
3. Complete the body of the onMessage method and, if necessary, the setMessageDrivenContext and ejbCreate methods.
These basic steps are explained in detail next.
After you finish the steps covered in this chapter, you must add information to your finished bean's property sheet so that it can interact with other beans, find its resources, and listen for the appropriate messages. These steps, which prepare your finished bean to work in an application, are discussed in Chapter 8.
If you need to create a package to house your message-driven bean, select a filesystem, right-click, and choose New Java Package.
When you're ready to create a message-driven bean, do as follows:
1. In the IDE's main window, choose View Explorer to open the Explorer window.
2. In the Filesystems tab of the Explorer, select the package or filesystem where you want your message-driven bean to reside.
3. Right-click and choose New J2EE Message-Driven EJB.
The EJB Builder wizard appears, displaying New Wizard-Message-Driven EJB in the window's title bar.
In the EJB Builder`s Message-Driven Bean Name and Properties page, name your message-driven bean and decide how to manage any transactions the bean performs. The default is Container-Managed Transactions, but you can decide to provide all transaction management code in the bean class if you wish.
When you have made your selection, you can click Finish. (Or, you can click Next to go to the page in which you can specify an existing bean class for your message-driven bean. After that, you click Finish.)
Your newly created message-driven bean appears in the Filesystems tab of the IDE's Explorer. The bean's infrastructure (its basic bean class and its two component methods) has been generated automatically by the EJB Builder.
FIGURE 7-1 shows how a typical message-driven bean appears in the Explorer's Filesystems tab.
Of the two primary nodes shown, one is a logical node (marked with a bean icon) and one represents the actual class (marked with a class icon). Do all your editing in the logical node. The bean's two primary nodes are described next.
The Classes node contains the bean class code, which includes both methods. The Create Method node points to the code that initializes your message-driven bean. The OnMessage Method node points to the method that is invoked when a message is received.
When you expand the two nodes under your message-driven bean's package node, you see something like the tree view in FIGURE 7-2.
The wizard automatically places certain default methods in each message-driven bean: a create method, an onMessage method, and two life-cycle methods. As shown in TABLE 7-2, the create method, ejbCreate, behaves much like create methods in other types of enterprise beans, but onMessage is a new and different kind of method.
The wizard also adds the default life-cycle methods described in TABLE 7-3.
To complete your message-driven bean, do the following:
Make your additions in the Explorer by clicking bean components under the logical bean node to open the Source Editor.
Appendix A discusses the best ways to make changes in your enterprise beans, and the errors and anomalies that you might see if you use other approaches. As a general rule, you should work through the logical node rather than the individual class nodes, use the bean's property sheets or the Customizer dialog box to edit methods, and use the IDE's Source Editor to complete or edit any bean code that isn't available to you through one of the dialog boxes.
A single instance of your message-driven bean can handle only one message at a time, and the bean can have only one onMessage method. An example of a completed method follows.
The setMessageDrivenContext method stores the message-driven context reference in a field and populates non-persistent fields. You can, if necessary, use this method to allocate resources that are independent of the bean object and last as long as the bean exists. These resources might include a queue-connection or topic-connection factory.
By default, the EJB Builder wizard generates code that assigns the message-driven context to a non-persistent field named context. Ordinarily, you don't need to add anything to the generated method. However, if you need to complete it, copy the generated context into the instance variable. For example:
public void setMessageDrivenContext(javax.ejb.MessageDrivenContext aContext ) { this.context=context; } |
Your message-driven bean is now finished, except for a few steps that prepare the bean to work in its eventual environment. You must specify the following in the bean's property sheet:
If your message-driven bean will receive messages from a client, and you plan to deploy your bean to Sun ONE Application Server 7, you must specify the destination in the Sun ONE AS tab of the bean's property sheet.
If your message-driven bean will act as a client itself, sending messages to a destination, you must specify the following in the References tabbed interface of the bean's property sheet:
These property settings are discussed next.
To specify whether the message-driven bean will be a queue listener or a topic listener, do as follows:
1. In the IDE's Explorer window, right-click the message-driven bean's logical node and choose Properties.
The property sheet for the bean appears.
2. In the Properties tabbed interface, click the Message-Driven Destination field and then the ellipsis (...) button.
3. Select Queue, Topic, or (Not Set).
4. Click OK to dismiss the property editor.
If you want to filter your bean's incoming messages, do as follows:
1. Click the Message Selector field and then the ellipsis (...) button.
2. Specify a filter if you wish to reduce the number of messages for which your bean must listen.
3. Click OK to dismiss the property editor.
The References tabbed interface of a message-driven bean's property sheet contains the Resource Reference and Resource Environment Reference fields. These fields are completed on behalf of the client that sends messages. For example, your message-driven bean might be part of an application in which a web module sends messages to a queue for consumption by your bean. In that case, this Resource Reference and this Resource Environment Reference should be specified by the provider of the web module.
Or, if your message-driven bean is meant to act as a client within its own module, sending messages to a queue or topic, you specify the resource factory and the resource here.
To associate the message-driven bean with a factory object that will create the destination object, do as follows:
1. In the References tabbed interface, click the Resource References field and then the ellipsis (...) button.
In the property editor are fields for specifying the connection factory that the client (or message-driven bean as client) will use to gain access to its messaging resource.
The Add Resource Reference dialog box appears with two tabbed interfaces, Standard and Sun ONE App Server.
If you are deploying your message-driven bean to Sun ONE Application Server 7, also complete the following fields.
3. When you're finished, click OK to dismiss the dialog box.
To associate the message-driven bean with a particular destination object, do as follows:
1. In the References tabbed interface, click the Resource Environment References field and then the ellipsis (...) button.
In the property editor are fields for specifying the actual resources to which the client (or message-driven bean as client) will send messages.
The Add Resource Environment Reference dialog box appears with two tabbed interfaces.
If you plan to deploy your message-driven bean to Sun ONE Application Server 7, also complete the following field.
3. When you're finished, click OK to dismiss the dialog box.
Read more about message-driven destinations in Chapter 2 and more about setting properties in Chapter 8.
Recommendations for working with finished enterprise beans are given in Appendix A.
The messaging tier of your application will run into fewer problems if you understand the following possible complications.
For detailed design considerations, refer to Enterprise JavaBeans Specification, version 2.0 and to texts on programming enterprise beans.
Enterprise beans can be a very powerful and flexible part of your application. Creating the basic parts of an enterprise bean can be very simple, especially with a tool like the Sun ONE Studio 5 IDE. However, completing the bean so that it satisfies the needs of your application can be somewhat more complex. For details, refer to Enterprise JavaBeans Specification, version 2.0 at:
http://java.sun.com/products/ejb/docs.html and to
The J2EE Tutorial Addendum at: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JMSJ2EEex.html
Copyright © 2003, Sun Microsystems, Inc. All rights reserved.