今天在网上查找了些关于 ActiveMQ Net 版本的一些信息,这些代码有的借鉴了网上的一些代码,现做一些简单的纪要:

1. 发送端:

View Code
 1 public class SendActiveMQ : ActiveMQ, ISendActiveMQ
 2     {
 3         private ISession _session;
 4         private IConnection _connection;
 5         public SendActiveMQ(string brokerUri, string queueDestination)
 6             : base(brokerUri, queueDestination)
 7         {
 8             ConnectionFactory factory = new ConnectionFactory(this.BrokerUri);
 9             _connection = factory.CreateConnection();
10             _session = _connection.CreateSession();
11             //Apache.NMS.Util.SessionUtil.DeleteDestination(_session,queueDestination);
12         }
13 
14         public ISession Session
15         {
16             get { return _session; }
17         }
18 
19         public void SendMessage(IMessage message)
20         {
21             IDestination destination = SessionUtil.GetDestination(Session, this.QueueDestination);
22             using (IMessageProducer producer = Session.CreateProducer(destination))
23             {
24                 _connection.Start();
25                 producer.Send(message);
26             }
27         }
28 
29         public void Dispose()
30         {
31             this.Session.Close();
32             this._connection.Close();
33         }
34     }

2. 接受端:

View Code
 1  public class ReceiveActiveMQ : ActiveMQ, IReceiveActiveMQ
 2     {
 3         public event MessageReceivedEventHandler OnReceiveMessageEventHandler;
 4         private ActiveMqListener _listener;
 5 
 6         public ReceiveActiveMQ(string brokerUri, string queueDestination)
 7             : base(brokerUri, queueDestination)
 8         {
 9 
10         }
11         public void Stop()
12         {
13             _listener.Stop();
14         }
15        
16         public void Start()
17         {
18             ConnectionFactory factory = new ConnectionFactory(BrokerUri);
19             TimeSpan timeout = new TimeSpan(0, 0, 10);
20             using (IConnection connection = factory.CreateConnection())
21             {
22                 using (ISession session = connection.CreateSession())
23                 {
24                     IDestination destination = SessionUtil.GetDestination(session, QueueDestination);
25                     using (IMessageConsumer consumer = session.CreateConsumer(destination))
26                     {
27                         connection.Start();
28                         _listener = new ActiveMqListener(consumer, timeout);
29                         _listener.MessageReceived += new MessageReceivedEventHandler(OnMessageReceived);
30                         _listener.Start();
31                     }
32                 }
33             }
34         }
35 
36         void OnMessageReceived(object sender, MessageEventArgs args)
37         {
38             if (this.OnReceiveMessageEventHandler != null)
39             {
40                 this.OnReceiveMessageEventHandler(sender, args);
41             }
42         }
43 
44         public void Dispose()
45         {
46             if (this._listener != null)
47             {
48                 this._listener.Stop();
49             }
50         }
51     }

接收端中使用了个ActiveMqListener 这个类,这个类中有个死循环,用于监听是否受到信息,如果收到信息触发信息收到事件。

 

posted on 2012-08-13 17:17  刚子85  阅读(367)  评论(0编辑  收藏  举报