ACTIVEMQ is a good thing, do not have to say more. ACTIVEMQ offers multiple language support, such as Java, C, C + +, C #, Ruby, Perl, Python, PHP, etc. Since I developed the GUI under Windows, I am more concerned with C + + and C #, where C # 's activemq is simple, and Apache provides NMS (. NET Messaging Service) support for. NET development, just a few steps to build a simple implementation. The application of C + + is relatively troublesome, later to write the article introduction.
1, to ACTIVEMQ official website to download the latest version of the ACTIVEMQ, I was under the 5.3.1,5.3.2 now has come out.
2, to ACTIVEMQ official website Download the latest version of APACHE.NMS, need to download Apache.nms and Apache.NMS.ActiveMQ Two bin package, if the source of interest, you can also download the SRC package. Here to remind you, If you download 1.2.0 version of Nms.activemq,apache.nms.activemq.dll in the actual use of a bug, that is, stop ActiveMQ application will throw WaitOne function exception, see the SRC package source code discovery is due to Apache.NMS.ActiveM Q-1.2.0-src\src\main\csharp\transport\inactivitymonitor.cs in the following code caused, modify the source code to recompile. Look at the latest version of 1.3.0 has fixed this bug, so download the latest version.
private void Stopmonitorthreads () {Lock (monitor) {if (Monitorstar Ted. Compareandset (True, False)) {AutoResetEvent shutdownevent = new AutoResetEvent (FA LSE); Attempt to wait for the timers to shutdown, but don ' t wait//forever, if they don ' t shutdown aft Er, seconds, just quit. This.readCheckTimer.Dispose (shutdownevent); Shutdownevent.waitone (Timespan.frommilliseconds (2000)); This.writeCheckTimer.Dispose (shutdownevent); Shutdownevent.waitone (Timespan.frommilliseconds (2000)); Definition of WaitOne: public virtual bool WaitOne (TimeSpan timeout,bool exitcontext) this.asyncTasks.Shutdown (); This.asynctasks = null; This.asyncwritetask = null; This.asyncerrortask = null; }}} private void Stopmonitorthreads () {Lock (monitor) { if (Monitorstarted.compareandset (True, False)) {AutoResetEvent Shutdowne vent = new AutoResetEvent (false); Attempt to wait for the timers to shutdown, but don ' t wait//forever, if they don ' t shutdown after Seconds, just quit. This.readCheckTimer.Dispose (shutdownevent); Shutdownevent.waitone (Timespan.frommilliseconds (2000)); This.writeCheckTimer.Dispose (shutdownevent); Shutdownevent.waitone (Timespan.frommilliseconds (2000)); Definition of WaitOne: public virtual bool WaitOne (TimeSpan timeout,bool exitcontext) This.asyncTasks.Shutdown () ; This.asynctasks = nulL This.asyncwritetask = null; This.asyncerrortask = null; } } }
3, run Activemq, find activemq extracted Bin folder: ... \apache-activemq-5.3.1\bin, Execute the Activemq.bat batch file to start the ACTIVEMQ server, the default port is 61616, which can be modified in the configuration file.
4, write C # program to achieve ACTIVEMQ simple application. Create a new C # project (a Producter project and a consumer project), WinForm or the console program, which is built with the console project, Add references to Apache.NMS.dll and Apache.NMS.ActiveMQ.dll, and then you can write the implementation code, with the simple producer and consumer implementation code as follows:
Producer
Using System; Using System.Collections.Generic; Using System.Text; Using Apache.nms; Using Apache.NMS.ActiveMQ; Using System.IO; Using System.Xml.Serialization; Using System.Runtime.Serialization.Formatters.Binary; Namespace Publish {class Program {static void Main (string[] args) {try {//create the Connection Factory iconnectionfactory Factory = new Connec Tionfactory ("tcp://localhost:61616/"); using (iconnection connection = factory. CreateConnection ()) {//create the Session using (ISession s Ession = connection. CreateSession ()) {//create The Producer for the Topic/queue Imessageproducer prod = session. Createproducer (New Apache.NMS.ActiveMQ.Commands.ActiveMQTopic ("testing")); Send Messages int i = 0; while (! console.keyavailable) {itextmessage msg = prod. Createtextmessage (); Msg. Text = i.ToString (); Console.WriteLine ("Sending:" + i.tostring ()); Prod. Send (msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, Timespan.minvalue); System.Threading.Thread.Sleep (5000); i++; }}} console.readline (); } catch (System.Exception e) {Console.WriteLine ("{0}", e.message); Console.ReadLine (); } } } }
Consumer:
Using System; Using System.Collections.Generic; Using System.Text; Using Apache.nms; Using Apache.NMS.ActiveMQ; Using System.IO; Using System.Xml.Serialization; Using System.Runtime.Serialization.Formatters.Binary; Namespace Subscribe {class Program {static void Main (string[] args) {TR y {//create the Connection factory iconnectionfactory factory = new Conn Ectionfactory ("tcp://localhost:61616/"); Create the connection using (iconnection connection = factory. CreateConnection ()) {connection. ClientId = "Testing listener"; Connection. Start (); Create the session using (ISession session = connection. CreateSession ()) {//create the Consumer Imessag Econsumer ConsUmer = Session. Createdurableconsumer (New Apache.NMS.ActiveMQ.Commands.ActiveMQTopic ("testing"), "testing listener", NULL, FALSE); Consumer. Listener + = new MessageListener (Consumer_listener); Console.ReadLine (); } connection. Stop (); Connection. Close (); }} catch (System.Exception e) {Console.WriteLine (e.message); }} static void Consumer_listener (IMessage message) {try {itextmessage msg = (itextmessage) message; Console.WriteLine ("Receive:" + MSG.) Text); } catch (System.Exception e) {Console.WriteLine (e.message); } } } }
program implementation of the function: the producer producer established a theme named testing, and every 5 seconds to send messages to the topic, the consumer consumer subscribed to the testing theme, so long as the producer sends testing subject messages to the ACTIVEMQ server, The server sends the message to consumers subscribed to the testing topic.
Compile to generate Producer.exe and Consumer.exe, and execute two exe, you can see the message sent and received.
This example is a built-in theme (Topic), ACTIVEMQ also supports another way: Queue, that is, peer, what is the difference between the two? The difference is that topic is broadcast, that is, if a topic is subscribed to by multiple consumers, the server sends the message to all consumers whenever a message arrives at the server, and the queue is point-to-point, that is, a message can only be sent to a consumer, and if a queue is subscribed to by multiple consumers, If there is no special case, the message will be sent to different consumers one by one, such as:
Msg1-->consumer A
Msg2-->consumer B
Msg3-->consumer C
Msg4-->consumer A
Msg5-->consumer B
Msg6-->consumer C
Special case refers to: ACTIVEMQ support filtering mechanism, that is, the producer can set the Message property (properties), which corresponds to the consumer side of the selector, only the consumer set selector and the properties of the message match, The message will be sent to the consumer. Both topic and queue support Selector.
How do properties and selector be set up? Take a look at the following code:
Producer
public void SetProperties () {itextmessage msg = prod. Createtextmessage (); Msg. Text = i.ToString (); Msg. Properties.setstring ("Myfilter", "test1"); Console.WriteLine ("Sending:" + i.tostring ()); Prod. Send (msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, timespan.minvalue); Itextmessage msg = prod. Createtextmessage (); Msg. Text = i.ToString (); Msg. Properties.setstring ("Myfilter", "test1"); Console.WriteLine ("Sending:" + i.tostring ()); Prod. Send (msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, timespan.minvalue);}
Consumer
public void Setselector () {//Consumer Selector Consumer = session. Createconsumer (New Apache.NMS.ActiveMQ.Commands.ActiveMQQueue ("testing"), "myfilter= ' test1 '"); Selector Imessageconsumer consumer = Session is set by parameter when generating consumer. Createconsumer (New Apache.NMS.ActiveMQ.Commands.ActiveMQQueue ("testing"), "myfilter= ' test1 '");}