BasicThe Advanced Message Queuing Protocol (AMQP1) is an application-layer protocol specification used by asynchronous messaging, a line-layer protocol rather than an API, which cannot be used directly by the developer, and its clients can send and receive messages regardless of the source of the message. The original purpose of AMQP is simply to provide the financial community with a message protocol that can collaborate with one another, and now the goal is to provide common building tools for the common Message Queuing architecture.
Version numberThe AMQP version is represented by two or three numbers – the major version number, the minor version number, and the optional revision number. For convenience, the version number is expressed as: major-minor[-revision] or major.minor[.revision]:
In the official note, major, minor, and revision all support numbers from 0 to 99.
Major, minor, and revision in 100 and above are reserved for internal testing and development.
The version number indicates syntax and semantic interoperability.
Version 0-9-1 represents major = 0, minor = 9, revision = 1.
The 1.1 version is expressed as Major = 1, minor = 1, revision = 0. amqp/1.1 is equivalent to amqp/1.1.0 or amqp/1-1-0.
Qpid and RabbitMQ are service software that implements the same functionality, all of which support the AMQP protocol
two ways to implement: Qpid
Configure @Configuration public class Mqconfiguration {//MQ address @Value ("${mq.connection}") Private String Mqconnectio
N
Listener Address One @Value ("${address1}") Private String Address1;
Listener address two @Value ("${address2}") Private String address2;
The listener of address one @Autowired private Listener1 listener1;
The listener of address two @Autowired private Listener2 listener2; Get the connection factory, by connecting the address will build this factory, make the name of the factory generated @Bean (name = "Connectfactory") public amqconnectionfactory Getamqconnectionfacto
Ry () throws urlsyntaxexception {return new amqconnectionfactory (mqconnection); Or the bottom is set to final cachingconnectionfactory result = new Cachingconnectionfactory (New Amqconnectionfactory (
mqconnection));
Result.setsessioncachesize (10); //spring provides the Java Message Service tool class, establishes a connection, etc., specifically to which address is sent in the sending class according to the type to judge @Bean (name = "Jmstemplate") public jmstemplate Jmstemplate
() throws Urlsyntaxexception {jmstemplate jmstemplate = new Jmstemplate (); Jmstemplate.setconnectionfactorY (This.getamqconnectionfactory ());
Jmstemplate.setexplicitqosenabled (TRUE);
return jmstemplate; }//Subscribe or listen to the address, generate a amqanydestination class for listening @Bean (name = "Address1subscribe") public amqanydestination Address1subs
Cribe () throws URISyntaxException {return new amqanydestination (Address1subscribe); /** * is a management class for asynchronous message snooping, * First, generate an object for a management class * second, connect * third, set listener, is a class object, listen to this address message **/@Bean (name = "Address1listener Container ") Public Defaultmessagelistenercontainer Address1listenercontainer () throws URISyntaxException {Def
Aultmessagelistenercontainer container = new Defaultmessagelistenercontainer ();
Container.setconnectionfactory (This.getamqconnectionfactory ());
Container.setdestination (This.address1subscribe ());
Container.setmessagelistener (Listener1);
return container;
} @Bean (name = "Address2subscribe") public amqanydestination Address2nsubscribe () throws URISyntaxException { RetUrn New amqanydestination (address2subscribe); } @Bean (name = "Address2listenercontainer") public Defaultmessagelistenercontainer Address2nlistenercontainer () t
Hrows urisyntaxexception {Defaultmessagelistenercontainer container = new Defaultmessagelistenercontainer ();
Container.setconnectionfactory (This.getamqconnectionfactory ());
Container.setdestination (This.address2subscribe ());
Container.setmessagelistener (LISTENER2);
return container; }}//Send message @Component public class Messagesender {//Send message Address one @Value ("${addr.one.publish}") Private String Pub
Lishone;
Address of the sending message a @Value ("${addr.two.publish}") Private String publishtwo;
Inject the Java Message Service class in the configuration Class @Autowired private jmstemplate jmstemplate; The conversion between JSON and Bean is the class private objectmapper objectmapper = new Objectmapper (). Configure (Deserializationfeature.fail_
On_unknown_properties, false); In addition to sending the message object, the type public void SendMessage (M) is sent to determine the sending address when using the messagesEssagetypeenum MessageType, Object message) throws URISyntaxException, jsonprocessingexception {if (message = = Nu
LL) {throw new Badrequestexception ("JMS message can ' t be null!");
}//According to the type to determine the sent message address Destination Destination = null;
if (Messagetype.equals (messagetypeenum.task_issue)) {destination = new amqanydestination (publishone);
} else {destination = new amqanydestination (publishtwo); } logger.info (New namevaluelist<string, string> (). Add ("desc", "Messagesender send Onemessage"). Ad
D ("type", Messagetype.name ()). Add ("Message", objectmapper.writevalueasstring (Message)));
Jmstemplate.convertandsend (destination, objectmapper.writevalueasstring (message)); }}//Receive end @Component public class Listener1 implements MessageListener {private static final Objectmapper Object_ma
Pper = new Objectmapper (); Since the listening address setting is already set in the configuration class, there is no need to set it up, and if there is no setting it can be done in the bottom//@JmsListener (dEstination = "${address1}", concurrency = "5") @Override public void onMessage (Message message) {if (message ! = null) {if (message instanceof textmessage) {TextMessage textmessage = (textmessage) mess
Age
String msg = NULL;
try {msg = Textmessage.gettext (); Logger.info (New Namevaluelist (). Add ("desc", "Listener1 receive one Message"). Add ("Body", msg)
);
Handler1message1 (msg); } catch (Exception e) {}}} else {Logger.info ("Listener1 Receive one EM
Pty message,ignore! ");
throw new IllegalArgumentException ("Assetalteration mesage is empty!"); }}//For business processing private void Handler1message1 (String msg) throws IOException, URISyntaxException, INTERRUPTEDEXCEP tion {while (true) {if (Hasnolock ())} {break;
} else {//If there is a lock, block 2s thread.sleep (2000L); }}//Deserialize msg AlterationMessage1 alterationMessage1 = Object_mapper.readvalue (msg, Alterationmessa
Ge1.class);
} private Boolean Hasnolock () throws IOException, urisyntaxexception {list<info> List = aa.getlist ();
Boolean NoLock = true;
for (Info info:list) {if (something) {noLock = false;
Logger.info ("There is a lock:" +key);
}} return noLock; }
}
two ways to implement: RabbitMQ
This article is good to say
Reference articles
Http://www.infoq.com/cn/articles/AMQP-RabbitMQ
AMQP-0-9-1 Chinese Specification http://www.blogjava.net/qbna350816/archive/2016/08/12/431554.html