c# ActiveMQ 类
2015-11-11 13:37
567 查看
using System;
using System.Collections.Generic;
using System.Text;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
namespace yangguang.weixinandsogou
{
public struct Property
{
public string name;
public string value;
}
class MQ
{
private string URI;
private string TOPIC;
private string USERNAME;
private string PASSWORD;
private IConnectionFactory factory;
private IConnection connection;
private ISession session;
private IMessageProducer producer;
public string uri
{
set { URI = value; }
get { return URI; }
}
public string topic
{
set { TOPIC = value; }
get { return TOPIC; }
}
public string username
{
set { USERNAME = value; }
}
public string password
{
set { PASSWORD = value; }
}
public MQ()
{
producer = null;
factory = null;
connection = null;
session = null;
}
~MQ()
{
if (producer != null)
{
producer.Dispose();
}
Close();
}
public void Start()
{
factory = new ConnectionFactory(URI);
if (USERNAME != "")
{
connection = factory.CreateConnection(USERNAME, PASSWORD);
}
else
{
connection = factory.CreateConnection();
}
connection.Start();
session = connection.CreateSession();
}
public void Close()
{
if (session != null)
{
session.Close();
}
if (connection != null)
{
connection.Stop();
connection.Close();
}
}
public void CreateProducer(bool blnTopic, string strTopicName)
{
if (blnTopic)
{
producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(strTopicName));
}
else
{
producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(strTopicName));
}
}
public void CreateProducer(bool blnTopic)
{
if (blnTopic)
{
producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(TOPIC));
}
else
{
producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(TOPIC));
}
}
public IMessageConsumer CreateConsumer(bool blnTopic, string strTopicName)
{
if (blnTopic)
{
return session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(strTopicName));
}
else
{
return session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(strTopicName));
}
}
public IMessageConsumer CreateConsumer(bool blnTopic, string strTopicName, string strSelector)
{
if (strSelector == "")
{
// GlobalFunction.MsgBox("MQ selector不能为空");
return null;
}
if (blnTopic)
{
return session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(strTopicName), strSelector, false);
}
else
{
return session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(strTopicName), strSelector, false);
}
}
public void SendMQMessage(string strText)
{
ITextMessage msg = producer.CreateTextMessage();
msg.Text = strText;
producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
}
public void SendMQMessage(string strText, List<Property> lstProperty)
{
try
{
ITextMessage msg = producer.CreateTextMessage();
msg.Text = strText;
foreach (Property prop in lstProperty)
{
msg.Properties.SetString(prop.name, prop.value);
}
producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
}
catch (System.Exception ex)
{
//GlobalFunction.MsgBoxException(ex.Message, "SendMQMessage");
}
}
}
}
using System.Collections.Generic;
using System.Text;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
namespace yangguang.weixinandsogou
{
public struct Property
{
public string name;
public string value;
}
class MQ
{
private string URI;
private string TOPIC;
private string USERNAME;
private string PASSWORD;
private IConnectionFactory factory;
private IConnection connection;
private ISession session;
private IMessageProducer producer;
public string uri
{
set { URI = value; }
get { return URI; }
}
public string topic
{
set { TOPIC = value; }
get { return TOPIC; }
}
public string username
{
set { USERNAME = value; }
}
public string password
{
set { PASSWORD = value; }
}
public MQ()
{
producer = null;
factory = null;
connection = null;
session = null;
}
~MQ()
{
if (producer != null)
{
producer.Dispose();
}
Close();
}
public void Start()
{
factory = new ConnectionFactory(URI);
if (USERNAME != "")
{
connection = factory.CreateConnection(USERNAME, PASSWORD);
}
else
{
connection = factory.CreateConnection();
}
connection.Start();
session = connection.CreateSession();
}
public void Close()
{
if (session != null)
{
session.Close();
}
if (connection != null)
{
connection.Stop();
connection.Close();
}
}
public void CreateProducer(bool blnTopic, string strTopicName)
{
if (blnTopic)
{
producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(strTopicName));
}
else
{
producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(strTopicName));
}
}
public void CreateProducer(bool blnTopic)
{
if (blnTopic)
{
producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(TOPIC));
}
else
{
producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(TOPIC));
}
}
public IMessageConsumer CreateConsumer(bool blnTopic, string strTopicName)
{
if (blnTopic)
{
return session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(strTopicName));
}
else
{
return session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(strTopicName));
}
}
public IMessageConsumer CreateConsumer(bool blnTopic, string strTopicName, string strSelector)
{
if (strSelector == "")
{
// GlobalFunction.MsgBox("MQ selector不能为空");
return null;
}
if (blnTopic)
{
return session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(strTopicName), strSelector, false);
}
else
{
return session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(strTopicName), strSelector, false);
}
}
public void SendMQMessage(string strText)
{
ITextMessage msg = producer.CreateTextMessage();
msg.Text = strText;
producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
}
public void SendMQMessage(string strText, List<Property> lstProperty)
{
try
{
ITextMessage msg = producer.CreateTextMessage();
msg.Text = strText;
foreach (Property prop in lstProperty)
{
msg.Properties.SetString(prop.name, prop.value);
}
producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
}
catch (System.Exception ex)
{
//GlobalFunction.MsgBoxException(ex.Message, "SendMQMessage");
}
}
}
}
相关文章推荐
- 用代码控制DateTimePicker弹出,C# SendKeys用法
- c# webbrower 代理 类 IEProxy
- c# 如何使用DllImport
- C#中的字符串处理——找出最长数字子串
- (原创)c#学习笔记09--定义类06--接口和抽象类
- C#换行
- c#后台访问接口
- (原创)c#学习笔记09--定义类05--类库项目
- c#代码实现软件开机自启动
- C#反射的使用(Reflection)
- C#调用GDI+1.1中的函数实现高斯模糊、USM锐化等经典效果。
- C# PDFBox 解析PDF文件
- (原创)c#学习笔记09--定义类03--构造函数和析构函数
- c#处理空白字符
- C#中Config文件中,特殊符号的书写方法。
- c#之向ftp服务器传文件
- C#操作XML文件
- C# 添加,修改,删除Xml节点 摘录
- C#删除xml指定节点
- C# 添加,修改,删除Xml节点 摘录