Apache-ActiveMQ消息队列 — HelloWorld实例 https://my.oschina.net/u/2981366/blog/779250
pom.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<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>cn.net.bysoft</groupId> <artifactId>activemqapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.14.1</version> </dependency> </dependencies> </project> |
引用activemq.all.jar,最好你的类库的版本与你的activemq版本一致。
Producer(发送者)代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package cn.net.bysoft.activemqapp.test1; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * 消息的生产者,发送消息的一方 */ public class Producer { // 默认连接用户名 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; // 默认连接密码 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // 默认连接地址 private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL; // 发送的消息的次数 private static final int SENDNUM = 10; public static void main(String[] args) { // 连接工厂 ConnectionFactory connectionFactory; // 连接 Connection jms_connection = null; // 会话 接受或者发送消息的线程 Session jms_session; // 消息的目的地 Destination destination; // 消息生产者 MessageProducer messageProducer; // 实例化连接工厂 connectionFactory = new ActiveMQConnectionFactory(Producer.USERNAME, Producer.PASSWORD, Producer.BROKEURL); try { // 通过连接工厂获取连接 jms_connection = connectionFactory.createConnection(); // 开始连接 jms_connection.start(); // 创建session jms_session = jms_connection.createSession(true, Session.AUTO_ACKNOWLEDGE); // 创建一个名称为HelloWorld的消息队列 destination = jms_session.createQueue("HelloWorld"); // 创建消息生产者 messageProducer = jms_session.createProducer(destination); // 发送消息 sendMessage(jms_session, messageProducer); jms_session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (jms_connection != null) { try { jms_connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } /** * 发送消息 */ public static void sendMessage(Session session, MessageProducer messageProducer) throws Exception { for (int i = 0; i < Producer.SENDNUM; i++) { // 创建一条文本消息 TextMessage message = session.createTextMessage("ActiveMQ 发送消息" + i); System.out.println("发送消息:Activemq 发送消息" + i); // 通过消息生产者发出消息 messageProducer.send(message); } } } |
该段程序主要实现了连接到ActiveMQ,并创建了名叫HelloWorld的消息队列,并向该队列发送了10条消息。当发送后,控制条会打印如下信息:
可以登录http://127.0.0.1:8161/admin/queues.jsp查看你的消息队列:
Consumer(接收者)代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package cn.net.bysoft.activemqapp.test1; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * 消息的消费者,接收(使用)消息的一方 */ public class Consumer { private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;// 默认连接用户名 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;// 默认连接密码 private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;// 默认连接地址 public static void main(String[] args) { ConnectionFactory connectionFactory;// 连接工厂 Connection connection = null;// 连接 Session session;// 会话 接受或者发送消息的线程 Destination destination;// 消息的目的地 MessageConsumer messageConsumer;// 消息的消费者 // 实例化连接工厂 connectionFactory = new ActiveMQConnectionFactory(Consumer.USERNAME, Consumer.PASSWORD, Consumer.BROKEURL); try { // 通过连接工厂获取连接 connection = connectionFactory.createConnection(); // 启动连接 connection.start(); // 创建session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建一个连接HelloWorld的消息队列 destination = session.createQueue("HelloWorld"); // 创建消息消费者 messageConsumer = session.createConsumer(destination); while (true) { TextMessage textMessage = (TextMessage) messageConsumer.receive(100000); if (textMessage != null) { System.out.println("收到的消息:" + textMessage.getText()); } else { break; } } } catch (JMSException e) { e.printStackTrace(); } } } |
上面的代码为消费者使用ActiveMQ消息队列中的消息,运行后控制台输出:
再一次登录http://127.0.0.1:8161/admin/queues.jsp查看你的消息队列
上面的例子就是点对点的消息模型发送同步消息。
我只是来随便看看!