<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版本一致。java
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条消息。当发送后,控制条会打印以下信息:apache
能够登陆http://127.0.0.1:8161/admin/queues.jsp查看你的消息队列:session
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消息队列中的消息,运行后控制台输出:app
再一次登陆http://127.0.0.1:8161/admin/queues.jsp查看你的消息队列:jsp
上面的例子就是点对点的消息模型发送同步消息。maven