ActiveMQ入门

xiaoxiao2021-02-28  72

1. 新建maven工程 2. 引入jar包,本来是下载5.15.0,运行的时候,显示和JDK版本不一致,就版ActiveMQ的版本改的低一点

<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.11.1</version> </dependency> 3. 编写消息生产者

package com.nineclient.activemq; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import com.nineclient.util.ConnFactory; public class JMSProducer { //发送的消息数量 private static final int SENDNUM = 10; public static void main(String[] args) { //连接 Connection connection = null; //会话,接受或者发送消息的线程 Session session = null; //消息的目的地 Destination destination = null; //消息的生产者 MessageProducer messageProducer = null; try { //通过连接工厂获取连接 connection = ConnFactory.getConnectionFactory().createConnection(); //启动连接 connection.start(); //创建session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); //创建一个名称为HelloWorld消息队列 destination = session.createQueue("HelloWorld"); //创建生产者 messageProducer = session.createProducer(destination); //发送消息 sendMessage(session,messageProducer); session.commit(); } catch (JMSException e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } public static void sendMessage(Session session, MessageProducer messageProducer) throws JMSException { for (int i=0; i<JMSProducer.SENDNUM; i++) { //创建一条文本消息 TextMessage message = session.createTextMessage("ActiveMQ send message " + i); System.out.println("发送消息:ActiveMQ 发送消息 " + i); //通过消息生产者发出 messageProducer.send(message); } } } package com.nineclient.util; import javax.jms.ConnectionFactory; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class ConnFactory { //默认连接用户名 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; //默认连接密码 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; //默认连接地址 private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL; private static ConnectionFactory connectionFactory = null; private ConnFactory () { } public static synchronized ConnectionFactory getConnectionFactory(){ if(connectionFactory == null) { connectionFactory = new ActiveMQConnectionFactory(ConnFactory.USERNAME,ConnFactory.PASSWORD,ConnFactory.BROKERURL); } return connectionFactory; } } 4. 编写消息消费者

package com.nineclient.activemq; 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 JMSConsumer { //默认连接用户名 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; //默认连接密码 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; //默认连接地址 private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) { //连接工厂 ConnectionFactory connectionFactory = null; //连接 Connection connection = null; //会话 接受或者发送消息的线程 Session session = null; //消息的目的地 Destination destination = null; //消息的消费者 MessageConsumer messageConsumer; //实例化连接工厂 connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME,JMSConsumer.PASSWORD,JMSConsumer.BROKERURL); 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()); } } } catch (JMSException e) { e.printStackTrace(); } } } 运行测试 先启动ActiveMQ,双击bin目录下的activemq.bat即可,在浏览器里面输入http://localhost:8161/admin/,用户名和密码 admin/admin

运行生产者

运行消费者

至此,实例就结束了

转载请注明原文地址: https://www.6miu.com/read-39504.html

最新回复(0)