SpringBoot整合ActiveMQ的简单应用(1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34178998/article/details/86507688

1、首先是从官网下载

然后打开

运行起来,这时候要根据自己的电脑运行什么样的位数,本人电脑是64位的,所以运行64位的程序,这时候会显示一个界面

说明启动成功了,然后在电脑浏览器中输入地址:http://localhost:8161/admin/queues.jsp

进行登录,用户名和密码都是admin,这个时候就能看到消息队列了

我这里有三个消息队列

maven中导包

<!-- 配置 activemq-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
		</dependency>

    

之后就在项目中编写生产者和消费者了

生产者:


import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Destination;
import javax.jms.Queue;
 
/**
 * describe:
 *
 * @author DCX
 * @date 2019-1-16
 */
@Component
@EnableScheduling
public class PromoteActProducer {
 
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
 
   // @Scheduled(cron = "0/3 * * * * ?")    // 每3s执行1次
    public void sendMsg(String destinationNmae,String msg) {
    	Destination destination = new ActiveMQQueue(destinationNmae);
        this.jmsMessagingTemplate.convertAndSend(destination, msg);
    }
}

消费者


import org.springframework.jms.annotation.JmsListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
/**
 * describe:
 *
 * @author DCX
 * @date 2019-1-16
 */
@Component
public class PromoteActConsumer {
 
    /**
     * 客户端消费
     * @param consumer
     * @throws InterruptedException 
     */
	
    @JmsListener(destination = "任务名称")
    public void receiveQueue(String consumer) throws InterruptedException {
    	Thread.sleep(4000);
        System.out.println(consumer+"--这条消息已经消费了");
    }
}

这个消费者要注意一下, @JmsListener(destination = "任务名称"),这个注解是监听我创建的destination的名称叫做“任务名称”的一个消息,它才会去消费

消费者这里是自动监听消费的,但是生产者的生产消息不是自动的,这时我才用手动方式来增加队列消息,在控制层编写一个测试

内容为:


import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.xtkj.entity.common.Result;
import cn.xtkj.mq.PromoteActProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/testActiveMQ")
@Api(tags = "任务测试", description = "TestTask")
public class TestTaskController {
	@Resource
	private PromoteActProducer producer;
	
	@PostMapping("createMsg")
	@ApiOperation(value = "生产者创建消息", response = Result.class)
	private Object createMsg(
			@ApiParam(value = "内容", required = false) @RequestParam(required = false) String destinationNmae,
			@ApiParam(value = "任务信息", required = false) @RequestParam(required = false) String msg) {
		
		for (int i = 0; i < 10; i++) {
			producer.sendMsg(destinationNmae, msg);
		}
		
		return "生产顺利";
	}

}

然后在配置文件中配置信息

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=false
#true表示使用连接池
spring.activemq.pool.enabled=true
#连接池最大连接数
spring.activemq.pool.max-connections=5
#空闲的连接过期时间,默认为30秒
spring.activemq.pool.idle-timeout=30000
#强制的连接过期时间,与idleTimeout的区别在于:idleTimeout是在连接空闲一段时间失效,而expiryTimeout不管当前连接的情况,只要达到指定时间就失效。默认为0,never
spring.activemq.pool.expiry-timeout=0

最后启动项目,

本人采用swagger-ui接口调试

传入参数之后,生产者会生产十个消息,这时候再去页面看下

刚刚新建的十条队列消息已经在队列中

猜你喜欢

转载自blog.csdn.net/qq_34178998/article/details/86507688
今日推荐