zoukankan      html  css  js  c++  java
  • ActiveMQ Net 版本

    今天在网上查找了些关于 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 这个类,这个类中有个死循环,用于监听是否受到信息,如果收到信息触发信息收到事件。

  • 相关阅读:
    监控网速
    nginx与apache 对比 apache是同步多进程模型,一个连接对应一个进程;nginx是异步的,多个连接(万级别)可以对应一个进程
    shell 爬虫
    shell 读写远程数据库
    tmp
    交换分区 在dd命令执行期间 top 其消耗系统约14%的cpu,而mem占比约为0
    中间变量 加层 对解决问题的思路 逆序生成
    ALLOWED_HOSTS = ['*']
    搭建一个简单的Python的Web环境 监控服务器内存 线程 进程 网络
    小米加步枪
  • 原文地址:https://www.cnblogs.com/ssjylsg/p/2636724.html
Copyright © 2011-2022 走看看