spring+jms

 <bean id="listenerContainer"  
       class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
       <property name="connectionFactory" ref="connectionFactory"></property>  
        <property name="destination" ref="destination"></property>  
       <property name="messageListener" ref="receiveMessageListener"></property>  
   </bean> 
   
   
    <bean id="receiveMessageListener"  
       class="com.demo.jms.ReceiveMessageListener"></bean> 
    
        
        <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL">
      <value>tcp://localhost:61616</value>
    </property>
   
 </bean>
  
  <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
     <property name="connectionFactory">
       <ref bean="connectionFactory"/>
     </property>
  </bean>
  
  <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg index="0">
      <value>HelloWorldQueue</value>
    </constructor-arg>
  </bean>

 /*继承MessageListener 即可接收*/

public class ReceiveMessageListener implements MessageListener {

 /*
  * (non-Javadoc)
  *
  * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
  */
 private final static Logger logger = LoggerFactory
   .getLogger(ReceiveMessageListener.class);

 public void onMessage(Message message) {
  if (message instanceof TextMessage) {
   TextMessage text = (TextMessage) message;
   try {
    logger.info("Received message:" + text.getText());
    String texts = text.getText();
    new Add().sendMsg(texts);
   } catch (JMSException e) {
    e.printStackTrace();
   }
  }

 }

你可能感兴趣的:(spring)