ActiveQM安装以及Spring配置

本文介绍了ActiveMQ的安装步骤及与Spring框架的集成方法。详细讲述了如何通过命令行工具完成ActiveMQ的安装,并展示了如何配置Spring来实现与ActiveMQ的消息交互。此外,还提供了使用Java代码动态发送消息的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、activemq安装
    #tar -zxvf apache-activemq-5.8.0-bin.tar.gz
    #cd /usr/local/activemq/bin
    #chmod 755 activemq
    #./activemq start   #启动
    #./activemq start > log #启动,输出到日志
    #nohup ./activemq start > log #后台启动
    #netstat -ano | grep 61616
2、activemq与spring集成
    配置文件如下:
   
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${activemq.server}" />
<property name="userName" value="${activemq.userName}" />
<property name="password" value="${activemq.password}" />
<property name="useAsyncSend" value="true" />
<property name="clientID" value="ActiveMQ" />
</bean>
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="${activemq.topicName}"></constructor-arg>
</bean>
 
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="defaultDestination" ref="destination"></property>
</bean>
 
 
<bean id="messageListener" class="com.cn.org.sync.mq.MessageHandler"></bean>
 
 
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer"
lazy-init="false">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destination" ref="destination"></property>
<property name="messageListener" ref="messageListener"></property>
</bean>
 
<!-- activeMq -->
</beans>
    通过 messageListener监听消息,收到后进行处理,JmsTemplate是spring提供的可以send与recive消息。若用动态代码方式发送,如下:
    
 
@Service
public class MqService{
private Logger log = Logger.getLogger(getClass());
/**
* 批量发送消息
* @param msgList
*/
public void sendMsgBatch(List<String> msgList) {
if(msgList==null || msgList.isEmpty()) return;
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
Connection connection = null;
Session session;
Destination destination;
try {
connectionFactory.setBrokerURL(GitvConstants.MQ_URL);
connectionFactory.setUserName(GitvConstants.MQ_USERNAME);
connectionFactory.setPassword(GitvConstants.MQ_PWD);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE, Session.CLIENT_ACKNOWLEDGE);
destination = session.createTopic(GitvConstants.MQ_QUEUE_NAME);
MessageProducer msgProducer = session.createProducer(destination);
for(String str:msgList){
msgProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage textMsg = session.createTextMessage("");
msgProducer.send(textMsg);
log.info("发送消息OK:msg="+str);
}
msgProducer.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}
    配置文件如下:
    
activemq.server=${mvn.activemq.server}
activemq.topicName=DATA.TO.AUTH
activemq.userName=active
activemq.password=production-password
3、activemq集群
    集群主要有两种:master-slave 和 broker cluster
    master-slave主要包括三类:Pure Master Slave 、Shared File System Master Slave 和 JDBC Master Slave
   
参考网页:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值