ActiveMQ

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

ActiveMQ:是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ是一个完全支持JMS1.1J2EE 1.4规范的 JMS Provider实现。
JMSJMSJava消息服务(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>

上架方法发送消息
Product项目中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();
}


### ActiveMQ 使用指南和配置教程 ActiveMQ 是一个功能强大的消息中间件,广泛应用于分布式系统中。以下是关于 ActiveMQ 的安装、配置及使用方法的详细说明。 #### 1. 安装与启动 ActiveMQ 的安装过程相对简单。首先需要下载并解压 ActiveMQ 的二进制文件[^2]。完成解压后,进入 `bin` 目录,根据操作系统选择合适的启动脚本: ```bash # 在 Linux 或 macOS 上 ./activemq start # 在 Windows 上 activemq.bat start ``` 启动后,可以通过访问默认管理界面 `http://localhost:8161/admin/` 来验证是否成功运行。 #### 2. 配置文件详解 ActiveMQ 的核心配置文件为 `activemq.xml`,位于安装目录下的 `conf` 文件夹中[^5]。以下是一些常见的配置项: - **持久化存储**:ActiveMQ 支持多种持久化机制,如 KahaDB 和 LevelDB。默认使用 KahaDB,其配置如下: ```xml <persistenceAdapter> <kahaDB directory="${activemq.data}/kahadb"/> </persistenceAdapter> ``` 如果需要切换到 LevelDB,可以修改为: ```xml <persistenceAdapter> <levelDB directory="${activemq.data}/leveldb"/> </persistenceAdapter> ``` - **日志存储路径**:日志文件通常存储在 `%activemq安装目录%/data/kahadb` 中[^5]。 #### 3. 使用方法 ActiveMQ 提供了多种使用模式,包括点对点(Point-to-Point)和发布/订阅(Publish/Subscribe)。具体操作步骤可参考相关文档[^4]。 以下是一个简单的 Java 示例代码,展示如何向队列发送消息: ```java import javax.jms.*; import org.apache.activemq.ActiveMQConnectionFactory; public class Producer { public static void main(String[] args) throws JMSException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("TEST.QUEUE"); MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage("Hello ActiveMQ!"); producer.send(message); session.close(); connection.close(); } } ``` #### 4. 管理控制台 ActiveMQ 提供了一个基于 Web 的管理控制台,用于监控和管理消息队列及主题。可以通过以下方式访问控制台[^3]: - 打开浏览器并输入地址:`http://localhost:8161/admin/` - 默认用户名和密码为 `admin/admin` #### 5. 常见问题解决 如果在使用过程中遇到问题,可以参考以下建议: - **无法连接到服务器**:确保 ActiveMQ 服务已正确启动,并检查防火墙设置。 - **日志文件过大**:调整日志级别或清理旧的日志文件[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值