ACTIVEMQ First Experience

Source: Internet
Author: User

First introduce the next MQ,MQ English name MessageQueue, the Chinese name is the message queue used by everyone, why use it, plainly speaking is a message to accept and forward the container, can be used for message push.

Here are the topics that are presented today for you: ACTIVEMQ

He is an open source Message Queuing software produced by Apache that runs under the JVM and supports multiple languages, such as java,c++,c#.

Now let me introduce you to the server side of how to configure ACTIVEMQ:

1, of course, is to download the software

Go to the official website to download: http://activemq.apache.org/I download is apache-activemq-5.8.0-bin 5.8 version, of course, open source is also supported to download source, need to compile their own, here do not do too much introduction

2, after decompression, enter \apache-activemq-5.8.0\bin\win32 start Activemq.bat. The system will automatically execute the boot process, of course, the general installation failure is not installed in the JVM environment, the start of success should be like this

3, open the browser input http://localhost:8161/admin/default configuration is this, of course, you can also change this configuration

4, to this end, the server started to complete

————————————————————————————————— Perfect Segmentation ———————————————————————————————————

Now is the topic of today, how to use the API provided by ACTIVEMQ in C # for subscribing and publishing messages

1, first need to download ActiveMQ provided API functions, this also from the official website Apache.nms-1.6.0-bin and Apache.nms.activemq-1.6.0-bin These two DLLs are downloaded after the folder bin directory;

2, the two documents in the project reference;

3, in Activemq, there are two concepts, one is the producer (Producer), the other is the consumer (Consumer), the producer is what we often say the publisher, and the consumer, is the subscriber, so the explanation may be better understand, if you know the Publish subscription model, If you do not know, the literal meaning is also very good understanding, the Publisher is to publish the message, and the Subscriber by subscribing to the event, the message received;

4, directly on the code, WinForm code, if you do not want to handle the interface thread callback problem, you can use the console program

5, produce

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using apache.nms;10 using Apache.nms.activemq;11 namespace MqProducer13 {public partial class Producerdemo:f         ORM15 {16//Declaration Connection object Factory-Private iconnectionfactory factory;18 Producerdemo () 20 {InitializeComponent (); Initproducer ();}24 public void Initproducer () try28 {29//initial chemical plant, where the default URL is no need to modify the factory = new C Onnectionfactory ("tcp://localhost:61616");}33 catch34 {lbmess Age. Text = "Initialization failed!!";             }37}38-Btnconfirm_click (object sender, EventArgs e) 40 {41 Establish a connection using the factory (iconnection connection = factory. CreateConnection ()) 43 {44//creates session session with connection using (isession session = Connec tion. CreateSession ()) 46 {47//create a producer through a session, and new in the method is the Queue48 in MQ IMessage Producer prod = session.                     Createproducer (New Apache.NMS.ActiveMQ.Commands.ActiveMQQueue ("Firstqueue")); 49//Create a sent Message object 50 Itextmessage message = Prod. Createtextmessage (); 51//assigns the actual message to this object. Text = txtmessage.text;53//Sets the properties of the Message object, which is very important, is the filter condition of the queue and is also the only specified attribute of the peer message. Properties.setstring ("Filter", "Demo"); 55//producers send messages out, several enumeration parameters Msgdeliverymode whether long chain, msgpriority message priority, send minimum Units, and of course other overloaded prod. Send (Message, Msgdeliverymode.nonpersistent, Msgpriority.normal, timespan.minvalue); lbmessage.text = "Sent successfully!!"; Txtmessage.text = ""; Txtmessage.focus (); 60}61}62 63}64}65} 

6, Consumer

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using apache.nms;10 using Apache.nms.activemq;11 namespace MqConsumer13 {public partial class Consumerdemo:f ORM15 {Consumerdemo () + InitializeComponent (); Initconsumer (); }21 public void Initconsumer () 23 {24//Create connection Factory iconnectionfactory F Actory = new ConnectionFactory ("tcp://localhost:61616"); 26//factory-built connection iconnection connection = f Actory. CreateConnection (); 28//This is the client name of the connection ID connection. ClientId = "Firstqueuelistener"; 30//Start connection, listen to the initiative to initiate the connection to the connection. Start (); 32//Create a session by connecting isession session = connection. CreateSession (); 34//Create a consumer through a session, this is the queue sessionType of listening parameter setting Imessageconsumer consumer = session.             Createconsumer (New Apache.NMS.ActiveMQ.Commands.ActiveMQQueue ("Firstqueue"), "Filter= ' demo"); 36//Register monitoring event 37 Consumer. Listener + = new MessageListener (Consumer_listener),//connection. Stop ();//connection.  Close (); }42 Consumer_listener (IMessage message), {itextmessage msg = (ITEXTM essage) message;46//Asynchronous call, otherwise you cannot return to the main thread, Tbreceivemessage.invoke (new Delegaterevmessage (Revmessage), M SG);}50 public delegate void Delegaterevmessage (Itextmessage message); Evmessage (Itextmessage message) Tbreceivemessage.text + = string. Format (@ "Received: {0}{1}", message.) Text, Environment.NewLine); 56}57}58}

7. Start the interface and that's it.

  

Today I said is a fur, I also received the task of the afternoon to do MQ aspects of the development, only temporary embrace of Buddhism, hope, we continue to study, of course, nothing can be the code of the API to see how the above is realized, the benefits of open source is not much to say, do their homework to

ACTIVEMQ First Experience

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.