搜索词>>mq 耗时0.0040
  • spring boot整合activemq实现MQ的使用-java编程中

    spring boot整合activemq。本博客将通过一个简单的例子讲解在spring boot中activemq如何作为消费者(Consumer )和如何在spring boot中消息提供者(Provider )的实现。<h2>一.去activemq官网下载mq软件</h2> 去Apache官网下载activemq软件,并安装。 <h2>二.编写Java代码</h2> java编程中spring boot整合activemq实现MQ的使用<br /> 1.在spring boot项目中添加activemq的依赖 <pre> <code class="language-xml"><!-- activemq support --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!-- /activemq support --></code></pre> 2.application.properties配置文件中添加mq的相关配置 <pre> <code>#==================activemq Config Start================== spring.activemq.broker-url=tcp://127.0.0.1:61616 spring.activemq.in-memory=true spring.activemq.password=admin spring.activemq.user=admin spring.activemq.packages.trust-all=false spring.activemq.packages.trusted= spring.activemq.pool.configuration.*= spring.activemq.pool.enabled=false spring.activemq.pool.expiry-timeout=0 spring.activemq.pool.idle-timeout=30000 spring.activemq.pool.max-connections=1 #==================activemq Config End ==================</code></pre> <br /> 3.消息提供者 <pre> <code class="language-java">package com.htwl.collection.cqyth.amq; import java.text.SimpleDateFormat; import java.util.Date; import javax.jms.Destination; 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; /** * MQ消息提供者 * * @author leftso * */ @Component @EnableScheduling // 测试用启用任务调度 public class Provider { /** MQ jms实例 **/ @Autowired private JmsMessagingTemplate jmsMessagingTemplate; private static int count = 0; @Scheduled(fixedDelay = 5000) // 每5s执行1次-测试使用 public void send() { Destination destination = new ActiveMQQueue("TEST_QUEUE_LOG");// 这里定义了Queue的key String message = "Send AMQ Test ..." + count; System.out.println("[" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "]" + message); count++; this.jmsMessagingTemplate.convertAndSend(destination, message); } } </code></pre> 4.消费 <pre> <code class="language-java">package com.htwl.collection.cqyth.amq; import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class Consumer { Logger LOGGER = LoggerFactory.getLogger(this.getClass()); /** * 使用@JmsListener注解来监听指定的某个队列内的消息 **/ @JmsListener(destination = "TEST_QUEUE_LOG") public void removeMessage(String msg) { System.out.println("["+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())+"]Receive:"+msg); } } </code></pre>  
  • rabbitmq windows 安装/windows安装rabbitmq

    rabbitmq windows 安装/windows安装rabbitmqrabbitmq windows 安装/windows安装rabbitmq <h2>1.获取RabbitMQ windows版本的安装文件</h2> 首先我们需要去获取RabbitMQ官方网站获取RabbitMQ windows版本的安装文件,官方下载地址为http://www.rabbitmq.com/install-windows.html<br /> 找到下图中的地方点击下载即可。<br /> <img alt="RabbitMQ windows下载地址" class="img-thumbnail" src="/resources/assist/images/blog/d988fc1fdfa84eeeb9fc890d93c0c6d3.png" /> <h2>2.获取并安装RabbitMQ依赖的Erlang运行环境</h2> 如果你的电脑已经安装了Erlang的运行环境则忽略该步骤<br /> <br /> Erlang下载地址为http://www.erlang.org/downloads<br /> 选择对应的文件下载,我的电脑是window 64bit所以下载的64bit的版本如下图<br /> <img alt="Erlang运行环境下载" class="img-thumbnail" src="/resources/assist/images/blog/431f966f7c2b4d16b69e38f74b96e088.png" /> <h2>3.安装/启用RabbitMQ web管理</h2> RabbitMQ与其他MQ有点区别。默认情况下是关闭了web管理的。启用/安装方式如下 <ul> <li>在电脑的开始菜单中找到RabbitMQ Command Prompt (sbin dir)并点击打开</li> </ul> <p><img alt="RabbitMQ Command Prompt " class="img-thumbnail" src="/resources/assist/images/blog/314690dd0dc24188855120117ba47c9d.png" /></p> <ul> <li>停止RabbitMQ服务(默认情况安装后会启用)</li> </ul> 停止命令: <pre> <code class="language-html">rabbitmq-service stop</code></pre> <img alt="停止服务" class="img-thumbnail" src="/resources/assist/images/blog/f7b3d47942c04a1e89793f86c6715771.png" /> <ul> <li>启用web管理器</li> </ul> 命令: <pre> <code class="language-html">rabbitmq-plugins enable rabbitmq_management</code></pre> <img alt="启用web管理" class="img-thumbnail" src="/resources/assist/images/blog/c7111ca483e14000b56bb34543cfd8d0.png" /> <ul> <li>启动RabbitMQ服务</li> </ul> 命令: <pre> <code class="language-html">rabbitmq-service stop</code></pre> <img alt="rabbitmq服务启动" class="img-thumbnail" src="/resources/assist/images/blog/74f26447eb8c4ae1af89fcae2544e05f.png" /> <h2><br /> 4.进入管理界面</h2> 默认管理地址为:http://localhost:15672<br /> 默认用户/密码guest/guest<br /> <img alt="管理页面" class="img-thumbnail" src="/resources/assist/images/blog/92d46c2b336649b6bcd305113bf7b234.png" />