欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot集成activeMq实战

程序员文章站 2022-04-30 19:45:30
...

1.首先搭建好activeMq服务器

参考我的上一篇文章:Linux搭建activMq服务

2. 创建springboot项目

参考我的文章:springboot实战

3. maven依赖引入

<!-- activemq start -->
    <!-- mq依赖 -->
	 <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-activemq</artifactId>  
        </dependency> 
     <!-- mq连接池 -->
        <dependency>
	     <groupId>org.apache.activemq</groupId>
	     <artifactId>activemq-pool</artifactId>
        </dependency>
<!-- activemq end -->

4.application.properties文件配置

# activemq配置  
spring.activemq.broker-url=tcp://IP:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.jms.pub-sub-domain=false
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=50
spring.activemq.pool.expiry-timeout=10000
spring.activemq.pool.idle-timeout=30000
#注意 对象传输需开启包白名单 否则会报错- -
spring.activemq.packages.trust-all=true

5. 生产者代码

@Component
public class JMSProducer {
	@Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

	public void sendMessage(Destination destination,String message) {
        this.jmsMessagingTemplate.convertAndSend(destination,message);
    }
	public void send(String message) {
		Destination destination = new ActiveMQQueue("com.xue.demo");
		jmsMessagingTemplate.convertAndSend(destination, message);
    }
}

6.消费者代码

@Component
public class JMSConsumer {
    private final static Logger logger = LoggerFactory.getLogger(JMSConsumer.class);

    @JmsListener(destination = "com.xue.demo")
    public void receive(String message) {
    	logger.info("接收到消息:{}",message);
        System.out.println("收到的 message 是:" + message);
    }
}

7.测试类代码

        @Autowired
	private JMSProducer jp;
	
	@RequestMapping("/send")
	public String send() {
		jp.send("i am producer");
		logger.info("i am producer");  
		return "success";
	}

8.结果

springboot集成activeMq实战


相关标签: springboot activeMQ