Spring整合ActiveMQ

队列模式

Spring配置文件

<!-- 配置JMS连接工厂 -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <!-- ActiveMQ  broker连接地址-->
    <property name="brokerURL" value="failover:(tcp://127.0.0.1:61616)" />
    <!--解决接收消息抛出异常:javax.jms.JMSException: Failed to build body from content. 
			Serializable class not available to broke -->
    <property name="trustAllPackages" value="true" />
    <!-- 是否异步发送 -->
    <property name="useAsyncSend" value="true" />
</bean>

<!-- 定义消息队列(Queue) -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
    <!-- 设置消息队列的名字 -->
    <constructor-arg>
	<value>defaultQueueName</value>
    </constructor-arg>
</bean>
	
<!-- 配置JMS模板,Spring提供的JMS工具类,它发送、接收消息(Queue) -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="defaultDestination" ref="queueDestination" />
    <property name="pubSubDomain" value="false"/>
    <!--接收超时时间-->
    <property name="receiveTimeout" value="10000" />
    <property name="explicitQosEnabled" value="true"/>
</bean>


生成和订阅模式

Spring配置文件

DefaultMessageListenerContainer

  • subscriptionDurable:是否持久化
  • trustAllPackages:解决接收消息抛出异常:javax.jms.JMSException: Failed to build body from content. Serializable class not available to broke
<bean id="consumerListenerClient" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<!-- 开启订阅模式 -->
		<property name="pubSubDomain" value="true"/>
		<property name="destination" ref="topicDestination" />
		<property name="subscriptionDurable" value="false"/>
		<!--这里是设置接收客户端的ID,在持久化时,但这个客户端不在线时,消息就存在数据库里,直到被这个ID的客户端消费掉-->
		<!-- <property name="clientId" value="consumerClient3"/> -->
		<property name="messageListener" ref="topicMessageListener" />
		<!-- 消息应答方式
			 Session.AUTO_ACKNOWLEDGE  消息自动签收
			 Session.CLIENT_ACKNOWLEDGE  客户端调用acknowledge方法手动签收
			 Session.DUPS_OK_ACKNOWLEDGE 不必必须签收,消息可能会重复发送
		-->
		<property name="sessionAcknowledgeMode" value="1"/>
	</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值