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连接空闲一段时间失效,epiryTimeout不管当前连接的情况,只要达到指定时间就失效。默认为0,never
spring.activemq.pool.expiry-timeout=0
最后启动项目,
本人采用swagger-ui接口调试
传入参数之后,生产者会生产十个消息,这时候再去页面看下
刚刚新建的十条队列消息已经在队列中