ActiveMQ使用教程

一、下载 apache-activemq-5.13.2-bin.zip后解压,运行apache-activemq-5.13.2\bin\win64路径下的activemq.bat

运行成功如下

注:运行失败的话可以把apache-activemq-5.13.2\conf下的activemq.xml文件0.0.0.0:61616改成127.0.0.1:61616

        <transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://127.0.0.1:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://127.0.0.1:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://127.0.0.1:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://127.0.0.1:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://127.0.0.1:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

二、访问http://localhost:8161/admin/queues.jsp用户名密码默认都是admin

三、使用Maven导入相关依赖

    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-all</artifactId>
      <version>5.15.4</version>
    </dependency>

注:不同版本jdk对应不同的MQ,具体如下

MQ版本号Build-Jdk依赖JDK
apache-activemq-5.0.01.5.0_121.5+
apache-activemq-5.1.01.5.0_121.5+
apache-activemq-5.2.01.5.0_151.5+
apache-activemq-5.3.01.5.0_171.5+
apache-activemq-5.4.01.5.0_191.5+
apache-activemq-5.5.01.6.0_231.6+
apache-activemq-5.6.01.6.0_261.6+
apache-activemq-5.7.01.6.0_331.6+
apache-activemq-5.8.01.6.0_371.6+
apache-activemq-5.9.01.6.0_511.6+
apache-activemq-5.10.01.7.0_12-ea1.7+
apache-activemq-5.11.01.7.0_601.7+
apache-activemq-5.12.01.7.0_801.7+
apache-activemq-5.13.01.7.0_801.7+
apache-activemq-5.14.01.7.0_801.7+
apache-activemq-5.15.01.8.0_1121.8+

四、Queue队列模式

(1)创建生产者

package springboot.activeMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Producer {
	private static final String url="tcp://127.0.0.1:61616";//服务地址,端口默认61616
	private static final String queueName="queue-test";//要创建的消息名称
	public static void main(String[] args) throws JMSException {
		//1.创建ConnectiongFactory,绑定地址
		ConnectionFactory factory=new ActiveMQConnectionFactory(url);
		//2.创建Connection
		Connection connection= factory.createConnection();
		//3.启动连接
		connection.start();
		//4.创建会话
		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//5.创建一个目标
		Destination destination=session.createQueue(queueName);
		//6.创建一个生产者
		MessageProducer producer=session.createProducer(destination);
		for (int i = 0; i < 20; i++) {
			//7.创建消息
			TextMessage textMessage=session.createTextMessage("我是消息生产者:"+i);
			//8.发送消息
			producer.send(textMessage);
			System.out.println("发送消息:"+i);
		}
		connection.close();
	}
}

运行结果

(2)创建消费者

package springboot.activeMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Consumer {
	private static final String url="tcp://127.0.0.1:61616";//端口默认
	private static final String queueName="queue-test";//要消费的消息名称
	public static void main(String[] args) throws JMSException {
		//1.创建ConnectiongFactory,绑定地址
		ConnectionFactory factory=new ActiveMQConnectionFactory(url);
		//2.创建Connection
		Connection connection= factory.createConnection();
		//3.启动连接
		connection.start();
		//4.创建会话
		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//5.创建一个目标
		Destination destination=session.createQueue(queueName);
		//6.创建一个消费者
		MessageConsumer consumer=session.createConsumer(destination);
		//7.创建一个监听器
		consumer.setMessageListener(new MessageListener() {
			public void onMessage(Message arg0) {
				TextMessage textMessage=(TextMessage)arg0;
				try {
					System.out.println("接收消息:"+textMessage.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});
	}
}

运行结果

再启动一次消费者

运行一次生产者,此时两个消费者分别显示

五、Topic主题模式

(1)创建生产者

package springboot.activeMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class TipicProducer {
	private static final String url="tcp://127.0.0.1:61616";//服务地址,端口默认61616
	private static final String topicName="topic-test";//要创建的消息名称
	public static void main(String[] args) throws JMSException {
		//1.创建ConnectiongFactory,绑定地址
		ConnectionFactory factory=new ActiveMQConnectionFactory(url);
		//2.创建Connection
		Connection connection= factory.createConnection();
		//3.启动连接
		connection.start();
		//4.创建会话
		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//5.创建一个目标
		Destination destination=session.createTopic(topicName);
		//6.创建一个生产者
		MessageProducer producer=session.createProducer(destination);
		for (int i = 0; i < 15; i++) {
			//7.创建消息
			TextMessage textMessage=session.createTextMessage("我是消息生产者:"+i);
			//8.发送消息
			producer.send(textMessage);
			System.out.println("发送消息:"+i);
		}
		connection.close();
	}
}

运行结果

(2)创建消费者

package springboot.activeMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class TipicConsumer {
	private static final String url="tcp://127.0.0.1:61616";//端口默认
	private static final String topicName="topic-test";//要消费的消息名称
	public static void main(String[] args) throws JMSException {
		//1.创建ConnectiongFactory,绑定地址
		ConnectionFactory factory=new ActiveMQConnectionFactory(url);
		//2.创建Connection
		Connection connection= factory.createConnection();
		//3.启动连接
		connection.start();
		//4.创建会话
		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//5.创建一个目标
		Destination destination=session.createTopic(topicName);
		//6.创建一个消费者
		MessageConsumer consumer=session.createConsumer(destination);
		//7.创建一个监听器
		consumer.setMessageListener(new MessageListener() {
			public void onMessage(Message arg0) {
				TextMessage textMessage=(TextMessage)arg0;
				try {
					System.out.println("接收消息:"+textMessage.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});
	}
}

运行结果

先运行消费者再运行生产者,消费者才能接收到

再次运行生产者后

再一次启动消费者后启动生产者,此时两个消费者接收的消息一样


六、总结

(1)队列模式的消费者在运行后也能收到之前的消息,不过属于轮流进行消费,每个消费者接收不到完整的消息

(2)主题模式的消费者在运行后只能收到之后的消息,运行之前生产的消息接收不到;并且每个消费者都能接收全部的消息

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页