ActiveMQ(实战三) 与spring的集成

本文详细介绍了使用Spring JMS实现消息生产者和消费者的步骤,包括接口定义、实现类实现、以及配置文件设置。通过实例展示了如何在Spring框架下进行消息的发送与接收。

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


 一 ,消息的生产者

1. 接口

package com.xiu.mq.sender;
public interface SendService {

void sendMessage(String myMessage);

}


2.实现类

package com.xiu.mq.sender;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class SendServiceImpl implements SendService {
private JmsTemplate sendTemplate;

public JmsTemplate getSendTemplate() {
return sendTemplate;
}

public void setSendTemplate(JmsTemplate sendTemplate) {
this.sendTemplate = sendTemplate;
}

@Override
public void sendMessage(final String message) {
MessageCreator messageCreator = new MessageCreator() {
public Message createMessage(Session session) {
TextMessage msg = null;
try {
msg = session.createTextMessage(message);// 消息正文
msg.setStringProperty("mykey", "ALL_GOODS_CHANGED_MESSAGE");// 定制消息头
} catch (Exception e) {
}
return msg;
}
};
sendTemplate.send(messageCreator);
}

}

二,短信的消费者


package com.xiu.mq.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xiu.mq.sender.SendService;
public class BootSender {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-jms.xml");
SendService sender = (SendService) context.getBean("sendService");
String myMessage = "hello world";
sender.sendMessage(myMessage);
System.out.println("-----send over-----");

}
}

package com.xiu.mq.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xiu.mq.sender.SendService;
public class BootReceiver {


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


System.out.println("-----receive over-----");


}
}



三   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.0.0.67:61616"></property>
<property name="sendTimeout" value="1000"></property>
</bean>
</property>
</bean>
<!-- 消息的目的地,消息队列的名称配置 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="zhuguanghe"></constructor-arg>
</bean>
<!-- 消息发送的配置 -->
<bean id="sendTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="destination"></property>
<property name="explicitQosEnabled" value="true"></property>
<property name="deliveryMode" value="2"></property>
</bean>


<bean id="sendService" class="com.xiu.mq.sender.SendServiceImpl">
<property name="sendTemplate" ref="sendTemplate"></property>
</bean>


<!-- 消息接受配置 -->
<bean id="demoMsgListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="com.xiu.mq.receiver.MyMessageListenImpl"></bean>
</constructor-arg>
<property name="defaultListenerMethod" value="handleMyMessage"></property>
<property name="messageConverter">
<null />
</property>
</bean>
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="concurrentConsumers" value="10"></property>
<property name="destination" ref="destination"></property>
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="messageListener" ref="demoMsgListener"></property>
</bean>

</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值