.net客户端调用activeMQ代码
1、添加对 Apache.NMS 和Apache.NMS.ActiveMQ引用;
ActiveMQ .NET http://activemq.apache.org/nms/index.html
2、在webconfig中添加调用MQ所需的MQIP,MQName,MQUserName,MQPassword;
<appSettings>
<add key="MQIP" value="XX" />
<add key="MQName" value="XX" />
<add key="MQUsername" value="XX" />
<add key="MQPassword" value="XX" />
</appSettings>
<add key="MQIP" value="XX" />
<add key="MQName" value="XX" />
<add key="MQUsername" value="XX" />
<add key="MQPassword" value="XX" />
</appSettings>
3、创建一个通用的调用类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;
namespace Lee.Client
{
public class MQ
{
/// <summary>
/// 发送消息
/// </summary>
/// <param name="mqIp">ip地址</param>
/// <param name="mqName">mq名称</param>
/// <param name="mqUsername">用户名</param>
/// <param name="mqPassword">密码</param>
/// <param name="strParam">消息参数</param>
/// <param name="intParam">消息参数</param>
public static void SendMessage(string mqIp, string mqName, string mqUsername, string mqPassword, string strParam, int intParam)
{
IConnectionFactory factory = new ConnectionFactory(new Uri("tcp://" + mqIp));
using (IConnection connection = factory.CreateConnection(mqUsername, mqPassword))
{
Console.WriteLine("创建一个连接!");
ISession session = connection.CreateSession();
ActiveMQQueue queue = (ActiveMQQueue)session.GetQueue(mqName);
IMessageProducer producer = session.CreateProducer(queue);
producer.Persistent = true;
// 发送消息
IMapMessage request = session.CreateMapMessage();
request.Body.SetString("strParam", strParam);//具体参数
request.Body.SetInt("intParam", intParam);
producer.Send(queue, request);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;
namespace Lee.Client
{
public class MQ
{
/// <summary>
/// 发送消息
/// </summary>
/// <param name="mqIp">ip地址</param>
/// <param name="mqName">mq名称</param>
/// <param name="mqUsername">用户名</param>
/// <param name="mqPassword">密码</param>
/// <param name="strParam">消息参数</param>
/// <param name="intParam">消息参数</param>
public static void SendMessage(string mqIp, string mqName, string mqUsername, string mqPassword, string strParam, int intParam)
{
IConnectionFactory factory = new ConnectionFactory(new Uri("tcp://" + mqIp));
using (IConnection connection = factory.CreateConnection(mqUsername, mqPassword))
{
Console.WriteLine("创建一个连接!");
ISession session = connection.CreateSession();
ActiveMQQueue queue = (ActiveMQQueue)session.GetQueue(mqName);
IMessageProducer producer = session.CreateProducer(queue);
producer.Persistent = true;
// 发送消息
IMapMessage request = session.CreateMapMessage();
request.Body.SetString("strParam", strParam);//具体参数
request.Body.SetInt("intParam", intParam);
producer.Send(queue, request);
}
}
}
}
4、调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lee.Client
{
class Program
{
static void Main(string[] args)
{
string mqIp = System.Configuration.ConfigurationManager.AppSettings["MQIP"].ToString();
string mqName = System.Configuration.ConfigurationManager.AppSettings["MQName"].ToString();
string mqUsername = System.Configuration.ConfigurationManager.AppSettings["MQUserName"].ToString();
string mqPassword = System.Configuration.ConfigurationManager.AppSettings["MQPassword"].ToString();
MQ.SendMessage(mqIp, mqName, mqUsername, mqPassword,"字符型参数",0);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lee.Client
{
class Program
{
static void Main(string[] args)
{
string mqIp = System.Configuration.ConfigurationManager.AppSettings["MQIP"].ToString();
string mqName = System.Configuration.ConfigurationManager.AppSettings["MQName"].ToString();
string mqUsername = System.Configuration.ConfigurationManager.AppSettings["MQUserName"].ToString();
string mqPassword = System.Configuration.ConfigurationManager.AppSettings["MQPassword"].ToString();
MQ.SendMessage(mqIp, mqName, mqUsername, mqPassword,"字符型参数",0);
}
}
}
-------------20100107----------
改进调用类
IMessageProducer和ISession都继承了IDisposable接口,所以创建对象producer和session时使用Using。
public static void SendMessage(string ip, string mqName, string mqUsername, string mqPassword, string userID, string subsysID, int cent, string summary)
{
IConnectionFactory factory = new ConnectionFactory(new Uri("tcp://" + ip));
using (IConnection connection = factory.CreateConnection(mqUsername, mqPassword))
{
using (ISession session = connection.CreateSession())
{
ActiveMQQueue destination = (ActiveMQQueue)session.GetQueue(mqName);
using (IMessageProducer producer = session.CreateProducer(destination))
{
producer.Persistent = true;
IMapMessage request = session.CreateMapMessage();
request.Body.SetString("userID", userID);
request.Body.SetString("subsysID", subsysID);
request.Body.SetInt("cent", cent);
request.Body.SetString("summary", summary);
producer.Send(destination, request);
}
}
}
}
{
IConnectionFactory factory = new ConnectionFactory(new Uri("tcp://" + ip));
using (IConnection connection = factory.CreateConnection(mqUsername, mqPassword))
{
using (ISession session = connection.CreateSession())
{
ActiveMQQueue destination = (ActiveMQQueue)session.GetQueue(mqName);
using (IMessageProducer producer = session.CreateProducer(destination))
{
producer.Persistent = true;
IMapMessage request = session.CreateMapMessage();
request.Body.SetString("userID", userID);
request.Body.SetString("subsysID", subsysID);
request.Body.SetInt("cent", cent);
request.Body.SetString("summary", summary);
producer.Send(destination, request);
}
}
}
}
作者:青羽