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 Recvmsg)
{
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.createqueuesis (false, quenessness.auto_acknowledge);
TextMessage msg = session.createtextMessage ("Jianghu Express: Yushu Linfeng's rumored son, the nest,");
Sender = session.createsender (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
XML Version = "1.0" encoding = "UTF-8"?>
Name = "jboss.mq.destination: service = queue, name = kuaffMessage"> mbean> server> Configure the message service required for this program. Please run the run.bat: Run? C all under the directory of {$ jboss_home} / bin, start JBoss. Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, perform Ant Ejbjar, publish this EJB. Execute Run Target in the ANT view of Eclipse. Or on the command line, enter this project directory, perform Ant Run, test this EJB.