<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-pool -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.10.0</version>
</dependency>
<!-- 配置生产者 -->
<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.0.139:61616" />
</bean>
</property>
<property name="maxConnections" value="100" />
</bean>
<!-- 队列目的地 -->
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue" >
<constructor-arg index="0" value="spring-active-queue" /> <!--队列名称-->
</bean>
<!-- JMS工具类 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" >
<property name="connectionFactory" ref="jmsFactory" />
<property name="defaultDestination" ref="destinationQueue" />
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter" />
</property>
</bean>
//生产者
@Service
public class SpringMQ_Produce {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(String msg) {
jmsTemplate.send(session -> {
TextMessage textMessage = session.createTextMessage(msg);
return textMessage;
});
System.out.println("*发送信息完成*");
}
}
//消费者
@Service
public class SpringMQ_Cousumer {
@Autowired
private JmsTemplate jmsTemplate;
public void receiveMessage() {
String textMessage = (String) jmsTemplate.receiveAndConvert();
System.out.println("收到消息: " + textMessage);
}
}
//测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class SpringMQTest {
@Autowired
private SpringMQ_Produce springMQ_produce;
@Test
public void sendTest() {
springMQ_produce.sendMessage("Spring+ActiveMQ");
}
@Test
public void receiveTest() {
springMQ_cousumer.receiveMessage();
}
}
//*发送信息完成*
//收到消息: Spring+ActiveMQ
发送消息到Topic
修改默认destination,其他代码不变
<!-- 定义Topic-->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-active-topic" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" >
<property name="connectionFactory" ref="jmsFactory" />
<property name="defaultDestination" ref="destinationTopic" />
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter" />
</property>
</bean>
监听设置:
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory" />
<property name="destination" ref="destinationQueue" />
<!-- 自己实现 -->
<property name="MessageListener" ref="messageListener" />
</bean>
@Service("messageListener")
public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
if(message != null && message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("监听到消息 :" + textMessage.getText());
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
只启动生产者,不用启动消费者,注意监听的destination和发送的destination是否一样
监听到消息 :Spring+ActiveMQ
*发送信息完成*
java.lang.NoSuchMethodError: org.springframework.aop.scope.ScopedProxyUtils.isScopedTarget