【Java.JMS】一个简单的JMS实例

选择ActiveMQ。

建立一个简单的Maven工程,pom.xml如下:


  4.0.0

  com.gof
  jms-test
  0.0.1-SNAPSHOT
  jar

  jms-test
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
    
    
    
      org.apache.activemq
      activemq-client
      5.10.0
    
    
  


创建测试类:

package com.gof.jms.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class SimpleMessageSendandReceiveApp {
	public static final String user = "system";
	public static final String password = "manager";
	public static final String url = "tcp://localhost:61616";
	public static final String queueName = "test_queue";
	public static final String messageBody = "Hello JMS!";
	public static final boolean transacted = false;
	public static final boolean persistent = false;
	
    public static void main(String[] args){
    	Connection connection = null;
    	Session session = null;
    	
    	try{
    		// create the connection
    	    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
            connection = connectionFactory.createConnection();
            connection.start();
            
            // create the session
            session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(queueName);
            
            // create the producer
            MessageProducer producer = session.createProducer(destination);
            if (persistent){
            	producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            }else{
            	producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            }
            
            // create text message
            Message message = session.createTextMessage(messageBody);
            
            // send the message
            producer.send(message);
            System.out.println("Send message: " + ((TextMessage)message).getText());
            
            // create the consumer
            MessageConsumer consumer = session.createConsumer(destination);
            // blocking till receive the message
            Message recvMessage = consumer.receive();
            System.out.println("Receive message: " + ((TextMessage)recvMessage).getText());
            
    	}catch (Exception e){
    		e.printStackTrace();
    	}finally{
    		try{
    			// close session and connection
    		    if (session != null){
    			    session.close();
    		    }
    		    if (connection != null){
    			    connection.close();
    		    }
    		}catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    }
}


开始测试:

启动ActiveMQ,在admin页面(http://localhost:8161/admin)中创建名为test_queue的Queue:

【Java.JMS】一个简单的JMS实例_第1张图片


分步运行程序,当调用:

producer.send(message);

可以看到ActiveMQ接收到了这个消息:

【Java.JMS】一个简单的JMS实例_第2张图片

【Java.JMS】一个简单的JMS实例_第3张图片

同时也可以看到该Queue当前的Active Producer:

【Java.JMS】一个简单的JMS实例_第4张图片

当调用:

consumer.receive();

可以看到该消息被消费掉:

【Java.JMS】一个简单的JMS实例_第5张图片


在控制台中,可以看到输出的结果:

Send message: Hello JMS!
Receive message: Hello JMS!







你可能感兴趣的:(Java.JMS)