[MQ] simple example of activemq message sending and receiving

Source: Internet
Author: User

The message type used in this example is mapmessage.CodeIt's relatively simple. Post it for you to see:

Receive message:

Private void receive () {var factory = new connectionfactory (program. brokeruri); var timeout = new timespan (0, 0, 10); Using (VAR connection = factory. createconnection () {using (VAR session = connection. createsession () {var Destination = sessionutil. getdestination (Session, program. normalqueuedestination); Using (VAR consumer = session. createconsumer (destination) {connection. start (); var stopw Atch = new stopwatch (); var Index = 1; for (VAR I = 0; I <program. messagecount; I ++) {If (Index = 1) {stopwatch. start ();} Try {var message = (imapmessage) consumer. receive (timeout); index ++; var messageobj = Common. getmessageobjbyimapmessage (Message); If (messageobj! = NULL) {// debug. writeline (messageobj. mediataskid);} If (Index = program. statisticsmessagecountspan) {stopwatch. stop (); var spendseconds = stopwatch. elapsed. totalseconds; var speed = program. statisticsmessagecountspan/spendseconds; debug. writeline ("receive" + program. statisticsmessagecountspan + "messages spend:" + spendseconds + "seconds. ("+ speed. tostring ("0.00") + "/s)"); stopwatch. reset (); Index = 1 ;}} catch (exception ex) {debug. writeline (Ex );}}}}}}

Send message:

 Private   Void  Send (){  VaR Factory = New  Connectionfactory (program. brokeruri );  Using ( VaR Connection = Factory. createconnection ()){  Using ( VaR Session = Connection. createsession ()){ VaR Destination = Sessionutil. getdestination (Session, program. normalqueuedestination );  Using ( VaR Producer = Session. createproducer (destination) {connection. Start ();  VaR Mediataskid = 100000000000000  ;  VaR Stopwatch = New  Stopwatch (); VaR Index = 1  ;  For ( VaR I = 1 ; I <= program. messagecount; I ++ ){  If (Index = 1  ) {Stopwatch. Start ();}  Try {Mediataskid ++ ;  VaR Message = Session. createmapmessage (); Common. setmapmessage (message, common. getmessageobj (mediataskid. tostring (); producer. Send (Message); index ++ ;  If (Index = Program. statisticsmessagecountspan) {stopwatch. Stop (); VaR Spendseconds = Stopwatch. elapsed. totalseconds;  VaR Speed = program. statisticsmessagecountspan/ Spendseconds; debug. writeline (  "  Send  " + Program. statisticsmessagecountspan + "  Messages spend:  " + Spendseconds + "  Seconds .( " + Speed. tostring ( "  0.00  " ) + "  /S)  "  ); Stopwatch. Reset (); index = 1  ;}}  Catch (Exception ex) {Debug. writeline (Ex) ;}}} MessageBox. Show (  @"  Send done!  "  );} 

Simple listening encapsulation class:

Using system; using system. diagnostics; using system. threading; using Apache. NMS; namespace activemq. performancetest {public class activemqlistener {public event publish listener; private readonly consumer _ messageconsumer; private readonly timespan _ timeout; private readonly int _ sleepminutes; private bool _ listen; Public activemqlistener (publish messagecon) Sumer, timespan timeout, int sleepminutes) {_ messageconsumer = messageconsumer; _ timeout = timeout; _ sleepminutes = sleepminutes;} public void start () {_ Listen = true; startlistening ();} public void stop () {_ Listen = false;} private void startlistening () {While (_ listen) {var message = _ messageconsumer. receive (_ timeout); If (Message = NULL) {debug. writeline ("[% notice: activemqlistener sta RT "+ _ sleepminutes +" minutes sleep. %] "); thread. sleep (_ sleepminutes * 1000*60);} else {firerecieveevent (Message) ;}} private void firerecieveevent (Object message) {If (messagereceived! = NULL) {messagereceived (this, new messageeventargs (Message) ;}} Public Delegate void messagereceivedeventhandler (Object sender, messageeventargs ARGs); public class messageeventargs: eventargs {private readonly object _ message; public object message {get {return _ message ;}} public messageeventargs (Object message) {_message = message ;}}}

Complete code:

Http://files.cnblogs.com/CopyPaster/ActiveMQ.PerformanceTest.rar

 

 

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.