L JAVA Platform Asynchronous message module
The Java Platform Asynchronous message module, which is a message sending and processing package for RABBITMQ, contains the configuration, sending, receiving, failure retry, logging, and so on, which is divided into 4 parts:
1) RABBITMQ Access package: JAMQP (Jar package)
2) Message module Common object, configuration read and interface definition: JMSG (Jar package)
3) Message sending end: Jmsg-client (Jar package)
4) Message receiving end: Jmsg-server (War package)
L RabbitMQ Introduction
MQ is a typical representative of the consumer-producer model, where one end writes messages continuously to the message queue, while the other end can read or subscribe to messages in the queue. RABBITMQ is a complete, reusable enterprise messaging system based on AMQP. He follows the Mozilla Public License open source agreement. RABBITMQ is a popular open source Message Queuing system, developed in Erlang language. RABBITMQ is the standard implementation of AMQP.
The structure diagram of the RABBITMQ is as follows:
Exchange: A message switch that specifies what rules the message is routed to and to which queue.
Alternate Exchange: A message that is sent to an exchange fails when it is routed to that exchange.
Dead Letter Exchange: Badmail exchange, which sends messages in a queue that is longer than a certain amount of time to that exchange.
Queue: A message queue carrier in which each message is put into one or more queues.
Binding: Bind, which is the role of binding exchange and queue according to routing rules.
Routing key: The routing keyword, exchange messages are delivered based on this keyword.
Channel: The message channels, in each connection of the client, multiple channels can be established, each channel represents a session task.
L RabbitMQ Package
1) Rabbitconfig: All RABBITMQ related configuration
2) RABBITCONNECTPOOL:RABBITMQ Connection pool, manage all connections to RABBITMQ
3) RABBITPROXY:RABBITMQ connection package, including
4) Rabbitsendproxy: A connection dedicated to sending messages, inherited from Rabbitproxy
5) Rabbitreceiveproxy: A connection dedicated to receiving messages, inheriting from Rabbitproxy
6) Rabbitreceiverdispatcher: Message Dispatcher management class for message
7) message sent: From the connection pool Rabbitconnectpool get out of Rabbitsendproxy, call the Send method to send a message, return to get the rabbitsendproxy. Send failed messages log the ground file, and retry 3 times after the polling thread gets the message
8) Message receiving: When the service starts, it registers the queue to be monitored to rabbitreceiverdispatcher, each listener corresponds to a thread and the thread pool that processes the message, which is processed in parallel through the thread pool after getting the message from RABBITMQ to the push.
L Internal Process
n message sending logic
n message receive logic
L Configuration Instructions
n RabbitMQ Configuration table
n Message Configuration table
1) MessageName: Message name, unique in Messageconfig table
2) URL: Business processing HTTP API address for messages
3) Priority: Message prioritization, must be written p1,p2,p3 .... P8,P9, Routekey for RABBITMQ, final routekey when sending messages = Messagename.priority
L How to use
n Send Message
Message sending method definition:
/**
* Send Message
* @param transferobject
* Message Principal Object
* @param CustomTag
* Custom Message Labels
* @param messagename
* Message Name
*/
void Send (Object transferobject, String CustomTag, string messagename);
To configure the message in the Message configuration table, refer to jmsg and jmsg-client two jar packages and invoke the following method:
public void sendmsg () {
Rabbitmqsender sender = new Rabbitmqsender ();
Sender.send (New Object (), "tag", "MessageName");
}
Note: After the transmitted object (Transferobject) is JSON serialized, the size cannot exceed 64K, otherwise an exception will be thrown
n Receiving Messages
1) Implement an HTTP Api,post request parameter with the ability to receive a POST request as follows:
namevaluepair[] data = {
New Namevaluepair ("message", Message.gettransferobjectjson ()),
New Namevaluepair ("MessageName", Message.getmessagename ()),
New Namevaluepair ("tag", Message.getcustomtag ()),
};
2) The return value of the API is required as a JSON string, the content requirements are as follows (responsecode:0 indicates a successful processing, less than 0 indicates a system exception, and greater than 0 represents a business exception):
"{exceptionmessgage:null, responsecode:0}"
3) Configure the URL of the API into the Messageconsumersconfig table
The following is a simple example:
@Controller
public class Consumercontroller {
private static final Logger Logger = Logger.getlogger (Consumercontroller.class);
@RequestMapping (value = "Testmsgconsumer", Method=requestmethod.post)
@ResponseBody
Public String LogMessage (HttpServletRequest request) {
Logger.info (Request.getparameter ("MessageName") + "(" + request.getparameter ("tag") + "):" + Request.getparameter (" Message "));
Return "{exceptionmessgage:null, responsecode:0}";
}
}
Java Asynchronous messaging Platform