A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马