Springboot diary--MQ Message Queue consolidation (ii)

Source: Internet
Author: User
Tags message queue serialization

After setting up the environment based on the first article, we continue this article to show you how to use RABBITMQ in Springboot.

1), unicast: After adding a good pom file and a custom configuration, see:

    @Autowired    rabbittemplate rabbittemplate;    @Test    publicvoid  contextloads () {        ///  object is sent by default Java serialization, Parameters: Exchange,routingkey, message        rabbittemplate.convertandsend ("Exchange.direct", "Iceoooodin.news", "瓦尔克莉" );    }

To see if the message we sent was successful:

, the successful acquisition of ~.

Similarly, use code to get directly:

    @Test    publicvoid  receive () {        //  accept Data        Object o = Rabbittemplate.receiveandconvert ("iceoooodin.news");        System.out.println (O.getclass ());        System.out.println (o);    }

In addition, we also send maps, objects, and so on, in addition to STR types, such as:

  @Test  public  void   Contextloads () {map  <string, object> map = new  hashmap<> ();        Map.put ( msg, "First Data"  "Data", Arrays.aslist ("HelloWorld", 123, true  ));         //  object is sent by default Java serialization     Rabbittemplate.convertandsend ("Exchange.direct", "Iceoooodin.news" 
@Test      Public void contextloads () {        ///  object is sent by default Java serialization        to new book ("Gold Bottle M", "em ..." ));    }
 Public classBook {PrivateString BookName; PrivateString author; @Override PublicString toString () {return"book{" + "bookname=" "+ bookname +" \ "+", author= ' + author + ' \ ' + '} '; }     PublicBook (string bookname, string author) { This. BookName =BookName;  This. Author =author; }     PublicBook () {} PublicString Getbookname () {returnBookName; }     Public voidsetbookname (String bookname) { This. BookName =BookName; }     PublicString Getauthor () {returnauthor; }     Public voidSetauthor (String author) { This. Author =author; }}
Book.java

2), then we look at the broadcast, that is, send a bunch of messages is how to handle:

@Test      Public void Sendmst () {        rabbittemplate.convertandsend(new book ("Red House", "Grass"));    

If you are sending a broadcast, you can see that the message is sent separately to all of the matching message queues:

3), we learn to send and receive, and then see how to create Exchange or queue bar, we create and bind to write together, according to the need to dismantle themselves:

@Autowired amqpadmin amqpadmin; This amqpadmin is used to manage QP, can be created, deleted and so on; @Test Public voidCreatexchange () {//Create ExchangeAmqpadmin.declareexchange (NewDirectexchange ("Amqpadmin.exchange")); System.out.println ("Create Exchange Complete"); //Create a queueAmqpadmin.declarequeue (NewQueue ("Amqpadmin.queue",true));
System.out.println ("Create queue complete"); //bindingAmqpadmin.declarebinding (NewBinding ("Amqpadmin.queue", Binding.destinationtype.QUEUE, "Amqpadmin.exchange", "Amqp.haha",NULL)); }

The results are too lazy to write, we have to look at their own experiment to know, to the Web management interface to see if there are correct additions and bindings ~

Springboot diary--MQ Message Queue consolidation (ii)

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.