ActiveMQ

1. 什么是ActiveMq
activeMq是apache下的一个消息中间件 是在分布式系统中完成消息的发送和接收的基础软件

ActiveMQ :是 Apache 出品,最流行的,能力强劲的开源消息总线。 ActiveMQ 是一个完全支持 JMS1.1 J2EE 1.4 规范的 JMS Provider 实现。
JMS JMS Java 消息服务( Java Message Service )应用程序接口,是一个 Java 平台中关于面向消息中间件( MOM )的 API 用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信 Java 消息服务是一个与具体平台无关的 API ,绝大多数 MOM 提供商都对 JMS 提供支持。

2. ActiveMq是干什么用的
在不同系统中进行异步调用, 不是实时的

同步就是调用就执行(实时性好)
异步调用后不知道什么时候才能执行(实时性不好)
使用场景:
比如一所大学两万多人, 1000名教师, 所有学生同时给所有老师打分

消息中间件的两种模式:
a: 点对点 , 相当于qq的私聊, 一个消息发送方, 一个消息接收方
b: 发布订阅模式: 相当于群聊, 一个消息发送方, 多个消息接收方
3. 怎么用
a. 配置消息发送方的xml文件
b. 配置消息消费方的xml文件
c. 搭建消息服务器
d. 在消息消费方编写监听器, 监听所需要接收的数据, 进行处理

应用场景

使用业务场景

传统企业处理业务存在问题

消息件在电商系统中的应用


ActiveMQ 的应用时名词解释


配置发送方的配置文件 mq.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:task="http://www.springframework.org/schema/task"
 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/task
     http://www.springframework.org/schema/task/spring-task-4.0.xsd
  http://code.alibabatech.com/schema/dubbo
  http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
  <!-- 配置连接MQ工厂, 由apache提供 -->
  <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
   <property name="brokerURL" value="tcp://192.168.200.128:61616"/>
   <property name="userName" value="admin"/>
   <property name="password" value="admin"/>
  </bean>

  <!-- 配置工厂的连接池 -->
  <bean id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
   <property name="connectionFactory" ref="activeMQConnectionFactory"/>
   <property name="maxConnections" value="20"/>
  </bean>
 
  <!-- 将上面的工厂交由spring管理 -->
  <bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
   <property name="targetConnectionFactory" ref="pooledConnectionFactoryBean"></property>
  </bean>
 
  <!-- 配置Spring jmsTemplete -->
  <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
   <property name="connectionFactory" ref="singleConnectionFactory"></property>
   <!-- 指定默认的目标地点 -->
   <property name="defaultDestinationName" value="productId"></property>
  </bean>
</beans>

配置接收方的配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 配置连接MQ工厂, 由apache提供 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.200.128:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>

<!-- 配置工厂的连接池 -->
<bean id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
<property name="connectionFactory" ref="activeMQConnectionFactory"/>
<property name="maxConnections" value="20"/>
</bean>

<!-- 将上面的工厂交由spring管理 -->
<bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="pooledConnectionFactoryBean"></property>
</bean>

<!-- 自定义的处理消息的类 -->
<bean id="customMessageListener" class="cn.itcast.core.service.message.CustomMessageListener"></bean>

<!-- 监听ActiveMQ消息服务器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!-- 1.连接MQ -->
<property name="connectionFactory" ref="singleConnectionFactory"/>
<!-- 2. 监听目标 -->
<property name="destinationName" value="productId"></property>
<!-- 3. 自定义的处理消息的类 -->
<property name="messageListener" ref="customMessageListener"></property>
</bean>
</beans>

上架方法发送消息
P roduct 项目中 ProductServiceImpl.java
@Override
public void isShow(Long[] ids) throws Exception {
Product product = new Product();
product.setIsShow(true);
if(ids != null){
for(final Long id : ids){
product.setId(id);
productDao.updateByPrimaryKeySelective(product);

//发送消息
jmsTemplate.send(new MessageCreator() {

@Override
public Message createMessage(Session session) throws JMSException {
// TODO Auto-generated method stub
return session.createTextMessage(String.valueOf(id));
}
});
}
}
}

创建自定义消息处理类
创建消息监听处理类
import javax.jms.Message;
import javax.jms.MessageListener;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import cn.itcast.core.service.SearchService;

public class CustomMessageListener implements MessageListener {
@Autowired
private SearchService searchService;

@Override
public void onMessage(Message msg) {
//强转成activeMQ的消息
ActiveMQTextMessage atm = (ActiveMQTextMessage)msg;
try {
String id = atm.getText();
//保存商品信息到solr服务器
searchService.insertProductToSolr(Long.parseLong(id));
} catch (Exception e) {
e.printStackTrace();
}
}
}

接收消息 ActiveMQ 服务器
项目中 SearchServiceImpl
@Override
public void insertProductToSolr(Long productId) throws Exception {
//将数据保存到索引库
Product p = productDao.selectByPrimaryKey(productId);

SolrInputDocument doc = new SolrInputDocument();
//1. 商品id
doc.addField("id", productId);
//2. 商品名称
doc.addField("name_ik", p.getName());
//3. 商品分类id
doc.addField("brandId", p.getBrandId());
//4. 商品图片
doc.addField("url", p.getImgUrl());

//5. 商品价格 select price from bbs_sku where product_id=442 order by price asc limit 1
SkuQuery skuQuery = new SkuQuery();
skuQuery.createCriteria().andProductIdEqualTo(productId);
skuQuery.setOrderByClause("price asc");
skuQuery.setPageNo(1);
skuQuery.setPageSize(1);
skuQuery.setFields("price");
List<Sku> list = skuDao.selectByExample(skuQuery);
doc.addField("price", list.get(0).getPrice());

solrServer.add(doc);
solrServer.commit();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值