. NET platform Activemq Getting Started instance

Source: Internet
Author: User

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

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.

         PublicForm1 () {InitializeComponent ();        Initproducer (); }        PrivateIconnectionfactory Factory;  Public voidInitproducer () {Try            {                //Initial chemical Factory, here the default URL is not required to modifyFactory =NewConnectionFactory ("tcp://localhost:61616"); }            Catch{Lbmessage.text="initialization failed !"; }        }        Private voidBtnconfirm_click (Objectsender, EventArgs e) {            //Connect through the factory            using(Iconnection connection =Factory. CreateConnection ()) {//creating session sessions from a connection                using(ISession session =connection. CreateSession ()) {//Create a producer through a session, and new in the method is the queue in MQImessageproducer prod = session. Createproducer (NewApache.NMS.ActiveMQ.Commands.ActiveMQQueue ("Firstqueue")); //to create a sent message objectItextmessage message =Prod.                    Createtextmessage (); //give an actual message to this objectMessage. Text =Txtmessage.text; //setting the properties of the Message object, which is important, is the filter condition of the queue and the only specified property of the peerMessage. Properties.setstring ("Filter","Demo"); //The producer sends the message out, a few enumeration parameters Msgdeliverymode whether long chain, msgpriority message priority, send the smallest unit, and of course other overloadsProd.                    Send (Message, Msgdeliverymode.nonpersistent, Msgpriority.normal, Timespan.minvalue); Lbmessage.text="send success!!"; 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:

         PublicForm1 () {InitializeComponent ();        Initconsumer (); }          Public voidInitconsumer () {//Create a connection factoryIconnectionfactory factory =NewConnectionFactory ("tcp://localhost:61616"); //building connections through the factoryIconnection connection =Factory.             CreateConnection (); //This is the client name identity of the connectionConnection. ClientId ="Firstqueuelistener"; //Start the connection, listen to the initiative to initiate the connectionconnection.             Start (); //create a session from a connectionISession session =connection.             CreateSession (); //Create a consumer through a session, this is the listener parameter setting for this session type of queueImessageconsumer consumer = session. Createconsumer (NewApache.NMS.ActiveMQ.Commands.ActiveMQQueue ("Firstqueue"),"filter= ' demo '"); //Registering for monitoring eventsConsumer. Listener + =NewMessageListener (Consumer_listener); //connection.             Stop (); //connection.  Close ();           }          voidConsumer_listener (IMessage message) {itextmessage msg=(itextmessage) message; //asynchronous call, or you cannot return to the main threadTbreceivemessage.invoke (NewDelegaterevmessage (revmessage), msg); }           Public Delegate voiddelegaterevmessage (Itextmessage message);  Public voidrevmessage (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 ...

Ah

. NET platform Activemq Getting Started instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.