【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:


分步运行程序,当调用:

producer.send(message);

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

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


当调用:

consumer.receive();

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


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

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







  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值