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

选择ActiveMQ。

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.gof</groupId>
  <artifactId>jms-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>jms-test</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Add by WXB -->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-client</artifactId>
      <version>5.10.0</version>
    </dependency>
    
  </dependencies>
</project>

创建测试类:

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();
    		}
    	}
    }
}<span style="font-family:SimSun;font-size:10px;">
</span>


开始测试:

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

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


分步运行程序,当调用:

producer.send(message);

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

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

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

当调用:

consumer.receive();

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


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

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







你可能感兴趣的:(java,jms)