1. JMS and AMQP
- JMS (Java Message Service):
- ACTIVEMQ is a JMS implementation;
- AMQP (Advanced Message Queuing Protocol)
- Compatible with JMS
- RABBITMQ is the implementation of AMQP
2. RabbitMQ Introduction
Message
: Consists of the message header and the message body, the message body is opaque, and the message header consists of a series of optional attributes;
Publisher
: A client application that publishes messages to the exchanger;
Exchange
: Used to receive messages sent by the producer and route them to the queue in the server;
- There are four types: direct (default), Fanout,topic and headers;
Queue
: Used to save the message until it is sent to the consumer, is the message container;
Binding
: Used for correlation between message queues and switches;
Connection
: A network connection, such as a TCP connection;
Channel
: An independent two-way data flow channel in multiplexed connections;
Consumer
: The consumer of the message, which represents a client application that obtains the message from the message queue;
Virtual Host
: A virtual host that represents a batch of switches, message queues, and related objects; Each vhost is essentially a mini version of the RABBITMQ server;
Broker
: Represents a Message Queuing server entity;
3. RabbitMQ Integration (springboot)
- automatic configuration:
-
rabbitautoconfiguration
- automatically configured connection factory
connectionfactory
;
-
rabbitproperties
encapsulates the configuration of the RABBITMQ;
-
rabbittemplate
: Send and receive messages to RABBITMQ;
-
ampqadmin
: RABBITMQ System Management functional component;
-
@EnableRabbit
: Turn on annotation-based RABBITMQ mode;
-
@EnableRabbit
and @RabbitListener
to listen for the contents of the message queue;
Application.properties configuration file Spring.rabbitmq.host=localhostspring.rabbitmq.username= guestspring.rabbitmq.password=guest//Test Class @runwith (Springrunner.class) @SpringBootTestpublic class applicationtests {@Autowired rabbittemplate rabbittemplate; @Test public void Contextloads () {///point-to-point messages//message needs to construct itself, define the message body content and message header//Rabbittemplate.send ( Exchange, Routekey, message); Rabbittemplate.convertandsend (Exchange, Routekey, object)//Only incoming object to be sent, automatically serialized to Rabbitmq, object by default as message body map<string, object> map = new hashmap<> (); Map.put ("msg", "Hurried me to ..."); Map.put ("Data", Arrays.aslist ("777477", 232,true)); Rabbittemplate.convertandsend ("Exchange.direct", "atnoodles.news", map); }//Receive message @Test public void receive () {Object o = Rabbittemplate.receiveandconvert ("Atnoodles.news"); System.out.println (O.getclass ()); Class java.util.HashMap System.our.println (o); }}//If you need toTo automatically convert the data sent to JSON, send it out//Com.noodles.springboot.rabbitmq.config.MyAMQPConfig.javaimport Org.springframework.amqp.support.converter.jackson2jsonmessageconverter;import Org.springframework.amqp.support.converter.messageconverter;import Org.springframework.context.annotation.Bean; Import org.springframework.context.annotation.Configuration; @Configurationpublic class myamqpconfig{@Bean public Me Ssageconverter Messageconverter () {return new Jackson2jsonmessageconverter (); }}
4. Ampqadmin
- Create and delete Queue, Exchange, Binding
// 测试类@RunWith(SpringRunner.class)@SpringBootTestpublic class ApplicationTests{ @Autowired AmqpAdmin amqpAdmin; @Test public void createExchange(){ // 创建 Exchange amqpAdmin.declareExchange(new DirectExchange("amqpAdmin.exchange")); System.out.println("创建完成..."); // 创建 Queue amqpAdmin.declareQueue(new Queue("amqpAdmin.queue", true)); }}
Resources:
- Springboot and messages
- RabbitMQ installation (Windows)
- MAC installation RABBITMQ
- Start/Stop RABBITMQ
Springboot and Messages (RabbitMQ)