Spring Integrated ACTIVEMQ

Source: Internet
Author: User

First, the XML configuration


<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= " Http://www.springframework.org/schema/context "xmlns:util=" Http://www.springframework.org/schema/util "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- Beans-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/s Chema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/util Http://www.springfra Mework.org/schema/util/spring-util-3.0.xsd "> <!--configuration ConnectionFactory-<bean id=" Mqfactory "C lass= "Org.apache.activemq.spring.ActiveMQConnectionFactory" > <property name= "Brokerurl" > <value& Gt;${mq_url}</value> &LT;/PRoperty> <property name= "Sendtimeout" value= "$" ></property> <property name= "Closetimeout" value= "$" ></property> <property name= "Useasyncsend" value= "true" ></property></bean> < Bean id= "jmsfactory" class= "Org.springframework.jms.connection.CachingConnectionFactory" > <constructor-arg    ref= "Mqfactory"/> <property name= "Exceptionlistener" ref= "Messageexceptionlistener"/><!--error Listening-- <property name= "Reconnectonexception" value= "true"/> <property name= "sessioncachesize" value= "[/&GT;&L]" t;! --Cached messages-</bean> <!--Spring JMS Template---<bean id= "jmstemplate" class= "com.iflashbuy.b Ase.queue.MyJmsTemplate "> <property name=" connectionfactory "> <ref local=" jmsfactory "/&          Gt </property> <!--default Queue--<property name= "Defaultdestinationname" value= "Default_queue"/> <!--default queue--&GT <property name= "DeliveryMode" value= "1" ></property> <!--default not persisted--<property name= "Deliverype Rsistent "value=" false "></property> <property name=" explicitqosenabled "value=" true "></property&        Gt <property name= "timeToLive" value= "></property>" </bean> </beans>



Second, integrated jmstemplate


Package Com.iflashbuy.base.queue;import Javax.jms.connectionfactory;import Javax.jms.jmsexception;import Javax.jms.message;import Javax.jms.messageproducer;import Javax.jms.session;import Org.springframework.jms.core.jmstemplate;public class Myjmstemplate  extends Jmstemplate {   public Myjmstemplate () {    super ();  }  Public myjmstemplate (ConnectionFactory connectionfactory) {    super (connectionfactory);  }  public void Dosend (messageproducer producer, message message) throws JMSException {    if (isexplicitqosenabled ()) {
   producer.send (Message, Getdeliverymode (), getpriority (), gettimetolive ());    } else {      producer.send (message);    }  }  Public Session getsession () {    try {return getconnectionfactory (). CreateConnection (). CreateSession (False, Session.auto_acknowledge);} catch (JMSException e) {e.printstacktrace (); return null;}  } }



Third, production data


Session session = Jmstemplate.getsession ();D estination msgdes = destinationfactory.createdestination ( Queueconstant.save_queue); MessageProducer producer = Session.createproducer (msgdes); ObjectMessage message = Session.createobjectmessage ( Orderqueue);//Set Persistent queue Producer.setdeliverymode (deliverymode.persistent);p roducer.send (message);



Four, consumer terminal configuration


<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= " Http://www.springframework.org/schema/context "xmlns:util=" Http://www.springframework.org/schema/util "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- Beans-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/s Chema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/util Http://www.springfra Mework.org/schema/util/spring-util-3.0.xsd "> <!--configuration ConnectionFactory-<bean id=" Mqfactory "C lass= "Org.apache.activemq.spring.ActiveMQConnectionFactory" > <property name= "Brokerurl" > <value& Gt;${mq_url}</value> &LT;/PRoperty> <property name= "Sendtimeout" value= "$" ></property> <property name= "Closetimeout" value= "<property" ></property> name= "Useasyncsend" value= "true" ></property></bean> <b EAN id= "operate" class= "Org.apache.activemq.command.ActiveMQQueue" > <constructor-arg value= "Save_queue,canc El_queue "/> </bean> <!--message listening container--<bean id=" Listenercontainer "class=" or G.springframework.jms.listener.defaultmessagelistenercontainer "> <property name=" connectionFactory "ref=" Mqfactory "/> <property name=" destination "ref=" operate "/> <property name=" Messagelisten Er "ref=" messagereceiver "/>//Data consumption in Messsagereceiverbean </bean> </beans>


V. Messagereceiver beans inherit messagelistener consumption data

@Component ("Messagereceiver") public class Messagereceiver implements MessageListener {  private static final Logger Log = Loggerfactory.getlogger (Messagereceiver.class); @Autowiredprivate datasnyservice Datasynservice;    public void OnMessage (Message message) {      log.info ("Get queuejob start");        try {        if (message instanceof objectmessage) {Serializable m= ((objectmessage) message). GetObject (); Log.info ("----- -------------------------------"); if (M instanceof orderqueue) {orderqueue job = (orderqueue) m;} Log.info ("------------------------------------");}        Log.info ("Get Queuejob End");} catch (Exception e) {e.printstacktrace ();}    }  }



Take out data for consumption in the Onmesage method.




Spring Integrated ACTIVEMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.