One: Why use Message Queuing?
In the development of an app backstage, one of the most important piece is the message, communication module, the use of open source OpenFire.
Architecture:
Two API servers
Two OpenFire servers
Several database server clusters
A large part of the business needs to send a message, the user placed the order, the user cancels the order, and so on all need the server to send a message to the user. The solution used is to specify its own message format based on the OpenFire. The user goes to action, and the API server notifies the OpenFire server to send the message. The OpenFire server goes to accept requests from the API server and then sends a message to the user.
There is a problem, countless interfaces will tell the OpenFire server to send messages, when the request to a certain extent when the OpenFire server must not be processed.
Workaround:
First, use the fast things first to send the message to save first. It then tests the number of requests per second that the OpenFire server can accept, and then sends the messages sequentially out of the saved messages every second.
In the absence of too much experience, the choice of a supposedly simplest redis, test the next reluctantly can.
This is the first time I've ever used a message queue.
Two: Using Message Queuing
In the new project, is the e-commerce app, which has a very important function is, in the constant price, in a certain time, the company's internal support a shot, followed by a lot of people to bid. One of the most recent possible pressures in the background is likely to be there. This is also a very important link.
In order to ensure the stability of this link, decided to use ACTIVEMQ (why use this, multi-pilot technology change haha)
The following formally began the use of ACTIVEMQ.
Three: Build ACTIVEMQ on the test server
First download ACTIVEMQ
Download URL: http://activemq.apache.org/activemq-5132-release.html
Tar XZFV apache-activemq-5.13.2-bin.tar.gz Decompression
CD CD APACHE-ACTIVEMQ-5.13.2/into the catalogue
./bin/activemq Start Start ACTIVEMQ
/sbin/iptables-i input-p TCP--dport 8161-j ACCEPT open activemq default console port
/sbin/iptables-i input-p TCP--dport 61616-j ACCEPT open activemq default connection port
/etc/rc.d/init.d/iptables Save
Install ACTIVEMQ on the test server complete
Four: Open ACTIVEMQ console
Http://ip:8161/admin
Then click Queues to create a queues
And then we can start the code path!!!!!
V: Use Java to test ACTIVEMQ
Create a producer:
Packagecom.nico.utils.ActiveMq;/*** Created by Administrator on 2016/4/27.*/Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;ImportJavax.jms.DeliveryMode;Importjavax.jms.Destination;ImportJavax.jms.MessageProducer;Importjavax.jms.Session;ImportJavax.jms.TextMessage;Importorg.apache.activemq.ActiveMQConnection;Importorg.apache.activemq.ActiveMQConnectionFactory; Public classSenderextendsThread { Public Static voidSendMessage (Session session, MessageProducer producer,inti)throwsException {textmessage message=session. Createtextmessage ("Message sent by ActiveMq" +i); //send message to Destination placeSYSTEM.OUT.PRINTLN ("Send message:" + "ActiveMq message sent" +i); Producer.send (message); } @Override Public voidrun () {intMessageno = 1; while(true) { //ConnectionFactory: Connection Factory, JMS uses it to create a connectionConnectionFactory ConnectionFactory; //CONNECTION:JMS Client-to-JMS Provider connectionsConnection Connection =NULL; //Session: A thread that sends or receives a messageSession session; //Destination: The destination of the message, to whom the message is sent.Destination Destination; //messageproducer: Message sendermessageproducer producer; //TextMessage message; //constructs the ConnectionFactory instance object, where the ACTIVEMQ implementation jar is usedConnectionFactory =Newactivemqconnectionfactory (Activemqconnection.default_user, activemqconnection. Default_password,"tcp://{ip}:61616"); Try { //construction Gets the connection object from the factoryConnection =connectionfactory.createconnection (); //StartConnection.start (); //Get Operation ConnectionSession =connection.createsession (Boolean.true, Session.auto_acknowledge); //Get session Note parameter value Xingbo.xu-queue is a server queue that must be configured in the ACTIVEMQ consoleDestination = Session.createqueue ("Firstqueue"); //get the message generator "sender"Producer =session.createproducer (destination); //set not persisted, learn here, actually according to the project decisionProducer.setdeliverymode (deliverymode.non_persistent); //constructs a message, writes dead here, the item is a parameter, or method getsSendMessage (Session, Producer,messageno); Session.commit (); } Catch(Exception e) {e.printstacktrace (); } finally { Try { if(NULL!=connection) Connection.close (); } Catch(Throwable ignore) {}} Messageno++; Try{sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }}
Create a consumer:
Packagecom.nico.utils.ActiveMq;Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;Importjavax.jms.Destination;ImportJavax.jms.MessageConsumer;Importjavax.jms.Session;ImportJavax.jms.TextMessage;Importorg.apache.activemq.ActiveMQConnection;Importorg.apache.activemq.ActiveMQConnectionFactory; Public classReceiverextendsthread{@Override Public voidrun () {//ConnectionFactory: Connection Factory, JMS uses it to create a connectionConnectionFactory ConnectionFactory; //CONNECTION:JMS Client-to-JMS Provider connectionsConnection Connection =NULL; //Session: A thread that sends or receives a messageSession session; //Destination: The destination of the message, to whom the message is sent.Destination Destination; //consumer, message recipientMessageconsumer Consumer; ConnectionFactory=Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_ PASSWORD,"tcp://{ip}:61616"); Try { //construction Gets the connection object from the factoryConnection =connectionfactory.createconnection (); //StartConnection.start (); //Get Operation ConnectionSession =connection.createsession (Boolean.false, Session.auto_acknowledge); //Get session Note parameter value Xingbo.xu-queue is a server queue that must be configured in the ACTIVEMQ consoleDestination = Session.createqueue ("Firstqueue"); Consumer=Session.createconsumer (destination); while(true) { //set the receiver to receive the message time, in order to facilitate testing, here who set the 100sTextMessage message = (textmessage) consumer.receive (100000); if(NULL!=message) {System.out.println ("Receive Message" +Message.gettext ()); } Else { Break; } Sleep (3000); } } Catch(Exception e) {e.printstacktrace (); } finally { Try { if(NULL!=connection) Connection.close (); } Catch(Throwable ignore) {} }}}
Then turn on our test:
Package com.nico.utils.ActiveMq; /** */Publicclass activedemo {publicstatic void Main (string[] args) { receiver Receiverthread=new Receiver (); Receiverthread.start (); Sender Senderthread=new sender (); Senderthread.start (); }}
Printing results:
Send message: ActiveMq message 1 received message ACTIVEMQ sent message 1 send message: ActiveMq sent message 2 received message ACTIVEMQ sent message 2 sent message: ActiveMq sent message 3 received message ActiveMq Send Message 3 Send message: ACTIVEMQ Send message 4 Receive message ACTIVEMQ Send message 4
You can then simply see the test results.
Then integrate the queue into the system to be able!
Message Queuing ACTIVEMQ