1.添加依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
<!--activemq对jms的支持,整合spring和activemq-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.23.RELEASE</version>
</dependency>
2.Spring配置文件
<context:component-scan base-package="spring"/>
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<!--真正可以产生Connnection的ConnectionFactory,由对应的JMS服务厂提供-->
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.12.128:61616"/>
</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"/> <!--队列名称-->
</bean>
<!--这个是主题的目的地-->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-active-queue"/> <!--主题名称-->
</bean>
<!--spring提供的JMS工具类,它可以进行消息发送、接收等-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"/>
<!--<property name="defaultDestination" ref="destinationQueue"/>--> //对于队列还是主题,不需要改变源代码,只需要改变这里的配置就好
<property name="defaultDestination" ref="destinationTopic"/>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean>
<!--配置监听程序:只需要启动生产者,消费者不用启动,自动会监听记录-->
<bean id="jmsController" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<!--<property name="destination" ref="destinationQueue"/>--> <!--监听队列-->
<property name="destination" ref="destinationTopic"/> <!--监听主题-->
<!--需要一个实现了MessageListener接口的类,自己创建-->
<property name="messageListener" ref="myMessageListener"/>
</bean>
<!--只需要启动生产者,消费者不用启动,自动会监听记录-->
<bean id="myMessageListener" class="spring.MyMessageListener"></bean>
3.监听器代码
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();
}
}
}
}
4.生产者代码
//消息生产者
@Service
public class SpringMQ_Produce {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
SpringMQ_Produce produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");
/*
匿名内部类方式
produce.jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("*******spring和ActiveMQ的整合case111....");
return textMessage;
}
});*/
produce.jmsTemplate.send((session) -> {
TextMessage textMessage = session.createTextMessage("*******spring和ActiveMQ的整合case222....");
return textMessage;
});
System.out.println("******send task over");
}
}
5.消费者代码
//消息消费者
@Service
public class SpringMQ_Consumer {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");
//因为生产者那边的是TextMessage,所以这里用String接收
String reValue=(String)consumer.jmsTemplate.receiveAndConvert();
System.out.println(reValue);
}
}