C # send and receive data through ACtiveMQ p2p,
1. Download ActiveMQ
Http://activemq.apache.org/
2. Run ActiveMQ
Uncompress apache-activemq-5.10.0-bin.zip, and double-click the apache-activemq-5.10.0 \ bin \ win32 \ activemq. bat to run the ActiveMQ program.
After ActiveMQ is started, you can log on to: http: // localhost: 8161/admin/in the browser for verification. The default user name is: admin and the password is admin.
(The premise is to configure the JDK environment. Opening the console indicates that MQ runs successfully)
Download. net Dll: apache.nms-1.6.0-bin.zipand apache.nms.activemq-1.6.4-bin.zip.
Download from here: http://archive.apache.org/dist/activemq/apache-nms/1.6.0/
(1) Apache. NMS. dll path: \ Apache. NMS. ActiveMQ-1.7.2-bin \ lib \ Apache. NMS \ net-3.5
(2) Apache. NMS. ActiveMQ. dll path: \ Apache. NMS. ActiveMQ-1.7.2-bin \ build \ net-3.5 \ debug
3. MQ example (producer) public Form1 () {InitializeComponent (); InitProducer ();} private IConnectionFactory factory; public void InitProducer () {try {// initialize factory = new ConnectionFactory ("tcp: // localhost: 61616");} catch {lbMessage. text = "initialization failed" ;}} private void button#click (object sender, EventArgs e) {// create a factory connection using (IConnection connection = factory. createConnection () {// create a Session using through a factory connection (ISession session = connection. createSession () {// create a producer through a session. In the method, the Queue IMessageProducer prod of MQ is created. createProducer (new Apache. NMS. activeMQ. commands. activeMQQueue ("firstQueue"); // create an ITextMessage object that sends messages = prod. createTextMessage (); XmlDocument Doc = new XmlDocument (); Doc. loadXml ("<? Xml version = '1. 0' encoding = 'utf-8'?> <Flightroute> <flight> <flightinfo> <acid> CCA1501 </acid> <runway> 13L </runway> <gate> N115 </gate> <cockpitdire> 180 </cockpitdirection> <deparr> DEP </deparr> </flightinfo> </flight </flightroute> "); message. text = Doc. innerXml; // assign the actual message to the message object // set the message object attribute, which is the filter condition of Queue and the unique message attribute of P2P. properties. setString ("filter", "demo"); prod. send (message, MsgDeliveryMode. nonPersistent, MsgPriority. normal, TimeSpan. minVa Lue); lbMessage. Text = "sent successfully! "; Text. Text =" "; Text. Focus ();}}}
You can click the button to send something
Acceptor code (consumer)
Public Form2 () {InitializeComponent (); InitConsumer ();} public void InitConsumer () {// create a connection factory IConnectionFactory factory = new ConnectionFactory ("tcp: // localhost: 61616 "); // create a connection through the factory IConnection connection = factory. createConnection (); // the ID of the connection server. clientId = "firstQueueListener"; // start the connection. start (); // create a conversation ISession session = connection through a connection. createSession (); // creates a consumer IMessageConsumer consumer = session through the session. createConsumer (new Apache. NMS. activeMQ. commands. activeMQQueue ("firstQueue"), "filter = 'Demo'"); // register the listener event consumer. listener + = new MessageListener (consumer_Listener);} void consumer_Listener (IMessage message) {ITextMessage msg = (ITextMessage) message; ReceiveMessage. invoke (new DelegateRevMessage (RevMessage), msg);} public delegate void DelegateRevMessage (ITextMessage message); public void RevMessage (ITextMessage message) {ReceiveMessage. text + = string. format (@ "Received: {0} {1}", message. text, Environment. newLine );}
The sent message is stored in the Queue if it is not received.