EJB3.0 Development Guide: Message Drive Bean

xiaoxiao2021-03-06  48

The service interface of the message driver bean has been defined. It is the MessageListener interface for using JMS. This interface defines the OnMessage method.

Implementation classes must use MessageDriven notes. SetMessageDrivenContext and EJBREMOVE methods can be implemented.

In jboss, the JNDI name of the queue of the message is specified by ConnectionConfig.

@ConnectionConfig (destinationType = javax.jms.queue.class, destinationjndiname = "queue / kuaffejb3 / sample", durable = true, subscriptionid = "kuaffMessage")

The example of the example provided herein is imported in Eclipse.

This example sends a TextMessage from the client. After the BEAN component receives this message, the message will be output to the console.

This example has main five files:

Messager.java: Business components.

Client.java: Test EJB client class.

JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.

Build.xml: Ant profile, to compile, release, test, and clear EJB.

Queue-Example-Service.xml: Message Service is used to test messages.

Here is a description of the content of each file.

Messager.java

package com.kuaff.ejb3.messager; import org.jboss.ejb3.mdb.ConnectionConfig; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.TextMessage; import javax .jms.MessageListener; @MessageDriven @ConnectionConfig (destinationType = javax.jms.Queue.class, destinationJndiName = "queue / kuaffejb3 / sample", durable = true, subscriptionId = "kuaffMessage") public class Messager implements MessageListener {public void onMessage ( Message recmsg) {system.out.println ("received message:"); try {textMessage message = (textMessage) Recvmsg; system.out.println (message.getText ());} catch (jmsexception e) {E .printstacktrace ();}}}

This message bean is very simple. If you don't do it after receiving the message, it is to output the message content to the console.

Client.java

package com.kuaff.ejb3.messager; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.TextMessage ; import javax.naming.InitialContext; public class Client {public static void main (String [] args) throws Exception {QueueConnection cnn = null; QueueSender sender = null; QueueSession session = null; InitialContext ctx = new InitialContext (); Queue queue = (Queue) ctx.lookup ( "queue / kuaffejb3 / sample"); QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup ( "ConnectionFactory"); cnn = factory.createQueueConnection (); session = cnn.createQueueSession (false, QueueSession. Auto_acknowledge); TextMessage Msg = session.createtextMessage ("Jianghu Express: Yushu Linfeng Fengli's bonchi appeared again."); Sender = session.createender (queue); sender.send (MSG); system.out.println ("Message has been issued"); }} This client will send a text message to the queue.

Queue-esample-service.xml

queue / kuaffejb3 / sample jboss.mq:service=DestinationManager < / server>

Configure the message service required for this program.

Run {$ jboss_home} / bin's run.bat: run -c all, start JBoss.

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

New Post(0)