目录
一、简介
Apache ActiveMQ™是最流行的开源,多协议,基于Java的消息传递服务器。它支持行业标准协议,因此用户可以通过广泛的语言和平台从客户选择中受益。可以使用C,C ++,Python,.Net等进行连接。使用无处不在的AMQP协议集成您的多平台应用程序。
二、相关解释
Provide(MessageProvide):消息生产者。
Consumer(MessageConsumer):消息消费者。
PTP:Point to Point,即点对点服务。
Pub/Sub:Publish/Subscribe,即发布/订阅的消息类型。
Queue:队列目标。
Topic:主题目标。
ConnectionFactory:连接工厂,JMS用它创建连接。
Connection:JMS客服端到JMS Provider的连接。
Destination:消息的目的地。
Session:会话,一个发送或接受消息的线程。
三、消息订阅类型
1、点对点(point to point):消息生产者和消息消费是一对一的关系。
2、发布订阅(Publish/Subcribe)
四、环境搭建
- 下载安装包(ActvieMQ下载路径)
- 解压。tar -zxvf apache-activemq-5.15.12-bin.tar.gz。
- 进入到bin目录下。
- 启动命令:./activemq start
- 关闭命令:./activemq stop
- 重启命令:./activemq restart
- 查看状态:./activemq status
五、控制台访问
默认端口:8161。控制台访问路径:部署服务器IP:8161。控制台默认用户名:admin 密码:adimin
六、Java整合
1、pom依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.12</version>
</dependency>
2、生产者
package com.michael;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Provider {
private static final String CONNECTION_URL = "tcp://192.168.125.119:61616";
private static final String QUEUE_NAME = "testQueue";
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTION_URL);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE_NAME);
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage();
message.setText("Hello World");
producer.send(message);
session.close();
connection.close();
}
}
3、消费者
package com.michael;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.io.IOException;
public class Consumer {
private static final String CONNECTION_URL = "tcp://192.168.125.119:61616";
private static final String QUEUE_NAME = "testQueue";
public static void main(String[] args) throws JMSException, IOException {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTION_URL);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE_NAME);
MessageConsumer consumer = session.createConsumer(queue);
// 1、轮询模式
// while (true) {
// TextMessage message = (TextMessage) consumer.receive();
// System.out.println(message.getText());
// }
// 2、监听模式
consumer.setMessageListener(message -> {
try {
System.out.println(((TextMessage)message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
});
System.in.read();
}
}
七、Spring集成
1、pom依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.12</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.12</version>
</dependency>
2、spring配置文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.michael"/>
<!-- connection工厂 -->
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://192.168.125.119:61616</value>
</property>
</bean>
</property>
</bean>
<!-- 消息工具类 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean>
<!-- 消息目的地 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg name="name" value="spring-queue"/>
</bean>
<!-- 消息监听器 -->
<bean id="messageListener" class="com.michael.SpringMessageListener"/>
<!-- 监听器容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="messageListener" ref="messageListener"/>
<property name="destination" ref="destination"/>
</bean>
</beans>
3、生产者
package com.michael;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
@Service
public class SpringProducer {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private ActiveMQQueue mqQueue;
public void sendMsg() {
jmsTemplate.send(mqQueue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("Hello spring, active");
}
});
}
}
八、springboot整合
1、pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2、applicationContext.properties
spring.activemq.broker-url=tcp://192.168.125.119:61616
spring.activemq.user=admin
spring.activemq.password=admin
3、生产者
package com.michael;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
@Service
public class Producer {
private final JmsTemplate JMS_TEMPLATE;
public Producer(JmsTemplate jms_template) {
JMS_TEMPLATE = jms_template;
}
public void send() {
JMS_TEMPLATE.send("springboot_queue", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("Hello SpringBoot");
}
});
}
}
4、消费者(监听器)
package com.michael;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
@Service
public class Consumer {
@JmsListener(destination = "springboot_queue")
public void onMessage(Message message) throws JMSException {
if (message instanceof TextMessage) {
System.out.println(((TextMessage) message).getText());
}
}
}