using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
namespace SiaSun.LMS.Common
{
public class ActiveMsg
{
#region 属性
private string _url;
/// 服务地址
/// 服务地址
///
public string url
{
get
{
return this._url;
}
set
{
this._url = value;
}
}
private IConnection _connection;
/// 连接
/// 连接
///
public IConnection connection
{
get
{
return this._connection;
}
set
{
this._connection = value;
}
}
#endregion
#region 方法
/// 构造
/// 构造
///
public ActiveMsg()
{
this.url = SiaSun.LMS.Common.StringUtil.GetConfig("ActiveMQUrl");
IConnectionFactory factory = new ConnectionFactory(this.url);
this.connection = factory.CreateConnection();
}
/// 析构
/// 析构
///
~ActiveMsg()
{
}
/// 发送消息
/// 发送消息
///
/// 消息类型
/// 消息体
/// 返回原因
/// 返回结果
public bool SendMsg( string sTopic, string sMsg, out string sResult)
{
bool bResult = true;
sResult = string.Empty;
IConnectionFactory factory = new ConnectionFactory(this._url);
try
{
//Create the Session
using (ISession session = this.connection.CreateSession())
{
//Create the Producer for the topic/queue
IMessageProducer prod = session.CreateProducer(
new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(sTopic));
//Send Messages
ITextMessage msg = prod.CreateTextMessage();
msg.Text = sMsg;
prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
}
}
catch (Exception ex)
{
bResult = false;
sResult = ex.Message.ToString();
}
finally
{
}
return bResult;
}
#endregion
}
}