一个简单的JMS实例(activeMQ)
来源:互联网 发布:林俊杰不是中国人知乎 编辑:程序博客网 时间:2024/05/29 09:28
选择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(); } } }}
开始测试:
启动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!
0 0
- 一个简单的JMS实例(activeMQ)
- JMS学习二(简单的ActiveMQ实例)
- JMS学习二(简单的ActiveMQ实例)
- JMS(二)--ActiveMQ简单的HelloWorld实例
- JMS(三)--ActiveMQ简单的HelloWorld实例
- ActiveMQ JMS Spring的一个实例
- ActiveMQ JMS Spring的一个实例
- ActiveMQ JMS Spring的一个实例
- 【Java.JMS】一个简单的JMS实例
- 【Java.JMS】一个简单的JMS实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 3、深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- JMS学习(3):--ActiveMQ简单的HelloWorld实例
- Android 广播接收者的生命周期
- log4j2整合activemq-client开发mq
- GIT开源安卓项目Top100
- 数组学习的几个案例
- css表格各行颜色交替 tr:nth-child(odd)
- 一个简单的JMS实例(activeMQ)
- ubuntu 登录 界面 卡死 密码
- 最短路径—Dijkstra算法和Floyd算法
- clearfix的用法
- 持续集成之Java项目转变成Maven项目
- Vijos P1347 乘积最大
- IOS下的应用唤起实现原理
- 芒果iOS开发之This application is modifying the autolayout engine from a background thread崩溃
- 螺旋矩阵