(六)springboot整合activemq

  1、现下载activemq,下载链接:http://activemq.apache.org/download.html,windows系统解压后进入bin目录,分32位、64位操作系统,运行activemq.bat启动程序,访问http://http://127.0.0.1:8161

 出现该界面说明安装成功,点击broker,输入账号admin 密码admin 进入管理界面

点击queue按钮,可以创建消息队列。

  2、pom文件中增加以下依赖,在application.properties对activema进行配置

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
#访问地址
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
#是否开启线程池
spring.activemq.pool.enabled=true
#最大连接数
spring.activemq.pool.max-connections=50

  3、在Application.class中添加如下代码,方便注入队列

package com.zc.app.test;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; @SpringBootApplication
public class TestApplication {
@Bean
public Queue queue(){
return new ActiveMQQueue("test.queue");
}
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
} }

TestApplication

   4、新建一个service接口用来发送消息,并实现发送消息

/**
*
*/
package com.zc.app.test.service; import javax.jms.Destination; public interface MsgService { public void sendMessage(Destination destination,String message); public void sendMessage(String message);
}

MsgService

/**
*
*/
package com.zc.app.test.service.impl; import javax.jms.Destination;
import javax.jms.Queue; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; import com.zc.app.test.service.MsgService; @Service
public class MsgServiceImpl implements MsgService{ @Autowired
private Queue queue; @Autowired
private JmsMessagingTemplate jms; //用来发送消息 @Override
public void sendMessage(Destination destination,String message) {
jms.convertAndSend(this.queue, message); } @Override
public void sendMessage(String message) {
jms.convertAndSend(message); } }

MsgServiceImpl

     5、写controller来调用接口

/**
*
*/
package com.zc.app.test.controller; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.zc.app.test.service.MsgService; @RestController
@RequestMapping("/msg")
public class SendController { @Autowired
private MsgService msgService; @GetMapping("send")
public String order(String message) { Destination destination = new ActiveMQQueue("test.queue");
msgService.sendMessage(destination, message);
return "send message success";
}
}

SendController

   6、启动程序,这时程序出现错误,提示JmsMessagingTemplate注入失败

Field jms in com.zc.app.test.service.impl.MsgServiceImpl required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' that could not be found.

   7、通过测试,将acticemq线程池配置删除后,程序可以正常启动,最后找到原因是因为少了jms的pool依赖包,在pom文件添加以下依赖后可以正常启动

        <dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>

     8、然后访问url:localhost:8080/msg/send?message=124515,提示成功后,可以从activemq的管理界面看到test.queue消息增加

最新文章

  1. 基于NPOI导出和导入Excel
  2. Module Zero之角色管理
  3. ubuntu配置 Java SE 1.6
  4. Pycharm注册码
  5. php手册总结(一)
  6. Oracle PL/SQL实战代码下载
  7. checkbox 设置不可更改
  8. 分享一个TP5实现Create()方法的心得
  9. 免费PHP WEB环境套件介绍
  10. poj1434Fill the Cisterns!(二分)
  11. asp.net中Response对象鱼Request对象
  12. 关于ThinkPHP中Session不能夸模块/控制器使用的问题-网上的答案我做个补充
  13. PHP获取用户真实 IP , 淘宝IP接口获得ip地理位置(转)
  14. mysql数据库乱码
  15. Innodb和MyISAM比较
  16. UESTC_导弹拦截 2015 UESTC Training for Dynamic Programming&lt;Problem N&gt;
  17. Java HttpURLConnection发送post请求示例
  18. C语言实例:类型转换
  19. win7,win10系统激活工具下载
  20. python locust 性能测试:locust 参数化(list) ---循环取数据,数据可重复使用

热门文章

  1. js中Function方法
  2. 字符编码ANSI、ASCII、GB2312、GBK、GB18030、UNICODE、UTF-8小结
  3. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; che
  4. 6、git和github
  5. FormsAuthentication.RedirectFromLoginPage 登录
  6. C#识别图中二维码
  7. fatal: Authentication failed (二)
  8. 51nod1119(除法取模/费马小定理求组合数)
  9. k8s 更新应用程序
  10. xcode8.3 shell 自动打包脚本