1. activemq

  首先引入依赖

  pom.xml文件

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-activemq</artifactId>
  4. </dependency>

  创建一个配置队列类

  JMSConfiguration.java

  1. package com.wangx.boot.util;
  2.  
  3. import org.apache.activemq.command.ActiveMQQueue;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.jms.annotation.EnableJms;
  7.  
  8. import javax.jms.Destination;
  9.  
  10. @Configuration
  11. @EnableJms
  12. public class JMSConfiguration {
  13.  
  14. @Bean
  15. public Destination createDestination () {
  16. return new ActiveMQQueue("com.wangx");
  17. }
  18. }

  创建一个消息生产者和消息消费者

  

  1. package com.wangx.boot.mq;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.jms.annotation.JmsListener;
  5. import org.springframework.jms.core.JmsMessagingTemplate;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import javax.jms.Destination;
  9. @Component
  10. public class JMSComponent {
  11.  
  12. @Autowired
  13. private JmsMessagingTemplate jmsMessagingTemplate;
  14.  
  15. @Autowired
  16. private Destination destination;
  17.  
  18. public void send (String message) {
  19. jmsMessagingTemplate.convertAndSend(destination, message);
  20. }
  21.  
  22. @JmsListener(destination = "com.wangx")
  23. public void listener (String message) {
  24. System.out.println("接收到的消息:" + message);
  25. }
  26. }

  @JmsListener中的destination必须与队列配置类中定一的queue的名字相同。

   SpringBoot提供了一个默认内置的消息队列中间件,如果我们使用spring.activemq.in-memory=true时将会使用内置的消息队列,但是它也提供了我们使用外部activemq的一些配置:

  

  1. #spring.activemq.broker-url=
  2. #spring.activemq.password=
  3. #spring.activemq.user=
  4. #spring.activemq.packages.trust-all=false
  5. #spring.activemq.packages.trusted=
  6. #spring.activemq.pool.configuration.*=
  7. #spring.activemq.pool.enabled=false
  8. #spring.activemq.pool.expiry-timeout=0
  9. #spring.activemq.pool.idle-timeout=30000
  10. #spring.activemq.pool.max-connections=1

  测试消息发送

  

  1. package com.wangx.boot.controller;
  2.  
  3. import com.wangx.boot.mq.JMSComponent;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8.  
  9. @Controller
  10. @RequestMapping("/mq")
  11. public class JMSController {
  12.  
  13. @Autowired
  14. private JMSComponent jmsComponent;
  15.  
  16. @RequestMapping("/send")
  17. @ResponseBody
  18. public String send(String msg) {
  19. jmsComponent.send(msg);
  20. return msg;
  21. }
  22. }

  当访问localhost:8080/mq/send?msg=xxx时,消费者的监听方法(带有@JmsListener注解的方法)会自动监听到消息,并打印到控制台上。

2. rabbitmq的使用

  首先引入pom.xml 

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>

ps:rabbitmq和activemq的依赖不能同时存在。

  首先还是创建一个队列配置类

  AMQConfiguration.java

  1. package com.wangx.boot.util;
  2.  
  3. import org.springframework.amqp.core.Queue;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. @Configuration
  8. public class AMQConfiguration {
  9.  
  10. @Bean
  11. public Queue queue() {
  12. return new Queue("hello", true);
  13. }
  14.  
  15. }

  接着创建消息生产和消费组件

  1. package com.wangx.boot.mq;
  2.  
  3. import org.springframework.amqp.core.AmqpTemplate;
  4. import org.springframework.amqp.core.Queue;
  5. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8.  
  9. @Component
  10. public class AMQComponent {
  11.  
  12. @Autowired
  13. private AmqpTemplate amqpTemplate;
  14.  
  15. public void send(String message) {
  16. amqpTemplate.convertAndSend("hello", message);
  17. }
  18.  
  19. @RabbitListener(queues = "hello")
  20. public void receiveQueue(String text) {
  21. System.out.println("接受到:" + text);
  22. }
  23. }

  在SpringBoot的启动类上添加@EnableRabbit表示开启rabbit消息队列。

  测试是否发送了消息

  

  1. package com.wangx.boot.controller;
  2.  
  3. import com.wangx.boot.mq.AMQComponent;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8.  
  9. @Controller
  10. @RequestMapping("/amq")
  11. public class AMQController {
  12.  
  13. @Autowired
  14. private AMQComponent amqComponent;
  15.  
  16. @RequestMapping("/send")
  17. @ResponseBody
  18. public String send(String msg) {
  19. amqComponent.send(msg);
  20. return msg;
  21. }
  22. }

  访问localhost:8080/amq/send?msg=xxx,在调用放松消息的方法时。监听的方法同样会收到消息,并打印到控制台上。

3. 调用rest服务

  3.1 代码实现

    首先引入依赖

    pom文件

  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. </dependency>

  然后随便写一个Controller接口,如:

  1. package com.wangx.boot.controller;
  2.  
  3. import com.wangx.boot.cache.CachingBook;
  4. import com.wangx.boot.entity.Book;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.*;
  7.  
  8. @RestController
  9. @RequestMapping("/api")
  10. public class ApiController {
  11.  
  12. @Autowired
  13. private CachingBook cachingBook;
  14.  
  15. @RequestMapping(value = "/select", method = RequestMethod.GET)
  16. public Book get(@RequestParam(defaultValue = "遮天") String name) {
  17. Book book = cachingBook.findById(name);
  18. return book;
  19. }
  20.  
  21. @RequestMapping(value = "/update", method = RequestMethod.GET)
  22. public Book update(@RequestParam(defaultValue = "遮天") String name) {
  23. Book bean = cachingBook.findById(name);
  24. bean.setAuthor("耳根");
  25. cachingBook.updateById(bean);
  26. return bean;
  27. }
  28.  
  29. @RequestMapping(value = "/del", method = RequestMethod.GET)
  30. public String del(@RequestParam(defaultValue = "遮天") String name) {
  31. return cachingBook.deleteById(name);
  32. }
  33. }

  启动服务,在另一个工程中使用RestTemplateBuilder来访问我们启动的服务,

  1. @Autowired
  2. private RestTemplateBuilder restTemplateBuilder;
  3.  
  4. /**
  5. * get请求
  6. */
  7. @Test
  8. public void getForObject() {
  9. //发送get请求
  10. String res = restTemplateBuilder.build().getForObject("http://localhost:8080/api/select",String.class, "遮天");
  11. System.out.println(res);
  12. Map<String,Object> map = new HashMap<String,Object>();
  13. map.put("name", "遮天");
  14. //发送post请求
  15. res = restTemplateBuilder.build().postForObject("http://localhost:8080/api/update", map, String.class);
  16. System.out.println(res);
  17. }

  可以成功调用我们启动的服务的接口。

  3.2 使用代理

    使用RestTemplate还可以自己实现代理的功能。

  

  1. public class ProxyCustomizer implements RestTemplateCustomizer {
  2. @Override
  3. public void customize(RestTemplate restTemplate) {
  4. //http://ip.zdaye.com/ 上可以查询可用的主机和端口
  5. String proxyHost = "59.33.46.187";
  6. int proxyPort = 6969;
  7.  
  8. HttpHost proxy = new HttpHost(proxyHost, proxyPort);
  9. HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
  10. @Override
  11. public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
  12.  
  13. return super.determineProxy(target, request, context);
  14. }
  15. }).build();
  16. HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
  17. httpComponentsClientHttpRequestFactory.setConnectTimeout(10000);
  18. httpComponentsClientHttpRequestFactory.setReadTimeout(60000);
  19. restTemplate.setRequestFactory(httpComponentsClientHttpRequestFactory);
  20. }
  21. }

  测试方式:

  1. String result = restTemplateBuilder.additionalCustomizers(new ProxyCustomizer()).build().getForObject("http://www.baidu.com", String.class);
  2. System.out.println(result);

SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用的更多相关文章

  1. SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

    1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...

  2. SpringBoot集成ActiveMq消息队列实现即时和延迟处理

    原文链接:https://blog.csdn.net/My_harbor/article/details/81328727 一.安装ActiveMq 具体安装步骤:自己谷歌去 二.新建springbo ...

  3. ActiveMQ 消息队列服务

      1 ActiveMQ简介 1.1 ActiveMQ是什么 ActiveMQ是一个消息队列应用服务器(推送服务器).支持JMS规范. 1.1.1 JMS概述 全称:Java Message Serv ...

  4. JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明

    1----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...

  5. rabbitmq学习(二):rabbitmq(消息队列)的作用以及rabbitmq之直连交换机

    前言 上篇介绍了AMQP的基本概念,组成及其与rabbitmq的关系.了解了这些东西后,下面我们开始学习rabbitmq(消息队列)的作用以及用java代码和rabbitmq通讯进行消息发布和接收.因 ...

  6. C#调用RabbitMQ实现消息队列

    前言 我在刚接触使用中间件的时候,发现,中间件的使用并不是最难的,反而是中间件的下载,安装,配置才是最难的. 所以,这篇文章我们从头开始学习RabbitMq,真正的从头开始. 关于消息队列 其实消息队 ...

  7. ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息

    Java消息服务(Java Message Service ,JMS)是一个Java标准,定义了使用消息代理的通用API .在JMS出现之前,每个消息代理都有私有的API,这就使得不同代理之间的消息代 ...

  8. C#实现ActiveMQ消息队列

    本文使用C#实现ActiveMQ消息队列功能. 一.首先需要导入两个包,分别是:Apache.NMS 和 Apache.NMS.ActiveMQ 二.创建Winform程序实现生产者功能. 三.Pro ...

  9. ActiveMQ基础教程(四):.net core集成使用ActiveMQ消息队列

    接上一篇:ActiveMQ基础教程(三):C#连接使用ActiveMQ消息队列 这里继续说下.net core集成使用ActiveMQ.因为代码比较多,所以放到gitee上:https://gitee ...

随机推荐

  1. C#3.0新增功能09 LINQ 标准查询运算符 04 运算

    连载目录    [已更新最新开发文章,点击查看详细] 本篇主要介绍标准查询运算符的常用运算功能. 01 对数据排序 排序操作基于一个或多个属性对序列的元素进行排序. 第一个排序条件对元素执行主要排序. ...

  2. Cesium 学习(二)所支持的模型数据类型,以及转换

    1.Cesium所支持的模型数据类型 目前所知的有glTF.glb.bgltf等格式的模型数据: 想要了解glTF等的知识可以看一下https://www.cnblogs.com/fuckgiser/ ...

  3. PHP中的$_POST变量

    定义 在 PHP 中,预定义的 $_POST 变量用于收集来自 method="post" 的表单中的值. $_POST 变量 预定义的 $_POST 变量用于收集来自method ...

  4. HashTable源码解读

    一:总述 底层实现原理是用数组+链表,与HashMap一样,但HashTable是线程安全的,HashMap是非线程安全的 下面是其结构图(与hashMap类似) 二:属性说明 /** * The h ...

  5. nodejs 如何自动化配置环境参数

    应用场景: 最近用 node 重构了网站的项目,部署到测试环境的时候测试一切正常. 直到有一天,运维把代码上线到内测环境的时候...... 突然发现:内测环境和测试环境竟然是同一台服务器,只不过是把代 ...

  6. bootstrap-treeview后台Json数据的封装及前台的显示

    1.bootStrap-treeview是我们常用的树形结构,页面风格也比较清新,但是后台数据的封装比较麻烦,经过研究终于解决,和大家分享一下. 2.前端代码如下 <script> var ...

  7. java抽奖思路

    现在在做一个有关抽奖的活动,将我自己所做的抽奖思路书写一下 1.项目奖项的配置存储在MongoDB 配置的参数为 奖项的等级(prizeLevel).数量(prizeNum).奖项的名称(prizeN ...

  8. 【Docker】unauthorized: incorrect username or password

    昨天朋友推荐玩玩 Docker.虽然之前就听过,但一直不清楚干嘛的,也没去搞过,虽然前段时间就装了,但一直没打开.这两天刚开始熟悉,就遇到了点小问题.一番 Google 之后解决了,记录一下. CLI ...

  9. 【iOS】ARC & MRC

    iOS 项目类型,是 ARC 还是 MRC 未完……

  10. ubuntu 下常用的mysql 命令

    一.mysql服务操作  0.查看数据库版本 sql-> status;  1.net start mysql //启动mysql服务  2.net stop mysql //停止mysql服务 ...