`

利用Spring中的JmsTemplate来进行ActiveMQ的整合

阅读更多

JmsTemplate就是Spring用来解决JMS冗长重复代码的方案,它可以创建连接,获取会话,发送和接收消息,使用它之后,使得你可以专注于构建要发送的消息及处理收到的消息。

 

<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">        <property name="brokerURL">
      	  <value>failover://(tcp://192.168.1.147:61616?tcpNoDelay=true,tcp://192.168.1.157:61616?tcpNoDelay=true)</value>
       </property>
       <property name="useAsyncSend" value="true"></property>
</bean>

  <!-- 配置connectionFactory -->
<bean id="jmsFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
	<property name="targetConnectionFactory" ref="connectionFactory"></property>
	<property name="sessionCacheSize" value="100" />
</bean>
  
 <!-- 发送消息的目的地(一个队列) -->  
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  
        <!-- Set the Queue Name -->  
        <constructor-arg index="0" value="goodsQueue?consumer.prefetchSize=100"/>
    </bean>  
    
    <bean id="destination_insert" class="org.apache.activemq.command.ActiveMQQueue">  
        <!-- Set the Queue Name -->  
        <constructor-arg index="0" value="insertQueue?consumer.prefetchSize=10"/>
    </bean>  
    
     <bean id="destination_img" class="org.apache.activemq.command.ActiveMQQueue">  
        <!-- Set the Queue Name -->  
        <constructor-arg index="0" value="imgQueue?consumer.prefetchSize=1"/>
    </bean>  
    
    <!-- 配置JMS模版 -->  
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
        <property name="connectionFactory"  ref="jmsFactory"/>  
        <property name="deliveryPersistent" value="true" />  
    </bean> 
    
    <bean id="myMessageProducer" class="com.naomi.spider.common.MyMessageProducer" >
      	<property name="jmsTemplate" ref="jmsTemplate" />
      	<property name="destination" ref="destination" />
    </bean>

   
    <!-- 异步接受 -->
    <bean id="jmsContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory" />
        <property name="destination" ref="destination" />
        <property name="messageListener" ref="myMessageConsumer" />
        <property name="sessionTransacted" value="false"/>
  	<property name="concurrentConsumers" value="20"/>
  	<property name="maxConcurrentConsumers" value="100"/>
	<property name="receiveTimeout" value="5000"/>
	<property name="idleTaskExecutionLimit" value="10"/>
	<property name="idleConsumerLimit" value="5"/>
</bean>
    

   发送消息

 

   

template.send((javax.jms.Destination) destination, new MessageCreator(){  
                public Message createMessage(Session session) throws JMSException {  
                    return session.createTextMessage("hello");  
                }  

接受消息

 

jmsTemplate接收消息十分的简单,只需要调用template.receive()方法,receive方法是同步的,默认情况下,对receive()方法的调用会造成阻塞,知道消息到达目标----如果必要,永远等下去。为了避免对消息内容等待,可以配置jmsTemplate时,通过设置receiveTimeout属性来指定接收消息超时间。template.receive()会从默认目标接收消息,如果你希望指定一个目标,可以传一个目标。如:template.receive("myQueue").

同步接收消息并不是spring唯一的选择,消息监听器可以实现异步。activemq提供了messageListener接口,来为我们实现异步的接收消息。这样我们就不需要再手动的调用receive方法获取信息,当有消息发送到队列中时,就会自动的接受消息。我采用的是多线程异步的方式,效率要比同步的好一些。

 

 

自动将消息转化为Java对象

Spring通过MessageConverter接口提供了对消息转换的支持。此时,发送和接收消息要换成template.convertAndSend(message);template.receiveAndConvert();

可是jmsTemplate如何知道消息转换器呢?需要在配置jmsTemplate的时候,加上messageConverter属性。

<property name="messageConverter" ref="messageObj"></property> 

 

分享到:
评论

相关推荐

    ActiveMQ+Spring+Maven Demo

    使用spring jmstemplate写的activemq小demo,浅显易懂。工程下载导入可用(maven 工程) activemq 可直接apache官网下载 传送门http://activemq.apache.org/download.html

    Spring+JMS+ActiveMQ+Tomcat实现消息服务的demo

    基于Spring+JMS+ActiveMQ+Tomcat,我使用的版本情况如下所示:Spring 3.2.0,ActiveMQ 5.4.3,Tomcat 6.0.43。本例通过详细的说明和注释,...Spring对JMS提供了很好的支持,可以通过JmsTemplate来方便地实现消息服务。

    Spring JMS异步发收消息 ActiveMQ

    JMS(使用消息中介:ActiveMQ) ...JmsTemplate是Spring消除冗长和重复JMS代码的解决方案。JmsTemplate可以创建连接,获取会话,以及发送和接收消息。http://blog.csdn.net/facepp/archive/2008/11/26/3374151.aspx

    Spring+JMS+ActiveMQ+Tomcat实现消息服务_服务器应用

    基于Spring+JMS+ActiveMQ+Tomcat,我使用的版本情况如下所示: ...Spring对JMS提供了很好的支持,可以通过JmsTemplate来方便地实现消息服务。这里,我们的消息服务不涉及事务管理。下面简单说明实例。

    Spring JMSTemplate 与 JMS 原生API比较

    NULL 博文链接:https://holdbelief.iteye.com/blog/1491604

    JMS与Spring之一(用JmsTemplate同步收发消息)

    JMS与Spring之一(用JmsTemplate同步收发消息)

    spring-jms:Spring JmsTemplate演示

    JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsQueueTemplate"); Destination destination = (Destination) context.getBean("defaultQueueDestination"); JmsUtil.sendMessage(jmsTemplate, de

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Springboot ActiveMQ 集成.rar

    Springboot ActiveMQ 集成,该项目中包含手动创建连接,以及使用Spring提供的支持,JmsTemplate的使用方式。

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    使用Jmstemplate向队列中发送数据

    NULL 博文链接:https://13813962825.iteye.com/blog/2066980

    springjms的demo

    Spring对JMS提供了很好的支持,可以通过JmsTemplate来方便地实现消息服务。本例通过activeMQ服务器模拟了消息的发送与接收。需要注意的是,activeMQ的运行依赖jdk的环境,而且对jdk的版本也有要求,我用的是jdk1.6+...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring+JMS+消息处理

    Spring+JMS+消息处理

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    SpringBoot 整合 JMSTemplate的示例代码

    主要介绍了SpringBoot 整合 JMSTemplate的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    sample-management-system:示例客户管理系统和邮件应用程序,从消息系统读取和写入

    介绍示例客户管理系统和邮件应用程序,从邮件系统读取和写入。 技术栈大纲1. Java EE 8:Bean 验证使用 JSR(349) 2. Spring MVC 3.2.3 3. RESTful/JSON 服务使用 Spring MVC 3.2.3 4.... 将 spring(JMSTemplate) 与 Jav

    SPRING API 2.0.CHM

    JmsTemplate102 JmsTransactionManager JmsTransactionManager102 JmsUtils JmxAttributeSource JmxException JmxMetadataUtils JmxUtils JndiAccessor JndiCallback JndiDataSourceLookup ...

    Spring系列学习之SpringMessaging消息支持

    SpringFramework为与消息传递系统的集成提供了广泛的支持,从使用JmsTemplate简化JMSAPI的使用到异步接收消息的完整基础结构。SpringAMQP为高级消息队列协议提供了类似的功能集。SpringBoot还为RabbitTemplate和...

Global site tag (gtag.js) - Google Analytics