Jboss EJB MDB

Source: Internet
Author: User
Tags log log log message queue client jboss jboss server log4j





JMS is not very familiar with, after a few days finally have a point of thought. Here are two programs, add a queue-example-service.xml configuration file is very simple, complex point in the future said.











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 (propertyname= "Destination", propertyvalue= "Queue/tutorial/example")



})



A destination is the "object on" JBOSSMQ server that clients



Use to send and receive messages. There are two 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 ("----------------");



}



}







The English comment is the one I found in the JBoss 4 Application Server Guide to know that destination is responsible for sending and receiving messages (message) in the JBoss server. Destination is divided into two different ways according to how the message is published: Queues and topics.



The topic release allows One-to-many, or many-to-many, communication channels, the message creator is called publisher, and the message recipient is called subscriber, so it is called a publish/subscribe (publish/subscribe).



Queue is another way to allow only one message to be sent to a customer. A sender places a message in a message queue, the recipient extracts from the queue and gets the message, and the message disappears in the queue. After the first recipient extracts and gets the message, the other person cannot get it. Also known as the point-to-point point.







About Activateconfig with Queue-example-service.xml, here as long as you know the destination on the line. From the program can see its interface for MessageListener can guess this program is responsible for listening to messages, after receiving the message to print. That's it:











Client.java



Package org.jboss.tutorial.mdb.client;







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");



Here the contents of lookup in Queue-example-service.xml have defined Jndi



Queueconnectionfactory factory = (queueconnectionfactory) ctx.lookup ("ConnectionFactory");



CNN = Factory.createqueueconnection ();



Session = Cnn.createqueuesession (false,//does not require a transaction



Queuesession.auto_acknowledge);//automatically receive messages







TextMessage msg = session.createtextmessage ("Hello World");







Sender = Session.createsender (queue);



Sender.send (msg);



SYSTEM.OUT.PRINTLN ("message sent successfully to remote queue.");



}



}







The order in which the client is executed here is Queueconnectionfactoryàqueueconnectionàqueuesessionàqueuesender



If that's not clear, you'll need to cram for JMS first. Oh, I also like this.



The client's task is to send a message, which is then received by the server.















Queue-example-service.xml



<?xml version= "1.0" encoding= "UTF-8"?>



<server>



<mbean code= "Org.jboss.mq.server.jmx.Queue"



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



<attribute name= "Jndiname" >queue/tutorial/example</attribute>



<depends optional-attribute-name= "Destinationmanager" >jboss.mq:service=DestinationManager</depends>



</mbean>



</server>







The first thing I feel is that this XML is related to JMX, and the Mbean represents the Manage bean, which is not a big problem.



The role of this XML is to instance a queue, the name is tutorial (can be changed), and then by jmx-console this console unified management, run this program can be http://localhost:8080/ Jmx-console/htmladaptor?action=inspectmbean&name=jboss.mq.destination%3aservice%3dqueue%2cname%3dtutorial (I hope you haven't come and changed the configuration above, I can't find a way to get you to the address of this queue faster, too much.) To see the queue on this profile, JBoss already has a couple of queue.



Then you define a Jndi,client.java and you can have a lookup.











Here attached log4j.properties in Jboss-ejb-3.0_preview_5.zip without this always shows a lack of appender. With this, a record.log log file will be generated 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















Running: Reference installing.html



Under Windows



Open command Prompt cmd, to Jboss_home/bin



Run.bat–c All



With Ant



Just build and run.



Look at 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 it's two programs, it took some time since I didn't know much about JMS before. Finding relevant information is important for understanding the above issues.



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



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



The JBoss 4 application Server Guide












Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.