文章目录
ActiveMQ的broker
broker是什么
相当于一个ActiveMQ服务器实例。
说白了,Broker其实就是实现了用代码的形式启动ActiveMQ将MQ嵌入到Java代码中,以便随时用随时启动,在用的时候再去启动这样能节省了资源,也保证了可用性。
这种方式,我们实际开发中很少采用,因为他缺少太多了东西,如:日志,数据存储等等。
注意:AactiveMQ也可以像Redis一样,通过指定不同的配置文件,来启动不同的ActiveMQ实例。
嵌入式的broker启动
用ActiveMQ Broker作为独立的消息服务器来构建Java应用。
ActiveMQ也支持在vm中通信基于嵌入的broker,能够无缝的集成其他java应用。
类比于SpringBoot中集成的tomcat,这个嵌入式的ActiveMQ Broker不是我们安装的ActiveMQ 。
引入依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
</dependency>
示例代码:
public class EmbedBroker {
public static void main(String[] args) throws Exception {
//ActiveMQ也支持在vm中通信基于嵌入的broker
BrokerService brokerService = new BrokerService();
brokerService.setPopulateJMSXUserID(true);
brokerService.addConnector("tcp://127.0.0.1:61616");
brokerService.start();
}
}
启动之后我们可以像使用我们安装的ActiveMQ一样去使用它!!!!!
Spring整合ActiveMQ
pom.xml添加spring支持JMS的包
<dependencies>
<!-- activemq核心依赖包 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.10.0</version>
</dependency>
<!-- 嵌入式activemq的broker所需要的依赖包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
</dependency>
<!-- activemq连接池 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.9</version>
</dependency>
<!-- spring支持jms的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--spring相关依赖包-->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.4</version>
</dependency>
<!-- Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
spring配置文件:spring-activemq.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.aismall.springActiveMQ"/>
<!-- 配置生产者 -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616"/>
</bean>
<!-- 这个是队列目的地,点对点的Queue -->
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 通过构造注入Queue名 -->
<constructor-arg index="0" value="spring-active-queue"/>
</bean>
<!-- 这个是队列目的地, 发布订阅的主题Topic-->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-active-topic"/>
</bean>
<!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 传入连接工厂-->
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 传入目的地 -->
<property name="defaultDestination" ref="destinationQueue"/>
<!-- 消息自动转换器-->
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean>
</beans>
队列生产者和消费者
生产者代码:
@Service
public class SpringMQ_Produce {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-activemq.xml");
SpringMQ_Produce springMQ_producer = (SpringMQ_Produce) applicationContext.getBean("springMQ_Produce");
//此处使用了lambda表达式
springMQ_producer.jmsTemplate.send((session) -> {
TextMessage textMessage=session.createTextMessage("*****spring和ActiveMQ的整合case*******");
return textMessage;
});
System.out.println("********send task over");
}
}
消费者代码:
@Service
public class SpringMQ_Consumer {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-activemq.xml");
SpringMQ_Consumer consumer=(SpringMQ_Consumer)applicationContext.getBean("springMQ_Consumer");
String retValue=(String)consumer.jmsTemplate.receiveAndConvert();
System.out.println("*******消费者收到的消息:"+retValue);
}
}
主题生产者和消费者
再前面的配置文件中,已经把有关目的地为主题的配置进去了,我们只需要在jmsTemplate
中把目的地改为topic类型
的即可,其他地方不用动:
<!-- 传入目的地:队列 -->
<property name="defaultDestination" ref="destinationQueue"/>
<!-- 更改为:topic -->
<property name="defaultDestination" ref="destinationTopic"/>
配置消费者的监听类
为什么要配置监听器?
- 在spring里面实现,消费者不启动,直接通过配置监听来完成
- 一般情况下,主题订阅发布这种模式,都是先订阅,才能收到发布的饿消息,通过配置监听器,我们不用启动消费者,当发布者发消息,直接通过监听器来实现消息的消费,我个人理解,
就是把消费者的功能配置到监听器里面,通过监听器来自动实现对主题的订阅以及消息的消费
。
监听器代码:
@Component
public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
if(null!=message&&message instanceof TextMessage){
TextMessage textMessage=(TextMessage) message;
try {
System.out.println(textMessage.getText());
}catch (JMSException e){
e.printStackTrace();
}
}
}
}
在XML配置文件中添加配置:
<!--自己编写的监听器配置,注意class这个位置,要和自己项目的一样-->
<bean id="MyMessageListener" class="com.aismall.springActiveMQ.myListener.MyMessageListener"></bean>
<!--配置消费者的监听类-->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!--传入连接工厂-->
<property name="connectionFactory" ref="connectionFactory"></property>
<!--传入目的地 -->
<property name="destination" ref="destinationTopic"></property>
<property name="messageListener" ref="MyMessageListener"></property>
</bean>