SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
1. activemq
首先引入依赖
pom.xml文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
创建一个配置队列类
JMSConfiguration.java
package com.wangx.boot.util; import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms; import javax.jms.Destination; @Configuration
@EnableJms
public class JMSConfiguration { @Bean
public Destination createDestination () {
return new ActiveMQQueue("com.wangx");
}
}
创建一个消息生产者和消息消费者
package com.wangx.boot.mq; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component; import javax.jms.Destination;
@Component
public class JMSComponent { @Autowired
private JmsMessagingTemplate jmsMessagingTemplate; @Autowired
private Destination destination; public void send (String message) {
jmsMessagingTemplate.convertAndSend(destination, message);
} @JmsListener(destination = "com.wangx")
public void listener (String message) {
System.out.println("接收到的消息:" + message);
}
}
@JmsListener中的destination必须与队列配置类中定一的queue的名字相同。
SpringBoot提供了一个默认内置的消息队列中间件,如果我们使用spring.activemq.in-memory=true时将会使用内置的消息队列,但是它也提供了我们使用外部activemq的一些配置:
#spring.activemq.broker-url=
#spring.activemq.password=
#spring.activemq.user=
#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
测试消息发送
package com.wangx.boot.controller; import com.wangx.boot.mq.JMSComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/mq")
public class JMSController { @Autowired
private JMSComponent jmsComponent; @RequestMapping("/send")
@ResponseBody
public String send(String msg) {
jmsComponent.send(msg);
return msg;
}
}
当访问localhost:8080/mq/send?msg=xxx时,消费者的监听方法(带有@JmsListener注解的方法)会自动监听到消息,并打印到控制台上。
2. rabbitmq的使用
首先引入pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
ps:rabbitmq和activemq的依赖不能同时存在。
首先还是创建一个队列配置类
AMQConfiguration.java
package com.wangx.boot.util; import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class AMQConfiguration { @Bean
public Queue queue() {
return new Queue("hello", true);
} }
接着创建消息生产和消费组件
package com.wangx.boot.mq; import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class AMQComponent { @Autowired
private AmqpTemplate amqpTemplate; public void send(String message) {
amqpTemplate.convertAndSend("hello", message);
} @RabbitListener(queues = "hello")
public void receiveQueue(String text) {
System.out.println("接受到:" + text);
}
}
在SpringBoot的启动类上添加@EnableRabbit表示开启rabbit消息队列。
测试是否发送了消息
package com.wangx.boot.controller; import com.wangx.boot.mq.AMQComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/amq")
public class AMQController { @Autowired
private AMQComponent amqComponent; @RequestMapping("/send")
@ResponseBody
public String send(String msg) {
amqComponent.send(msg);
return msg;
}
}
访问localhost:8080/amq/send?msg=xxx,在调用放松消息的方法时。监听的方法同样会收到消息,并打印到控制台上。
3. 调用rest服务
3.1 代码实现
首先引入依赖
pom文件
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
然后随便写一个Controller接口,如:
package com.wangx.boot.controller; import com.wangx.boot.cache.CachingBook;
import com.wangx.boot.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RestController
@RequestMapping("/api")
public class ApiController { @Autowired
private CachingBook cachingBook; @RequestMapping(value = "/select", method = RequestMethod.GET)
public Book get(@RequestParam(defaultValue = "遮天") String name) {
Book book = cachingBook.findById(name);
return book;
} @RequestMapping(value = "/update", method = RequestMethod.GET)
public Book update(@RequestParam(defaultValue = "遮天") String name) {
Book bean = cachingBook.findById(name);
bean.setAuthor("耳根");
cachingBook.updateById(bean);
return bean;
} @RequestMapping(value = "/del", method = RequestMethod.GET)
public String del(@RequestParam(defaultValue = "遮天") String name) {
return cachingBook.deleteById(name);
}
}
启动服务,在另一个工程中使用RestTemplateBuilder来访问我们启动的服务,
@Autowired
private RestTemplateBuilder restTemplateBuilder; /**
* get请求
*/
@Test
public void getForObject() {
//发送get请求
String res = restTemplateBuilder.build().getForObject("http://localhost:8080/api/select",String.class, "遮天");
System.out.println(res);
Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "遮天");
//发送post请求
res = restTemplateBuilder.build().postForObject("http://localhost:8080/api/update", map, String.class);
System.out.println(res);
}
可以成功调用我们启动的服务的接口。
3.2 使用代理
使用RestTemplate还可以自己实现代理的功能。
public class ProxyCustomizer implements RestTemplateCustomizer {
@Override
public void customize(RestTemplate restTemplate) {
//http://ip.zdaye.com/ 上可以查询可用的主机和端口
String proxyHost = "59.33.46.187";
int proxyPort = 6969; HttpHost proxy = new HttpHost(proxyHost, proxyPort);
HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
@Override
public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException { return super.determineProxy(target, request, context);
}
}).build();
HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
httpComponentsClientHttpRequestFactory.setConnectTimeout(10000);
httpComponentsClientHttpRequestFactory.setReadTimeout(60000);
restTemplate.setRequestFactory(httpComponentsClientHttpRequestFactory);
}
}
测试方式:
String result = restTemplateBuilder.additionalCustomizers(new ProxyCustomizer()).build().getForObject("http://www.baidu.com", String.class);
System.out.println(result);
SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用的更多相关文章
- SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...
- springboot学习笔记-6 springboot整合RabbitMQ
一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...
- Redis 学习笔记(六)Redis 如何实现消息队列
一.消息队列 消息队列(Messeage Queue,MQ)是在分布式系统架构中常用的一种中间件技术,从字面表述看,是一个存储消息的队列,所以它一般用于给 MQ 中间的两个组件提供通信服务. 1.1 ...
- SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存
1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式
在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- SpringBoot学习笔记(5)----SpringBoot中异常处理的三种方法
对于异常的处理,Spring Boot中提供默认的一个异常处理界面,如下图: 但是在实际的运用开发中,这样的页面显然是不友好的,Spring Boot也提供了自定义异常处理的方式,如下总结三种一场处理 ...
- springboot学习笔记-5 springboot整合shiro
shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/ 它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...
随机推荐
- (转载) listview实现微信朋友圈嵌套
listview实现微信朋友圈嵌套 标签: androidlistview 2016-01-06 00:05 572人阅读 评论(0) 收藏 举报 分类: android(8) 版权声明:本文为博 ...
- CADisplayLink & NSTimer
屏幕刷新与UI更新不同步:屏幕刷新由硬件(+GPU)保证,UI更新由软件(CPU保证). 出现卡顿的原因是软件的计算速度跟不上硬件的刷新速度. 一 简介 1 所在框架 CADisplayLink和其它 ...
- python3 django动态分页引发的list切片下标越界问题
起先是扒了一个包,动态分页的,但这个包分页之前要加载全部的数据,我这东西后台是个爬虫,不一定浏览的完所以这么做有点浪费资源,于是我改造了一下. # :param obj_count: 获得 条目总数# ...
- laravel save() 返回 null
原因:引用其他方法时,没有 return save()的操作结果. 在使用save()方法时,发现返回值是:null:
- sdoi2013 spring(hash+容斥)
大体思路是先求出来\(f[i]\)代表有至少\(i\)个位置相同的点对数. 然后就已经没什么好害怕的了(跟BZOJ3622一样) 然后这个\(f[i\)]怎么求呢? 最无脑的方法就是枚举位置,然后\( ...
- BZOJ 3413 匹配 (后缀自动机+线段树合并)
题目大意: 懒得概括了 神题,搞了2个半晚上,还认为自己的是对的...一直调不过,最后终于在jdr神犇的帮助下过了这道题 线段树合并该是这道题最好理解且最好写的做法了,貌似主席树也行?但线段树合并这个 ...
- [USACO18FEB] Snow Boots G (离线+并查集)
题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...
- php5 中魔术方法函数有哪几个
魔术函数:9.3 构造函数:__construct() 9.3.1 实例化对象时被调用. 9.3.2 在类中,构造函数是用来初始化对象的,利用构造函数,可以操作对象,并改变它的值. 9.3.3 当__ ...
- Vue2+Webpack+ES6 兼容低版本浏览器(IE9)解决方案
Vue2+Webpack+ES6 兼容低版本浏览器(IE9)解决方案 解决方式:安装 "babel-polyfill" 即可. 命令:npm install --save-dev ...
- 一个学习scrapy的网站
当然是scrapy中文网辣,从浅到深,例子报错几乎都有 http://www.scrapyd.cn/doc/