Spring Integrated RABBITMQ configuration file (producer and consumer)

Source: Internet
Author: User
Tags rabbitmq

1, first introduce the configuration file Org.springframework.amqp, as follows:

<Dependency>            <groupId>Org.springframework.amqp</groupId>            <Artifactid>Spring-rabbit</Artifactid>            <version>1.7.1.RELEASE</version>        </Dependency>

2, Preparation: Install the RABBITMQ, and add the configuration file rabbit.properties content in the project as follows:

rmq.ip=192.188.113.114   Rmq.port=5672rmq.producer.num=20rmq.manager.user=adminrmq.manager.password=admin

3, configure Spring-rabbitmq.xml, the content is as follows:

<!--Public Part -<!--Creating a Connection class connection installed RABBITMQ -<BeanID= "ConnectionFactory"class= "Org.springframework.amqp.rabbit.connection.CachingConnectionFactory">    <Constructor-argvalue= "localhost" />        < Propertyname= "username"value= "${rmq.manager.user}" />        < Propertyname= "Password"value= "${rmq.manager.password}" />       < Propertyname= "Host"value= "${rmq.ip}" />       < Propertyname= "Port"value= "${rmq.port}" />   </Bean><Rabbit:adminconnection-factory= "ConnectionFactory"/><!--defines Message Queuing, durable: persistent, exclusive: Only private queues that the creator can use, automatically deleted after disconnection, Auto_delete: Automatically delete queues when all consumer client connections are disconnected -<Rabbit:queuename= "Spittle.alert.queue.1"ID= "Queue_1"Durable= "true"Auto-delete= "false"Exclusive= "false" /><Rabbit:queuename= "Spittle.alert.queue.2"ID= "Queue_2"Durable= "true"Auto-delete= "false"Exclusive= "false" /><Rabbit:queuename= "Spittle.alert.queue.3"ID= "Queue_3"Durable= "true"Auto-delete= "false"Exclusive= "false" /><!--binding queue, RABBITMQ ExchangeType commonly used three modes: Direct,fanout,topic three kinds, we use direct mode, that is rabbit:direct-exchange tag, direct switch is very simple , if it is a direct type, the Routingkey in the message is compared to the bindingkey in all the binding associated with the exchange, and if it is equal, it is sent to the queue corresponding to the binding. There is one place to note: If the specified exchange is not found, an error is given. But routing key can not find the words, will not error, this message will be lost directly, so be careful here -<Rabbit:direct-exchangeID= "Spittle.fanout"name= "Spittle.fanout"Durable= "true"Auto-delete= "false">    <rabbit:bindings>        <rabbit:bindingQueue= "Spittle.alert.queue.1"Key= "{alert.queue.1}"></rabbit:binding>        <rabbit:bindingQueue= "Spittle.alert.queue.2"Key= "{alert.queue.2}"></rabbit:binding>        <rabbit:bindingQueue= "Spittle.alert.queue.3"Key= "{alert.queue.3}"></rabbit:binding>    </rabbit:bindings></Rabbit:fanout-exchange><!--Producer Part -<!--the producer class that sends the message, which is the producer -<BeanID= "Msgproducer"class= "Com.asdf.sdf.ClassA">    <!--value is the Routingkey in producer, which is the queue name, which must be the same as the key in the rabbit:bindings tag above -    < Propertyname= "QueueName"value= "{alert.queue.1}"/></Bean><!--Spring AMQP defaults to one of Jackson's plugins to convert producer-produced data to JSON into Message Queuing, as Fastjson is faster than Jackson, replacing it with an implementation of Fastjson -<BeanID= "Jsonmessageconverter"class= "Com.jy.utils.FastJsonMessageConverter"></Bean><!--or configure Jackson -<!--<bean id= "Jsonmessageconverter" class= " Org.springframework.amqp.support.converter.Jackson2JsonMessageConverter "/> -<rabbit:templateExchange= "Test-exchange"ID= "Rabbittemplate"connection-factory= "ConnectionFactory"Message-converter= "Jsonmessageconverter" /><!--Consumer part -<!--Custom Interface Classes -<BeanID= "Testhandler"class= "Com.rabbit.TestHandler"></Bean><!--the proxy class for listening for messages Messagelisteneradapter -<BeanID= "Testqueuelisteneradapter"class= "Org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter" ><!--class Name
     <Constructor-argref= "Testhandler" /><!--method Name--
     < Propertyname= "Defaultlistenermethod"value= "Handlertest"></ Property>< Propertyname= "Messageconverter"ref= "Jsonmessageconverter"></ Property></Bean><!--Configure the Listener acknowledeg= "manual" setting for manual answer. When the message processing fails: It will continue to resend until the message processing succeeds, the listener container acknowledge= "Auto" concurrency= "30" set the number of sends, Send up to 30 times -<Rabbit:listener-containerconnection-factory= "ConnectionFactory"Acknowledge= "Auto"Concurrency= " the"> <Rabbit:listenerQueues= "Spittle.alert.queue.1"ref= "Testqueuelisteneradapter" />
     <rabbit:listener queues= "spittle.alert.queue.2" ref= "Testqueuelisteneradapter"/>
<rabbit:listener queues= "spittle.alert.queue.2" ref= "Testqueuelisteneradapter"/>
</rabbit:listener-container>

4, producer (send-side) code:

@ResourcePrivaterabbittemplate rabbittemplate; PrivateString QueueName;  Public voidsendMessage (commonmessage msg) {Try{logger.error ("Send Message Start");                   System.out.println (Rabbittemplate.getconnectionfactory (). GetHost ()); //send information queuename switch, that is, the above Routingkey Msg.getsource () is Test_keyRabbittemplate.convertandsend (Queuename,msg.getsource (), MSG); Logger.error ("Send Message End"); } Catch(Exception e) {e.printstacktrace (); }        }     Public voidsetqueuename (String queuename) { This. QueueName =QueueName; }

5, Consumer code: Testhandler Class

 Public class Testhandler  {    @Override    publicvoid  handlertest (commonmessage commonmessage) {        System.out.println (new  String (Message.getbody ()));}    }

This article goes from:

81065892

Https://www.cnblogs.com/LipeiNet/p/6079427.html

Spring Integrated RABBITMQ configuration file (producer and consumer)

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.