Jboss-eap-5.1 Messaging

This section I will concertate on the difference of messaging mechenism with old platform Jboss.

1. Queue Test

      Firstly, deploy a Queue on EAP-5.1 Jboss, create 'homeTest-service.xml' file under $JBOSS_HOME\server\default\deploy\messaging, and the 'homeTest-service.xml' contain text as following:

<mbean code="org.jboss.jms.server.destination.QueueService"
      name="jboss.messaging.destination:service=Queue,name=testQueue"
      xmbean-dd="xmdesc/Queue-xmbean.xml">
      <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
      <depends>jboss.messaging:service=PostOffice</depends>
      <attribute name="SecurityConfig">
         <security>
            <role name="guest" read="true" write="true"/>
            <role name="publisher" read="true" write="true" create="false"/>
            <role name="noacc" read="false" write="false" create="false"/>
         </security>
      </attribute>
   </mbean>

 this is testQueue MBean definition descrption, and then creates a Java Message Service connection to a JBoss Messaging instance and uses it to create a session and a message producer, which sends a message to the queue/testQueue, then uses that connection to create a consumer that reads a single message from the queue. This test  is considered successful if the message consumer receives, without error, the message that was sent by the producer, And Output the send text message.

The Test Code as Following:

private void run() {
		
		String destinationName = JMSUtil.getDestTestQueue();
		
		InitialContext ic = null;
		ConnectionFactory cf = null;
		Connection connection = null;
		
		try {
			ic = JMSUtil.getInitialContext(true, "127.0.0.1");
			cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
			Queue queue = (Queue)ic.lookup(destinationName);
			JMSUtil.log("Queue " + destinationName + " exists");
			
			connection = cf.createConnection();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	        MessageProducer sender = session.createProducer(queue);
	        
	        TextMessage message = session.createTextMessage("Kylin Soong");
	        sender.send(message);
	        JMSUtil.log("The message was successfully sent to the " + queue.getQueueName() + " queue");
		
	        MessageConsumer consumer =  session.createConsumer(queue);
	        connection.start();
	        message = (TextMessage)consumer.receive(2000);
	        JMSUtil.log("Recieve Msg: " + message.getText());
	        
	        JMSUtil.displayProviderInfo(connection.getMetaData());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

 The Code is very simple, howerver, to make this code work spent bunchs of work, that's because new Version Jboss split Jbossall-client.jar into more little piece, In old version we only add Jbossall-client.jar on jar to classpath, and now we must add the following jar:


Jboss-eap-5.1 Messaging
 After add this complete, the above code working, and the output as following:

2011-08-02 21:09:10 [INFO] Queue /queue/testQueue exists
2011-08-02 21:09:11 [INFO] The message was successfully sent to the testQueue queue
2011-08-02 21:09:11 [INFO] Recieve Msg: Kylin Soong

 2. Topic Test

       Just like the testQueue Test, topic test almost do the same thing as queue test, this test creates a Java Message Service (JMS) connection to a JBoss Messaging instance and uses it to create a session, a publisher, and a subscriber. The publisher sends a message to the topic. The test is considered successful if the subscriber receives the message.  Also like queue test text message has been sent by publisher, the subscriber recieves the message and extract the text and output though log mechanism, The same Operation, before Code test we should deploy testTopic firstly, also like deploy testQueue, this time only need to add Topic referenced mbean to 'homeTest-service.xml', the mbean contents as following:

<mbean code="org.jboss.jms.server.destination.TopicService"
      name="jboss.messaging.destination:service=Topic,name=testTopic"
      xmbean-dd="xmdesc/Topic-xmbean.xml">
      <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
      <depends>jboss.messaging:service=PostOffice</depends>
      <attribute name="SecurityConfig">
         <security>
            <role name="guest" read="true" write="true"/>
            <role name="publisher" read="true" write="true" create="false"/>
            <role name="durpublisher" read="true" write="true" create="true"/>
         </security>
      </attribute>
   </mbean>

 As usual, Topic Test Code:

private void run() {
		String destinationName = JMSUtil.getDestTestTopic();

		InitialContext ic = null;
		Connection connection = null;
		
		try {
			ic = JMSUtil.getInitialContext(true, "127.0.0.1");
			ConnectionFactory cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
			Topic topic = (Topic) ic.lookup(destinationName);
			JMSUtil.log("Topic " + destinationName + " exists");
			
			connection = cf.createConnection();
	        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	        MessageProducer publisher = session.createProducer(topic);
	        MessageConsumer subscriber = session.createConsumer(topic);
	        
	        TopicTestListener messageListener = new TopicTestListener();
	        subscriber.setMessageListener(messageListener);
	        connection.start();
	         
	        TextMessage message = session.createTextMessage("Kylin Soong");
	        publisher.send(message);
	        JMSUtil.log("The message was successfully published on the topic");
	         
	        messageListener.waitForMessage();
	         
	        message = (TextMessage)messageListener.getMessage();
	        JMSUtil.log("Received message: " + message.getText());
	        
	        JMSUtil.displayProviderInfo(connection.getMetaData());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 This time is more easier to make topic test code work, the out is as following:

2011-08-02 22:15:25 [INFO] Topic /topic/testTopic exists
2011-08-02 22:15:26 [INFO] The message was successfully published on the topic
2011-08-02 22:15:26 [INFO] Received message: Kylin Soong

 

你可能感兴趣的:(jboss)