Use a message to drive Beans

xiaoxiao2021-03-06  44

EJB 2.0 Message Drive Beans Develop Message Drivers Beans and JMS This time we want to discuss this is EJB2.0 message driver Beans. I will combine JMS knowledge to demonstrate a specific example. All code can run on the EJB service support message Beans (EJB2.0 container); you also need a message server that supports JMS to control the message queue. This article assumes you understand the knowledge of enterprise JavaBeans. In this article we will create a mail message queue system. This system includes the following sections: JMS Servser: A JMS server, which will save the message queue of our mail system. JMS Queue: JMS queue, which will save JMS messages from the client. In our example, this message is a map message (MapMessage), which allows us to store the "key / value" information about the sent mail. Email Message Client: An Email message client, which will create a JMS message and send the message to the JMS queue. This message contains mail information to be sent. Email Message Drive Bean: An Email Message Drive Bean is responsible for receiving JMS mapping messages and sending it out. http://www.matrix.org.cn/upload/Article/a200311495538.gif <; / img> JMS structure before we tell the message to drive Beans, let us talk about JMS (Java Message Service) . We know that there are many news systems, each has their own API, which provides event exchange and data asynchronous services. As a programmer, I can send some information to the message server and continue to work without waiting for a response from the system. JMS API describes a standard approach to access almost all message systems, just like JDBC allows us to use the same API to access Oralce, Sybase, and SQL Server. As can be called asynchronous service, we have given additional benefits is that there is a loose coupled between the programs, and the code to send the request and the code of the response request are separated. Different clients send messages to the same designated destination; then, the recipient (Receiver) is separated from the destination and displayed. Let's quickly understand the basic concepts of some JMS API: Message Mappings The JMS Message Message Map Domain Message System has several running modes. The JMS API provides different domains, and he corresponds to different modes. One JMS Provider (Provider) one or more domains. Two, multiple public domains are point-to-peer and publish / contract. These two domains have the following concept: Destination: Object Client Specifies Message Send and Accept Destination Productuer: A Client Sends a message to a destination: a client accepts a message point-point point (PTP) from the destination Application has the following characteristics: a PTP provider is a sender a PTP consumer is a recipient a PTP target is a queue only by a recipient consumption / subscription (Pub / Sub) a Pub / SUB Applications include the following features: a PUB / SUB PRODUCER IS PUBLISHERA PUB / SUB CONSUER IS A SUBSCRIBERA PUB / SUB DESTINATION IS A TOPICA MESSAGE MULTIPLE SUBSCRIBERS A Pub / Sub Provider is a publisher a Pub / Sub consumer is a subscription A Pub / Sub goal is a topic a message can have multiple subscribers. For example, a mail communication system can use publish / subscription mode. Each person who is interested in mail communications can be an subscriber. After a new message is released, this message will be sent to each subscriber.

The example program describes our Email application that will use the PTP domain model. When an Eamil is placed in the queue, we only want one recipient to be received, and the other Email can be sent multiple times. The entry that the JMS message is placed in the queue is a JMS message. This is an ordinary message, he has a message head and a message. The JMS message has the following types: Type Description TextMessage text message. You can manipulate the TextMessageObjectage this message storage sequence object by msg.settext ("foo") and msg.getText methods. You can operate the message save key / value for information by msg.setObject (Object O) and Msg.getObject (). You can operate mapMessage through Msg.setString (Key, Value) and Msg.getstring (Key). He has several other Getter and Setter methods for basic Java types (for example, getBoolean, getInt, getObject, etc.) BYTESMESSAGE The message is a byte stream. He can be used to encapsulate existing message formats. StreetMessage This message allows you to send Java's original stream. A mapMessage will be used in our example because he provides a way to let us put the email's title information and content information in the message. EJB 2.0 Message Drive Bean We have already described the basic concept of JMS, now let's talk about the new concepts in the EJB2.0 specification. Review the JMS part of the previously speaking. We have a sender who places the message in the queue, then a recipor will read this message and use the received information to send email. This recipient can be a running program that receives the message in "Email Queue". In order to achieve this action, we need a recipient. This recipient's structure allows a parallel processing of a message stream, and it will handle transactions, which will enable us to handle commercial logic. This is the origin of the message bean. That is to say a message bean is a simple JMS consumer. A client cannot directly access the message beans (like you visiting the Status Bean) You can only send JMS messages to the destination listening to the message bean. In order to achieve the purpose of reuse, like other EJBs, many support information is deployed in the EJB deployment descriptor. This means that we don't have to care about we get the message (whether it is a queue or Topic), we only need to write an OnMessage (Message MSG) method to handle the message. We have already described JMS and MDB knowledge; now let us start our example. Development steps of Email Application: We will complete the Email application by following these steps: Install a "email queue" message on the JMS server . Create an Email client, he is responsible for sending a Java message to the Emial queue. Create a message driver bean, he will handle these messages, then use this information to send it with email. Write the deployment descriptor for the message drive. Package code. The code created is: Code Description com.customware.client.emailclienteMail client, he will send the message to the cassessor. The COM.CUSTOURE.EJB.EMAILMDB message driver bean will consume the JMS message from the client, and use the emailhelper to send mail. Com.customware.util.emailHelper A assistant class, he has a static method Sendmail (Map Mail, this method will send mail using JavaMail. Step 1: Installing a mail message queue This step will depend on your message server (such as IBM MQSeries, SonicMQ, etc.). We need to install a JMS queue.

I gave him the name of EmailQueue, the client and message driver bean deployment descriptor to use it. Step 2: Create an email client (emailclient.java) Now we need to create a client (JMS Sender). This client sees the search for Email, and then send it out. MAIN () method obtains parameters from the command line, create a HashTable (for storing MAP), calling the sendmail (MAP M) method. Sendmail method obtains information, creates a mapMessage based on the obtained information, and then sends the message to the EmailQueue queue via Sender.send (Message). The main work is in the constructor, these are all the contents of JMS work. The following is the content of the constructor: 1. Connect to the JNDI service via the GetInitialContext () assistant method. 2. Find a connection factory [QueueConnectionFactory] 3. Create a queue connection for our JMS server [Confactory.createQueUEConnection ()] 4. Create a JMS session (session), this session Production and consumption information. [Connection.createQueESession] 5. Find queues, he will send messages [(Queue) CTX.lookup (Queue_Name)] 6. Finally create a sender, this sender will use a session (we created earlier The send message to the queue we look for. Step 3: Create a message bean (emailmdb.java) When writing a session bean or entity bean, you must create a remote interface, a primary interface, and a bean class (entity Bean has an optional primary key class). The message bean only needs a bean class because there is a "client" he will use the interface of the bean. A message driver bean must extend two interfaces: Interface Description MessagelistenerJavax.jms.Messagelistener This is a JMS interface, and he provides an OnMessage (Message MSG) method. When a message is placed in the queue, the message-driven OnMessage method will be called, and the container will be consumed by the actual message. MessageDrivenBeanjavax.ejb.MessageDrivenBean This is the EJB interfaces, methods, he includes EJB life cycle: ejbCreate (): When the EJB container calls this method to create ejbRemove (): call setMessageDrivenContext (MessageDrivenContext ctx) when the container is destroyed EJB: when an object is After loading, EJBCREATE () calls before the context environment is transferred to EJB. Context contains information, saving this information and allows you to check, processing (getUsertractions (), setrollbackonly (), getRollbackOnly ()) security (getcallerprincipal () ()) If you look at the emailmdb.java code, you will find that several methods of the beginning have implemented the MessageDriveNbean interface. Everything we do in these methods is to print the information they call. SetMessageDrivenContext () Save the context environment into the instance variable so that we can find him in the future. It's almost these things you have to do. The final thing is to extend the OnMessage (Message MSG) method of the MessageListener interface. This is the process of our consumption message and handling them.

Start, we create a mapMessage by throwing messages. Then we look for the "Key /" value from the MAP message and put their value into the standard HashTable. Note that these methods We are called by mapMessage: // From MapNames = MapMessage.getMapNames (); // From MapMessage String Val = MapMessage.getstring (key); Finally, call emailhelper.sendmail (MAP) method, send the message as a mail. Is not it simple. This is the important part of the message-driven bean, we didn't write those embarrassing JMS code. In fact, how does the message driver bean know how to get these messages there? These we are implemented by deploying descriptors. The fourth: Define the deployment descriptor (EJB-JAR.XML) for MDB (MessageBeans), we want to tell the container (here is WebLogic) about MDB information. Use the annotated EJB deployment descriptor to install MDBS. So, we need to create a folder named Meta-INF to place these deployment files.

Meta-inf / ejb-jar.xml In the EJB-JAR file, we describe the name of the MDB class, the address type of the bean class, and security information. Here is this file; pay attention to class name and JMS address type (

Tag content according to

The specific creator described in the specified creators will change. If you are using a bea weblogic6.0sp1 you need to use

description)

Emailmdb com.customware.ejb.emailmdb Container javax.jms.queue system

Emailmdb com.customware.ejb.emailmdb Container javax.jms.queue system Emailmdb com.customware.ejb.emailmdb Container < / Transaction-Type> javax.jms.queue system

Emailmdb com.customware.ejb.emailmdb Container javax.jms.queue system we are What is the name of the container queue? This is to be placed in a specific document of the vendor. For example, if you are deployed on the BEA WebLogic 6.0 you need a weblogic-ejb-jar.xml file, this file should be like this:

META-INF / WebLogic-Ejb-jar.xml

Emailmdb 200 5 emailueue jms / emailmdb < WebLogic-Enterprise-Bean> emailmdb 200 5 emailQueue jms / emailmdb describes you in ejb-jar.xml The bean name defined in it. Then we can define the pool information. In this example we will have a minimum of 5 up to 200 MDBS instances. This will allow us to send 200 concurrent messages to the queue. The description tells the container that we will use the emailQueue to find the destination address. Because we didn't write it in code, if we changed, we only need to modify the deployment description file and then redeploy it.

The name of the description corresponds to the bean name you defined in EJB-JAR.XML. Then we can define the pool information. In this example we will have a minimum of 5 up to 200 MDBS instances. This will allow us to send 200 concurrent messages to the queue.

The description tells the container that we will use the emailQueue to find the destination address. Because we didn't write it in code, if we changed, we only need to modify the deployment description file and then redeploy it.

Step 5: Package code:

Now we have completed the code and deployment description file, we need to pack them and deploy them to the EJB server. The directory structure of these files is probably as shown below:

The example file downloads the examples of this article.

The compiled class files should be made in the client, EJB and Util directories, such as: /client/emailclient.class, ../ejb/emailmdb.class, and ../util/emailhelper.class. Now we have deployed Combine package code: ../ code% JAR CVF emailmdb.jar com meta-infrest We have an Email MDB JAR file, we deploy it to the EJB server. For testing, after deploying Bean, running a client You should see an EJB server sends an email. If all this is running, you need to make sure that JavaMail API Mail.jar is included in the ClassPath of the EJB server. in conclusion

We have created a message-driven bean, you can see how simple it is in JMS as a consumer. Message Driver Bean is a good supplement to the EJB component structure. He provides developers with a way to create consumers to gain it, support transaction processing and use the architecture of the container.

Translator: If you are using the code that WebLogic has the above code is not enough, you have to set a Destination. Set in the JMS in WebLogic's Console. If you don't understand, please contact me: mail : WAFD2003@yahoo.com.cn QQ: 282099538 (please indicate you from matrix :-))

Original: http://www.onjava.com/pub/a/onjava/2001/05/22/EJB_MSG.HTML? Page = 1

转载请注明原文地址:https://www.9cbs.com/read-51823.html

New Post(0)