ActiveMQ 八 (实战三)

本文介绍了一个基于ActiveMQ的消息队列系统实现案例,通过Spring框架整合ActiveMQ,实现了消息的发送与接收,并展示了如何配置Spring来管理ActiveMQ连接及消息目的地。

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


 一 , 消息的发送者(产生者)

package com.xiu.jms.consumer;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class HelloSender {

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:spring-jms.xml" });


JmsTemplate template = (JmsTemplate) applicationContext
.getBean("jmsTemplate");
Destination destination = (Destination) applicationContext
.getBean("destination");
template.send(destination, new MessageCreator() {


@Override
public Message createMessage(Session session) throws JMSException {
return session
.createTextMessage("发送消息:hello world activeMQ....");

}
});

System.out.println("成功发送了一条JMS消息");
}

}


二, 消息的接受者(消费者)

package com.xiu.jms.consumer;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
public class ProxyJMSConsumer implements MessageListener{
private ProxyJMSConsumer() {
}
private JmsTemplate jmsTemplate;


public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
@Override
public void onMessage(Message arg0) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:spring-jms.xml" });
Destination destination = (Destination) applicationContext
.getBean("destination");
while (true) {
TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);

if (null != txtmsg) {


System.out.println("[DB Proxy]" + txtmsg);
try {
System.out.println("[DB Proxy]收到的消息内容为" + txtmsg.getText());
} catch (JMSException e) {
}


}
}

}

}


三   spring-jms.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="byName">
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://10.188.53.76:61616">
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestinationName" value="subject"></property>
区别采用的模式为false是p2p,true为topic
<property name="pubSubDomain" value="true"></property>
</bean>
发送消息的目的地(一个队列)
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="subject" />
</bean>
消息监听器
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="concurrentConsumers" value="10"></property>
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="destinationName" value="subject"></property>
<property name="messageListener" ref="messageReceiver"></property>
<property name="pubSubNoLocal" value="false"></property>
</bean>
<bean id="messageReceiver" class="com.xiu.jms.consumer.ProxyJMSConsumer">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>

</beans>


四,测试类

package com.xiu.jms.consumer;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class JMSTest {

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:spring-jms.xml" });

ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext.getBean("messageReceiver");
System.out.println("初始化消息消费者");
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值