activemq消费者消费慢_ActiveMQ 跨域

学习主题:ActiveMQ

学习目标:

1 掌握什么是spring整合mq

2 掌握jsonp

对应视频:

http://www.itbaizhan.cn/course/id/85.html

对应文档:

对应作业

1. Spring整合ActiveMQ-创建生产者

(1) Spring整合ActiveMQ创建消息生产者时需要添加哪些依赖?

<dependencies>

<!-- ActiveMQ 客户端完整 jar 包依赖 -->

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-all</artifactId>

</dependency>

<!-- ActiveMQ 和 Spring 整合配置文件标签处理 jar 包依赖 -->

<dependency>

<groupId>org.apache.xbean</groupId>

<artifactId>xbean-spring</artifactId>

</dependency>

<!-- Spring-JMS 插件相关 jar 包依赖 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

</dependency>

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-pool</artifactId>

</dependency>

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-jms-pool</artifactId>

</dependency>

<!-- 单元测试 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

</dependency>

<!-- 日志处理 -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

</dependency>

<!-- spring -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

</dependency>

<!-- JSP 相关 -->

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jsp-api</artifactId>

<scope>provided</scope>

</dependency>

</dependencies>

(2) 如何在Spring中配置消息生产者?写出步骤

<!-- 需要创建一个连接工厂,连接ActiveMQ. ActiveMQConnectionFactory. 需要依赖ActiveMQ提供的amq标签 -->

<!-- amq:connectionFactory 是bean标签的子标签, 会在spring容器中创建一个bean对象. 可以为对象命名.

类似: <bean id="" class="ActiveMQConnectionFactory"></bean> -->

<amq:connectionFactory brokerURL="tcp://192.168.93.128:61616"

userName="admin" password="admin" id="amqConnectionFactory" />

<!-- 配置池化的ConnectionFactory。 为连接ActiveMQ的connectionFactory提供连接池 -->

<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactoryBean">

<property name="connectionFactory" ref="amqConnectionFactory"></property>

<property name="maxConnections" value="10"></property>

</bean>

<!-- spring管理JMS相关代码的时候,必须依赖jms标签库. spring-jms提供的标签库. -->

<!-- 定义Spring-JMS中的连接工厂对象 CachingConnectionFactory - spring框架提供的连接工厂对象.

不能真正的访问MOM容器. 类似一个工厂的代理对象. 需要提供一个真实工厂,实现MOM容器的连接访问. -->

<!-- 配置有缓存的ConnectionFactory,session的缓存大小可定制。 -->

<bean id="connectionFactory"

class="org.springframework.jms.connection.CachingConnectionFactory">

<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>

<property name="sessionCacheSize" value="3"></property>

</bean>

<!-- JmsTemplate配置 -->

<bean id="template" class="org.springframework.jms.core.JmsTemplate">

<!-- 给定连接工厂, 必须是spring创建的连接工厂. -->

<property name="connectionFactory" ref="connectionFactory"></property>

<!-- 可选 - 默认目的地命名 -->

<property name="defaultDestinationName" value="test-spring"></property>

</bean>

2. Spring整合ActiveMQ-创建消费者

(1) Spring整合ActiveMQ创建消息消费者时需要添加哪些依赖?

<dependencies>

<!-- activemq客户端 -->

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-all</artifactId>

</dependency>

<!-- spring框架对JMS标准的支持 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

</dependency>

<!-- ActiveMQ和spring整合的插件 -->

<dependency>

<groupId>org.apache.xbean</groupId>

<artifactId>xbean-spring</artifactId>

</dependency>

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-pool</artifactId>

</dependency>

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-jms-pool</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

</dependency>

(2) 如何在Spring中配置消息消费者?写出步骤

配置spring整合消息消费者的xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:jms="http://www.springframework.org/schema/jms"

xmlns:amq="http://activemq.apache.org/schema/core"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/jms

http://www.springframework.org/schema/jms/spring-jms.xsd

http://activemq.apache.org/schema/core

http://activemq.apache.org/schema/core/activemq-core.xsd">

<!-- 需要创建一个连接工厂,连接ActiveMQ. ActiveMQConnectionFactory. 需要依赖ActiveMQ提供的amq标签 -->

<!-- amq:connectionFactory 是bean标签的子标签, 会在spring容器中创建一个bean对象.

可以为对象命名. 类似: <bean id="" class="ActiveMQConnectionFactory"></bean>

-->

<amq:connectionFactory brokerURL="tcp://192.168.93.128:61616"

userName="admin" password="admin" id="amqConnectionFactory"/>

<!-- spring管理JMS相关代码的时候,必须依赖jms标签库. spring-jms提供的标签库. -->

<!-- 定义Spring-JMS中的连接工厂对象

CachingConnectionFactory - spring框架提供的连接工厂对象. 不能真正的访问MOM容器.

类似一个工厂的代理对象. 需要提供一个真实工厂,实现MOM容器的连接访问.

-->

<bean id="connectionFactory"

class="org.springframework.jms.connection.CachingConnectionFactory">

<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>

<property name="sessionCacheSize" value="3"></property>

</bean>

<!-- 注册监听器 -->

<!-- 开始注册监听.

需要的参数有:

acknowledge - 消息确认机制

container-type - 容器类型 default|simple

simple:SimpleMessageListenerContainer最简单的消息监听器容器,只能处理固定数量的JMS会话,且不支持事务。

default:DefaultMessageListenerContainer是一个用于异步消息监听器容器 ,且支持事务

destination-type - 目的地类型. 使用队列作为目的地.

connection-factory - 连接工厂, spring-jms使用的连接工厂,必须是spring自主创建的

不能使用三方工具创建的工程. 如: ActiveMQConnectionFactory.

-->

<jms:listener-container acknowledge="auto" container-type="default"

destination-type="queue" connection-factory="connectionFactory" >

<!-- 在监听器容器中注册某监听器对象.

destination - 设置目的地命名

ref - 指定监听器对象

-->

<jms:listener destination="test-spring" ref="myListener"/>

</jms:listener-container>

</beans>

配置消息监听器

/**

* 消息服务监听器

* @author Administrator

*

*/

@Component(value="myListener")

public class MyMessageListener implements MessageListener{

@Autowired

private UserService userService;

@Override

public void onMessage(Message message) {

//处理消息

ObjectMessage objMessage = (ObjectMessage)message;

Users user=null;

try {

user = (Users)objMessage.getObject();

} catch (JMSException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

this.userService.addUser(user);

}

配置测试类

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context =

new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-jms.xml","classpath:applicationContext-service.xml"});

context.start();

System.out.println("spring容器启动...");

System.in.read();

}

3. Jsonp介绍

(1) 什么是Jsonp?

时json的一种”使用模式”,可以让网页从别的域名(网站)那获取信息,即跨域读取数据

(2) Jsonp的优缺点是什么?

优点:兼容性好,不受同源策略限制 ,并且请求完毕后通过调用callback的方式回传结果

缺点:只支持get类型的HTTP请求,只支持跨越HTTP请求这种情况,不能解决他们之间的JavaScript调用的问题

(3) 什么是跨域?

协议+域名+端口 有一个不同就叫跨越

(4) 什么是同源策略?

同源策略 协议+域名+端口三者相同才可以互通资源

分享/讲解/扩展思考

点名提问从第一节课到最后一节课分别学到了什么,直到同学们把所有的知识点都说出来并且保证无误。

第174次(jsonp和httpclient)

学习主题:ActiveMQ

学习目标:

1 掌握jsonp跨域

2 掌握httpclient工具的使用

对应视频:

http://www.itbaizhan.cn/course/id/85.html

对应文档:

对应作业

4. 搭建跨域环境

(1) 创建两个项目作为跨域环境。

dd67d1cf115dab979e666193be656c03.png

5. 使用JsonP实现跨域

(1) 写出使用Jsonp完成跨域处理的实现步骤

Ajax请求

$(function(){

$("#but").click(function(){

$.ajax({

type:"get",

url:"http://localhost:9090/user/show",

dataType:"jsonp",

jsonp:"callback",

success:function(data){

alert(data);

var str = "";

for(i=0;i<data.length;i++){

str+= data[i].userid+" "+data[i].username+" "+data[i].userage+" ";

}

$("#show").html(str);

}

});

});

控制器接收和响应格式

@Controller

@RequestMapping("/user")

public class UserController {

@RequestMapping("/show")

@ResponseBody

public String showmsg(String callback) {

List<User> list = new ArrayList<>();

User user = new User(1, "张三", 12);

User user1 = new User(2, "赵文字", 2312);

User user2 = new User(3, "熊儿子", 1232);

list.add(user);

list.add(user1);

list.add(user2);

String json=JsonUtils.objectToJson(list);

//callback({...json格式})

return callback+"("+json+")";

}

6. MappingJacksonValue的使用

(1) 在Controller中使用MappingJacksonValue实现跨域响应

@RequestMapping("/show")

@ResponseBody

public Object showmsg(String callback) {

List<User> list = new ArrayList<>();

User user = new User(1, "张三", 12);

User user1 = new User(2, "赵文字", 2312);

User user2 = new User(3, "熊儿子", 1232);

list.add(user);

list.add(user1);

list.add(user2);

/*String json=JsonUtils.objectToJson(list);*/

//转换为json格式

MappingJacksonValue mv =new MappingJacksonValue(list);

//做callback的拼接

mv.setJsonpFunction(callback);

//callback({...json格式})

return mv;

7. 什么是HttpClient

(1) 什么是HttpClient?

HttpClient是Apache jakarta Common下的子项目

是一个支持HTTP协议的客户端编程工具包

HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java

应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net 包中已经提

供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能

还不够丰富和灵活。

8. HttpClient发送Get请求不带参数

(1) 如何通过HttpClient发送Get请求且不带参数?

* get 请求不带参数

* */

private static void doGet() throws Exception {

//创建一个HttpClient对象

CloseableHttpClient client=HttpClients.createDefault();

//创建Get请求对象 ,在请求中输入url

HttpGet httpGet = new HttpGet("http://www.baidu.com");

//发送请求,并返回响应

CloseableHttpResponse res= client.execute(httpGet);

//处理响应

//获取响应的状态码

StatusLine status=res.getStatusLine();

int code=status.getStatusCode();

System.out.println(code);

//获取响应的内容

HttpEntity body=res.getEntity();

String content = EntityUtils.toString(body, "utf-8");

System.out.println(content);

//关闭链接

client.close();

9. HttpClient发送Get请求带参数

(1) 如何通过HttpClient发送Get请求并且带参数?

/*

* get请求带参数

* */

public static void doGetParam() throws Exception {

//创建一个HttpClient对象

CloseableHttpClient client= HttpClients.createDefault();

//封装一个URI

URIBuilder uri =new URIBuilder("https://www.baidu.com/");

uri.addParameter("query", "秀儿");

//创建get请求

HttpGet get = new HttpGet(uri.toString());

//发送请求并获取结果

CloseableHttpResponse res= client.execute(get);

//获取状态码

int code= res.getStatusLine().getStatusCode();

//获取响应内容

HttpEntity entity =res.getEntity();

String content=EntityUtils.toString(entity,"utf-8");

System.out.println(content);

//关闭链接

client.close();

}

10. HttpClient发送POST请求不带参数

(1) 如何通过HttpClient发送Post请求且不带参数?

//post请求不带参数

public static void doPost() throws Exception {

//获取对象

CloseableHttpClient client= HttpClients.createDefault();

//创建post请求

HttpPost post = new HttpPost("http://localhost:8080/test/post");

//执行post

CloseableHttpResponse res=client.execute(post);

//得到状态码

int code=res.getStatusLine().getStatusCode();

System.out.println(code);

//获取响应内容

HttpEntity entit=res.getEntity();

String content=EntityUtils.toString(entit,"utf-8");

System.out.println(content);

//关闭链接

client.close();

}

11. HttpClient发送POST请求带参数

(1) 如何通过HttpClient发送Post请求并且带参数?

//post请求带参数

public static void doPostParam() throws Exception {

CloseableHttpClient client = HttpClients.createDefault();

HttpPost httpPost = new HttpPost("http://localhost:8080/test/post/param");

List<BasicNameValuePair> list = new ArrayList<>();

list.add(new BasicNameValuePair("name", "张三丰"));

list.add(new BasicNameValuePair("id", "zhangsanfeng"));

//将参数做字符串的转换

StringEntity entity = new UrlEncodedFormEntity(list,"utf-8");

//向请求中绑定参数

httpPost.setEntity(entity);

//执行post

CloseableHttpResponse res=client.execute(httpPost);

//得到状态码

int code=res.getStatusLine().getStatusCode();

System.out.println(code);

//获取响应内容

HttpEntity entit=res.getEntity();

String content=EntityUtils.toString(entit,"utf-8");

System.out.println(content);

//关闭链接

client.close();

}

12. HttpClient发送POST请求带Json格式参数

(1) 如何通过HttpClient发送Post请求并且带Json格式参数?

//post请求带Json参数

public static void doPostParamJson() throws Exception {

CloseableHttpClient client = HttpClients.createDefault();

HttpPost httpPost = new HttpPost("http://localhost:8080/test/post/param/json");

String json="{"username":"张三丰","pwd":"zhangsanfeng"}";

StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);

//向请求中绑定参数

httpPost.setEntity(entity);

//执行post

CloseableHttpResponse res=client.execute(httpPost);

//得到状态码

int code=res.getStatusLine().getStatusCode();

System.out.println(code);

//获取响应内容

HttpEntity entit=res.getEntity();

String content=EntityUtils.toString(entit,"utf-8");

System.out.println(content);

//关闭链接

client.close();

}

分享/讲解/扩展思考

点名提问从第一节课到最后一节课分别学到了什么,直到同学们把所有的知识点都说出来并且保证无误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值