1.ActiveMQ Introduction
The first analysis of such a scene: when we shop on the site, must pass, place orders, invoice creation, payment processing, order fulfillment, shipping and so on. However, when the user orders, immediately jump to the "Thank you for your order" page. Moreover, if there is no delay, the user will also receive an email. If we use the traditional way to achieve, is generally a database operation, and then invoke a variety of interfaces, various services waiting for various responses, so that the next order takes a long time to see the results, if something goes wrong, then this order results 1:30 will not see, for modern e-commerce, This is intolerable.
So activemq long can be a good solution to this problem: when the user orders, there is a call to ACTIVEMQ, used to submit the order, and then the system can return the results page, and the entire order process will not be called synchronously, background job can chat while running. The front desk order volume is very large case each order also need to call only once, backstage suddenly can't handle? Slowly, no one forced you, orders are in the ACTIVEMQ waiting in line, they will not be irritable.
This scenario is ideal for a cache queue, which is called Message Queuing, and many products can implement this feature, by the way, to answer the next potential problem: he is much faster than a database operation.
This article starts from the Foundation to study together, this paper system operating environment: WINDOW7 32 bit
2. Download ACTIVEMQ
Official website: http://activemq.apache.org/
3. Running ACTIVEMQ
Unzip Apache-activemq-5.10.0-bin.zip, and then double-click Apache-activemq-5.10.0\bin\win32\activemq.bat to run the ACTIVEMQ program.
See the last line of the console output: "Access to all MBeans is allowed" proves successful.
After starting ACTIVEMQ, you can use the browser login: http://localhost:8161/admin/authentication, the default username is: admin password is: admin
(If the Java environment is installed)
Download. NET version Dll:apache.nms-1.6.0-bin.zip and apache.nms.activemq-1.6.4-bin.zip at the same time
Download from here: http://archive.apache.org/dist/activemq/apache-nms/1.6.0/
3. First example of ACTIVEMQ
First write a message source using C #, which is the producer:
The interface is simple, a lable, a textbox, a button, and then add Apache.NMS.dll and Apache.NMS.ActiveMQ.dll references, look in the other 2 downloads, note the version number.
Public Form1 () {InitializeComponent (); Initproducer (); } Private Iconnectionfactory Factory; public void Initproducer () {try {//initial chemical factory, where the default URL is a fact that does not need to be modified Ory = new ConnectionFactory ("tcp://localhost:61616"); } catch {Lbmessage.text = "initialization failed!!"; }} private void Btnconfirm_click (object sender, EventArgs e) {//factory-Established connection Usin G (iconnection connection = factory. CreateConnection ()) {//create session using via connection (isession session = connection. CreateSession ()) {//Create a producer through a session, new in the method is the queue Imessageproducer in MQ Prod = session. Createproducer (New Apache.NMS.ActiveMQ.Commands.ActiveMQQueue ("Firstqueue")); Create a sent Message object Itextmessage msg = Prod. CreAtetextmessage (); Give the object a real message. Text = Txtmessage.text; It is important to set the properties of the Message object, which is the filter condition of the queue and the only specified property message for the peer message. Properties.setstring ("Filter", "Demo"); The producer sends the message out, several enumeration parameters Msgdeliverymode whether long chain, msgpriority message priority, send the smallest unit, and of course other overloaded prod. Send (Message, Msgdeliverymode.nonpersistent, Msgpriority.normal, Timespan.minvalue); Lbmessage.text = "sent successfully!!"; Txtmessage.text = ""; Txtmessage.focus (); } } }
You can click the button to send some messages, these messages are staged into the ACTIVEMQ, you can log in just the password to the page to see.
Then write the receiving end, i.e. the consumer:
Also add the reference to the 2 DLLs, the interface is very simple only one lable:
Public Form1 () {InitializeComponent (); Initconsumer (); } public void Initconsumer () {//Create connection factory iconnectionfactory factory = new Connectio Nfactory ("tcp://localhost:61616"); Connect iconnection connection = factory through factory construction. CreateConnection (); This is the client name of the connection that identifies connection. ClientId = "Firstqueuelistener"; Start the connection, listen to the initiative to initiate the connection connection. Start (); Create a session by connection ISession session = connection. CreateSession (); Create a consumer through the session, this is the queue this session type of listening parameter settings Imessageconsumer consumer = session. Createconsumer (New Apache.NMS.ActiveMQ.Commands.ActiveMQQueue ("Firstqueue"), "Filter= ' demo '"); Registers the listener event consumer. Listener + = new MessageListener (Consumer_listener); Connection. Stop (); Connection. Close (); } void Consumer_listener (IMessage message){itextmessage msg = (itextmessage) message; The main thread Tbreceivemessage.invoke (new Delegaterevmessage (Revmessage), MSG) cannot be returned under asynchronous invocation; } public delegate void Delegaterevmessage (Itextmessage message); public void Revmessage (Itextmessage message) {Tbreceivemessage.text + = string. Format (@ "Received: {0}{1}", message.) Text, Environment.NewLine); }
As soon as you start, you'll see that this message has been received and the server side has been deleted.
4. Summary
The above implementation of a peer-to message queue, is temporarily stored in wait for processing, the process is over the message is gone, guaranteed delivery, will not be lost, will not be swapped. This mode of use is limited, and customers can only have one, belongs to the Express form, not meet the order processing requirements, the reason is very simple: The blacklist inspection link to obtain this order information, audit link to get the next section we learn together "message mass", is shouting, the order came, everyone started busy it ...
Code download
. NET platform Activemq Getting Started instance