前面三节讲了ActiveMQ的安装、测试。JMS介绍以及四大组成元素,还有ActiveMQ的可靠性(持久化、事务、签收),还有Broker等等。
但是光学会还不够,我们还要把ActiveMQ应用到我们已有的框架中,如Spring和SpringBoot等等。
这一节主要学习如何在Spring中使用ActiveMQ。
首先创建一个spring项目,在pom文件中导入我们需要的jar包
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-allartifactId>
<version>5.15.9version>
dependency>
<dependency>
<groupId>org.apache.xbeangroupId>
<artifactId>xbean-springartifactId>
<version>4.17version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.11.3version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-poolartifactId>
<version>5.15.9version>
dependency>
然后导入Spring必须的jar包。
<context:component-scan base-package="com.zxg.activemq">context:component-scan>
<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://129.*.*.*:61616">property>
bean>
property>
<property name="maxConnections" value="100">property>
bean>
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="spring-active-queue">constructor-arg>
bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory">property>
<property name="defaultDestination" ref="destinationQueue">property>
<property name="messageConverter" >
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter">bean>
property>
bean>
public interface SpringMQ_Produce {
void send(String messageStr);
}
@Service("springMQ_Produce")
public class SpringMQ_ProduceImpl implements SpringMQ_Produce{
@Autowired
private JmsTemplate jmsTemplate;
public void send(String messageStr) {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringMQ_ProduceImpl produce = (SpringMQ_ProduceImpl) ctx.getBean("springMQ_Produce");
produce.jmsTemplate.send((session)->{
TextMessage textMessage = session.createTextMessage(messageStr);
return textMessage;
});
System.out.println("send success");
}
}
public class TestProduceService {
private SpringMQ_Produce springMQ_Produce;
ApplicationContext ctx;
{
ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
springMQ_Produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");
}
@Test
public void test() {
springMQ_Produce.send("Spring 整合ActiveMQ!!!");
}
}
为了方便,就直接在Service层实现测试
@Service
public class SpringMQ_Consumer {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");
String re = (String) consumer.jmsTemplate.receiveAndConvert();
System.out.println(re);
}
}
查看运行结果,正好是生产者发布的消息,并且消息队列出队发生变化。
首先修改配置文件applicationContext.xml
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-active-topic">constructor-arg>
bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory">property>
<property name="defaultDestination" ref="destinationTopic">property>
<property name="messageConverter" >
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter">bean>
property>
bean>
然后消费者与生产者的代码不用改,我们的配置信息全在applicationContext.xml中。
实现在spring里面实现消费者不启动,直接通过配置监听完成。
简单一点,就是把之前消费者中的监听器代码改为使用一个监听类来实现即可。
首先更改配置文件applicationContext.xml
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory">property>
<property name="destination" ref="destinationQueue">property>
<property name="messageListener" ref="myMessageListener">property>
bean>
然后写我们的监听类
@Component
public class MyMessageListener implements MessageListener{
@Override
public void onMessage(Message message) {
if(null!=message && message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("textMessage:"+textMessage);
}
}
}