ACTIVEMQ and Spring Integration

Source: Internet
Author: User

ActiveMQ use of standard producer and consumer models
There are two kinds of data structures Queue, Topic
1 , queue queues, producers produce a message that can only be consumed by one consumer
2 , Topic topics, producers produce a message that can be consumed by multiple consumers

 

default TCP connection ActiveMQ Port 61616

import jar

2. Write the configuration producer
Configuring the activemq connection Factory

<!--ActiveMQ Connection Factory -    <!--A connectionfactory that can actually generate connection, provided by the corresponding JMS service vendor -    <!--If you are connected to a network: tcp://ip:61616; network not connected: tcp://localhost:61616 and username, password -    <amq:connectionfactoryID= "Amqconnectionfactory"Brokerurl= "tcp://localhost:61616"UserName= "Admin"Password= "Admin"  />    <!--Spring Caching Connection Factory -    <!--Spring is used to manage the connectionfactory of real ConnectionFactory -      <BeanID= "Mqconnectionfactory"class= "Org.springframework.jms.connection.CachingConnectionFactor <!--target connectionfactory corresponds to the real can generate JMS Connection ConnectionFactory--<property name= "targetconnectionfactory "ref= "Amqconnectionfactory"></ Property>        <!--Ditto, similarly -        <!--<constructor-arg ref= "Amqconnectionfactory"/> -        <!--Session Cache Count -        < Propertyname= "Sessioncachesize"value= "+" />    </Bean>         <!--Spring jmstemplate message producer start -    <!--define the queue type for jmstemplate -    <BeanID= "Jmsqueuetemplate"class= "Org.springframework.jms.core.JmsTemplate">        <!--This connectionfactory corresponds to the ConnectionFactory object that we have defined spring provides. -          <Constructor-argref= "Mqconnectionfactory" />        <!--Non-pub/sub model (publish/subscribe), which is the queue mode -        < Propertyname= "Pubsubdomain"value= "false" />    </Bean>    <!--defining the topic type of Jmstemplate -    <BeanID= "Jmstopictemplate"class= "Org.springframework.jms.core.JmsTemplate">         <!--This connectionfactory corresponds to the ConnectionFactory object that we have defined spring provides. -          <Constructor-argref= "Mqconnectionfactory" />        <!--Pub/sub Model (Publish/subscribe) -        < Propertyname= "Pubsubdomain"value= "true" />    </Bean><!--defining a Queue listener -<Jms:listener-containerDestination-type= "queue"Container-type= "Default"connection-factory= "ConnectionFactory"Acknowledge= "Auto"><!--The default registered bean name, which should be the first letter of the class name lowercase -<Jms:listenerDestination= "Bos_sms"ref= "Topicconsumer"/></Jms:listener-container>

The queue mode producer template, topic mode, is different from the injection class:

@Service Public classQueuesender {//Inject Jmstemplate@Autowired @Qualifier ("Jmsqueuetemplate")    Privatejmstemplate jmstemplate; Create a Send method, pass in the queue name, and a message to send Public voidSend (String queuename,FinalString message) {Jmstemplate.send (QueueName,NewMessagecreator () { PublicMessage CreateMessage (Session session)throwsjmsexception {returnsession.createtextmessage (message);    }        }); }}

Queue consumer Templates

@Service  Public class Implements MessageListener {    publicvoid  onMessage (Message message) {        = ( textmessage) message;         Try {            System.out                    . println ("Consumer queueconsumer Get Message:" + Textmessage.gettext ());         Catch (jmsexception e) {            e.printstacktrace ();     }}}

Producer Testing

@RunWith (Springjunit4classrunner.  Class= "Classpath:applicationcontext-mq.xml")publicclass  producertest {    @Autowired    private  queuesender Queuesender;    @Autowired    private  topicsender Topicsender;    @Test    publicvoid  testsendmessage () {        queuesender.send ("Spring_ Queue "," I am a producer ");}    }

Consumer Testing

@RunWith (Springjunit4classrunner.  Class= "Classpath:applicationcontext-mq-consumer.xml")publicclass  consumertest {    @Test    publicvoid  testconsumermessage () {        while (true) {            //  JUnit exits to prevent the process from dying         }     }}

Results the corresponding tester will output the corresponding message after running the producer test.

ACTIVEMQ and Spring Integration

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.