1.
打开 application.properties 文件 添加 spring.activemq.in-memory=true spring.activemq.pool.enabled=false
spring.activemq.in-memory=true
#是否是内存模式,默认为true.
spring.activemq.pool.enabled=false
#是否创建PooledConnectionFactory,而非ConnectionFactory,默认false
2、自定义工厂
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import javax.jms.ConnectionFactory;
/**
* @author Yang
* @create 2018-04-24 15:22
* 描述:
*/
@Configuration
public class MyContainerFactory {
/**
* 主题模式
* @param connectionFactory
* @return
*/
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
return bean;
}
/**
* 队列模式
* @param connectionFactory
* @return
*/
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(connectionFactory);
return bean;
}
}
3、消费者 consumer 添加 @JmsListener(destination="",containerFactory="")
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
/**
* @author Yang
* @create 2018-04-23 16:54
* 描述:
*/
@Component
public class Consumer {
@JmsListener(destination = "queue",containerFactory ="jmsListenerContainerQueue")
@SendTo("returnmsg")
public String receiveQueue(String msg) {
System.out.println("==============" + msg + "==================");
return "老铁我收到了"+msg;
}
@JmsListener(destination = "queue",containerFactory ="jmsListenerContainerQueue")
public void receiveQueue2(String msg) {
System.out.println("======receiveQueue2========" + msg + "=========receiveQueue2=========");
}
@JmsListener(destination = "topic",containerFactory ="jmsListenerContainerTopic")
@SendTo("returnmsg")
public String receiveTopic(String msg){
System.out.println("==topic=====" + msg + "=======topic====");
return "topic"+msg;
}
@JmsListener(destination = "topic",containerFactory ="jmsListenerContainerTopic")
public void receiveTopic2(String msg){
System.out.println("==topic2=====" + msg + "=======topic2====");
}
}
4.效果:
5.代码下载:
https://download.csdn.net/download/mr_yangzc/10370847 |
|