This chapter covers how to create a Message Driven Bean (MDB) EJB component. We will create two MDB beans, DeliverItems and RequestItems as shown below. The DeliverItems bean will replenish the stocks of various items within MyStore, and the RequestItems bean will send requests to various suppliers to deliver items which are out of stock. The MyStore manager will issue/send this request.
Note : Both message-driven beans access the StoreAccess bean through its remote interface, even though the StoreAcessBean is in the same JVM. This is because we have implemented StoreAccessBean as a Remote Bean, so it only exposes its remote interface. However, in accessing the Manager and Item beans, which are also used by these message-driven beans we can use their local interfaces as they are in the same JVM, and we have exposed their local interfaces.
Tasks :Create a MD bean named RequestItems under package au.com.tusc.mdb.
Create an Immutable Value Object named RequestItem under package au.com.tusc.mdb. Add attributes and implement their accessor and mutator methods. The attributes are:
private String username
private String passwd
private String itemID
private int quantity
Implement the onMessage method.
Deploy the RequestItems Bean.
Create your test client named RequestMDBClient under package au.com.tusc.mdb.
Add a method named testMDBBean with the following signature and implement it.
public void testMDBBean
Run your client and test the bean.
Go To Package Explorer > Expand Mystore (project) node > select src, right click and a menu will pop up.
On the pop up menu > New > Lomboz EJB Creation Wizard.
Enter package name au.com.tusc.mdb, bean name RequestItems and select bean type as Message Drive Bean (Queue) as shown below.
Press Finish.
This will create a package named au.com.tusc.mdb under src and RequestItemsBean within that package as shown below.
Note : Message-driven beans listen for messages from a JMS Producer, which gets information from a producer (perhaps another bean) and transfers it to the relevant Consumer bean. Since it is only responsible for processing such messages, it doesn't need any helper classes such as Remote and RemoteHome interfaces, Util classes, DAO class, etc. like our previous types of beans. The only helper classes we have to create are immutable value objects, which will be responsible for holding the information extracted from messages and then transferred to the bean(s).
Let's examine what methods and tags are generated by the EJB creation wizard..
It creates one @ejb.bean tag which assigns the name, transaction type, destination type and some other properties as shown below.
Unlike our previous beans it has a setMessageContext method for setting the context.
It has ejbCreate and ejbRemove methods like the other types of beans, as shown below.
It has a new method named onMessage which is the one of significance to us, where all the business logic will be written (as shown below).
Once a message is received from the JMS producer as a Message object, its data is extracted and filled into the immutable value object and then transferred to the relevant bean. This is covered later on in this chapter.
Now, before we add any functionality, create a class or immutable value object for extracting information from the message.
Create Immutable Value Object RequestItem :Go to src > under package au.com.tusc.mdb > New >Class
本文地址:http://com.8s8s.com/it/it9684.htm