ACTIVEMQ Another mode of Sub/hub that is the publish subscription mode, is the Sub/hub is a drag n the meaning of the USB splitter. It means that a source is divided into n exits. As an example of the previous section, when an order is generated, the background n systems need to be linked, but there is a prerequisite to receive order information, then we need to publish a producer's message to n consumers.
Producers:
Try{//Create the Connection Factory iconnectionfactory Factory =New ConnectionFactory ("tcp://localhost:61616/");using (iconnection connection =Factory. CreateConnection ()) {//Create the Sessionusing (ISession session =Connection. CreateSession ()) {//Create the Producer for the topic/queue imessageproducer prod =Session. Createproducer (New Apache.NMS.ActiveMQ.Commands.ActiveMQTopic ("Testing"));//Send Messagesint 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; }}} console.readline (); } catch (System.Exception e) {Console.WriteLine ( "{0}"
Suppose the producer sends a message every 5 seconds:
Consumers:
Staticvoid Main (String[] args) {Try{//Create the Connection factory iconnectionfactory factory =New ConnectionFactory ("tcp://localhost:61616/");//Create the connectionusing (iconnection connection =Factory. CreateConnection ()) {connection. ClientId ="Testing Listener1"; Connection. Start ();//Create the Sessionusing (ISession session =Connection. CreateSession ()) {//Create the Consumer Imessageconsumer Consumer = session. Createdurableconsumer (New Apache.NMS.ActiveMQ.Commands.ActiveMQTopic ("Testing"),"Testing Listener1",NullFalse); 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 =< Span style= "color: #000000;" > (itextmessage) message; Console.WriteLine ( "receive: "+ Msg. Text); } catch (System.Exception e) {Console.WriteLine (E. Message); } }
Start a consumer:
We found out that he started from 15, instead of starting from scratch like in the previous section and starting another consumer:
We found that the message was received from the start, and the previous message was lost.
The overall status is as follows:
We observe the management interface:
Generated a testing topics, and Subscribers have 2 subscriptions that are testing:
This only needs to be received in a timely manner where the message needs to be subscribed.
Source code Download
ACTIVEMQ Publish Subscription Mode