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)