Messagedrivenbean in ejb3.0:
1. Java Message-driven Bean is a Java Message Service (JMS ).
2. JMS is an API used to access the Developer Center of the enterprise message system. The Enterprise message system can assist application software in message interaction over the network.
3. the JMS programming process is summarized as: ApplicationProgramA --> (Message) --> JBoss JMS --> (Message) --> application B. Application A and application B are not directlyCodeThe two are decoupled. The center of the message passing system is the message.
Message-driven Bean (MDB) is a component specially designed to process message requests. It also uses the instance pool technology like stateless Session Bean, containers can use a certain number of bean instances to concurrently process hundreds of JMS messages. Because MDB can process a large number of concurrent messages, it is very suitable for some message gateway products. If a service is executed for a long time and the execution result does not need to be reported to the user in real time, MDB is also suitable for use. If the order is successful, send an email or text message to the user.
Messages are of the following types, all of which are derived from the message interface.
Streammessage: a message that contains the Java basic value stream. The filling and reading operations are performed in order.
Mapmessage: a message that contains a group of name-value pairs. (No entry order defined)
Textmessage: a message whose body contains a Java string (for example, an XML message)
Objectmessage: a message that contains serialized Java objects.
Bytesmessage: a message that contains continuous byte streams.
Message transmission model:
JMS supports two message transmission models: point-to-point (PTP) and publish/subscribe (pub/Sub ).
The two have the following differences:
1. The PTP message transmission model specifies that a message can only be transmitted to one receiver. It is represented by javax. JMS. queue.
2. the pub/sub message passing model allows one message to be transmitted to multiple receivers. It is represented by javax. JMS. Topic.
Note: Each model is implemented by extending the public base class. For example, javax. JMS. queue and javax. JMS. Topic are both extended from the javax. JMS. Destination class.
The development procedure is as follows:
1. Configure the destinations-service.xml file.
JBoss uses an XML file to configure the queue address, the file name format should follow *-service. XML, I named the destinations-service.xml.
JBoss is composed of global JNDI names by default.: "Queue" + "/" + "target address ".
Before starting JMS programming, We need to configure the destination address (destination) for the message to arrive, because only the destination address exists, we can send the message to this address. As each application server has different configuration methods for the target address, the following uses JBoss as an example to configure a target address of the queue type.
<? XML version = "1.0" encoding = "UTF-8"?>
<Server>
<Mbean code = "org. JBoss. MQ. server. JMX. queue"
Name ="JBoss. MQ. Destination: service = queue, name = ztfqueue">
<Attribute name = "jndiname">Queue/ztfqueue</Attribute>
<Depends optional-Attribute-
Name = "destinationmanager"> JBoss. MQ: service = destinationmanager </depends>
</Mbean>
<Mbean code = "org. JBoss. MQ. server. JMX. Topic"
Name ="JBoss. MQ. Destination: service = topic, name = ztftopic">
<Attribute name = "jndiname">Topic/ztftopic</Attribute>
<Depends optional-Attribute-
Name = "destinationmanager"> JBoss. MQ: service = destinationmanager </depends>
</Mbean>
</Server>
Note: before any queue or topic is deployed, the destination manager mbean must be deployed on the application server. All dependencies are declared through the <depends> node.
2. Send messages in the Java class (using JNDI. properties ).
To send a message, follow these steps:
1. Get a JNDI initialization context (context)
Initialcontext CTX = new initialcontext ();
2. find a connection factory connectionfactory according to the context. The connection factory is provided by JMS and does not need to be created by ourselves. Each Vendor binds a global JNDI for it, we get it through its Global JNDI;
Queueconnectionfactory factory = (queueconnectionfactory) CTX. Lookup("Connectionfactory ");
3. Get a connection queueconnection from the connection factory.
Queueconnection conn = factory. createqueueconnection ();
4. Establish a session through a connection );
Queuesession session = conn. createqueuesession (false, queuesession. auto_acknowledge );
The Code indicates that a session that does not need transactions and can automatically establish the received message.
5. Find the target address:
Destination = (destination) CTX. Lookup ("queue/ztfqueue ");
6. Create the messageproducer (both queuesender and topicpublisher are extended from the messageproducer Interface) based on the session and target address ):
Messageproducer producer = session. createproducer (destination );
Textmessage MSG = session. createtextmessage ("Hello, Jingzhou, this is my first message-driven Bean ");
Producer. Send (MSG );
3. Use messagedrivenbean to receive messages.
When the container detects that a message arrives at the target address waiting for the bean, the container calls the onmessage () method to pass the message as a parameter to the MDB. In onmessage (), MDB decides how to process and modify the message. You can use annotations to specify which Destination Address (destination) The MDB listens ). When MDB is deployed, the container reads the configuration information.
An MDB usually implements the messagelistener interface, which defines the onmessage () method. Bean uses it to process received JMS messages.
@ Messagedriven (activationconfig =
{
@ Activationconfigproperty
(Propertyname = "destinationtype", propertyvalue = "javax. JMS. queue "),
@ Activationconfigproperty
(Propertyname = "destination", propertyvalue = "queue/ztfqueue "),
@ Activationconfigproperty (propertyname = "acknowledgemode", propertyvalue = "auto-
Acknowledge ")
})
Public class messagedrivenbean implements messagelistener {
Public void onmessage (message ){
Textmessage MSG = (textmessage) message;
Try {
System. Out. println (msg. gettext ());
} Catch (jmsexception e ){
E. printstacktrace ();
}
}
}
4. compress the SRC file into a jar package (use build. XML) and deploy it in c: \ jboss-5.0.0.GA \ Server \ Default \ deploy.
5. Run the Java class for sending messages. The result is displayed on the console.
6. Now, a messagedrivenbean is successfully developed.