JBoss EJB MDB

xiaoxiao2021-03-05  26

It is not very familiar with JMS and finally has a point of thinking for a few days. There are two programs here, adding a queue-example-service.xml configuration file very simple, complex point in the future.

Package org.jboss.tutorial.mdb.bean;

Import javax.ejb.MessageDriven;

Import javax.ejb.activationconfigproperty;

Import javax.jms.message;

Import javax.jms.MESSAGELISTENER;

@MessageDriven (ActivateConfig =

{

@ActiVationConfigProperty (PropertyName = "destinationType", PropertyValue = "javax.jms.queue"),

@ActiVationConfigProperty (property = "destination", PropertyValue = "Queue / Tutorial / Example")

})

// a destination is The Object on the jbossmq server That Clients

// use to send and receive messages. There Are Types of

// Destination Objects, Queues and Topics.

Public Class ExampleMDB IMPLEMENTS MessageListener

{

Public void onMessage (Message Recvmsg)

{

System.out.println ("--------------");

System.out.println ("Received Message");

System.out.println ("--------------");

}

}

English Note is I found in the JBoss 4 Application Server Guide, you can know that Destination is where the JBoss server is responsible for sending and receiving messages (Message). Destination is two species according to the message publishing method: Queues and Topics.

Topic releases allows a one-to-many, or more multi-communication channels, and the generator generator is called Publisher, and the message recipient is called Subscriber, which is called publish / subscribe.

Queue is another way, just allowing a message to be delivered to a customer. A sender places the message in the message queue, the recipient extracts and gets the message from the queue, and the message will disappear in the queue. The first recipient extracts and gets the message, others can't get it. Also known as a point-to-point (Point to Point).

About ActivateConfig is related to Queue-Example-Service.xml, just know Destination. From the program, you can see that its interface can guess this program is responsible for listening to the message and prints after receiving the message. That's it :)

Client.java

Package org.jboss.tutorial.mdb.cl;

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 / Tutorial / Example);

/ / The content of Lookup is defined JNDI in Queue-Example-Service.XML

QueueConnectionFactory Factory = (QueueConnectionFactory) CTX.lookup ("ConnectionFactory");

CNN = Factory.createqueueConnection ();

session = cnn.createqueuesis (false, // does not need transactions

Queuesession.Auto_acknowledge); / / Automatic receiving message

TextMessage Msg = session.createtextMessage ("Hello World");

Sender = session.createsender (queue);

Sender.send (MSG);

System.out.println ("Message Sent Successful To Remote Queue.");

}

}

The execution order here is queueConnectionFactoryà queueConnection à queuesession àqueuesender

If this is not clear, you must first take JMS. Oh, I am also like this.

The client task is to send a message and then receive by the server.

Queue-esample-service.xml

Name = "jboss.mq.destination: service = queue, name = tutorial">

Queue / Tutorial / Example

jboss.mq:service=DestinationManager

The first thing to give me is this XML and JMX, MBean represents Manage Bean, this problem is not big.

This XML's role is that instance queue, the name is TUTORAL (can be changed by himself), then managed by the JMX-Console console, run this program can be at http: // localhost: 8080 / jmx-console / htmladaptor? Action = InspectMbean & name = jboss.mq.destination% 3aservice% 3DQueue% 2cname% 3dtutorial (I hope that you haven't come yet and change the above configuration, I can't find the address that you can find this queue faster, too much.) Seeing the Queue on this profile, there have been several queue in JBoss. Then define a JNDI, Client.java can look l.

There is no log4j.properties in jboss-ejb-3.0_preview_5.zip, there is no such thing as the lack of appender. With this will generate a replard.log log file in this directory.

Log4j.properties

Log4j.Appender.r = org.apache.log4j.rollingfileappender

Log4j.Appender.r.file = record.log

Log4j.Appender.r.Layout = org.apache.log4j.patternlayout

Log4j.Appender.r.Layout.conversionPattern =% p% d {hh: mm: ss}% T% c {1} -% M% N

Log4j.Appender.r.maxbackupindex = 1

Log4j.Appender.r.maxfilesize = 100kb

Log4j.Appender.stdout.Layout = Org.apache.log4j.patternlayout

Log4j.Appender.stdout.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

Log4j.appender.stdout = org.apache.log4j.consoleAppender

Log4j.rootlogger = stdout, r

Run: Refer to Installing.html

Under Windows

Open the command prompt cmd to jboss_home / bin

Run.bat -c all

Use ANT

After Build, run.

See the JBoss window to see

01: 01: 20,828 info [stdout] ----------------

01: 01: 20,828 info [stdout] received message

01: 01: 20,828 info [stdout] ----------------

discuss:

Although two programs, I spent some time because I didn't know much about JMS. Find related information is important for understanding the above problems.

http://www.cn-java.com/target/news.php?news_id=2730

Http://blog.blogchina.com/category.225381.html

The Jboss 4 Application Server Guide

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

New Post(0)