<!--[endif]-->
1、添加对 Apache.NMS 和Apache.NMS.ActiveMQ引用;
ActiveMQ .NET http://activemq.apache.org/nms/index.html
2、在webconfig中添加调用MQ所需的MQIP,MQName,MQUserName,MQPassword;
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<
appSettings
>
< addkey = " MQIP " value = " XX " />
< addkey = " MQName " value = " XX " />
< addkey = " MQUsername " value = " XX " />
< addkey = " MQPassword " value = " XX " />
</ appSettings >
< addkey = " MQIP " value = " XX " />
< addkey = " MQName " value = " XX " />
< addkey = " MQUsername " value = " XX " />
< addkey = " MQPassword " value = " XX " />
</ appSettings >
3、创建一个通用的调用类。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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
{
publicclassMQ
{
/**////<summary>
///发送消息
///</summary>
///<paramname="mqIp">ip地址</param>
///<paramname="mqName">mq名称</param>
///<paramname="mqUsername">用户名</param>
///<paramname="mqPassword">密码</param>
///<paramname="strParam">消息参数</param>
///<paramname="intParam">消息参数</param>
publicstaticvoidSendMessage(stringmqIp,stringmqName,stringmqUsername,stringmqPassword,stringstrParam,intintParam)
{
IConnectionFactoryfactory=newConnectionFactory(newUri("tcp://"+mqIp));
using(IConnectionconnection=factory.CreateConnection(mqUsername,mqPassword))
{
Console.WriteLine("创建一个连接!");
ISessionsession=connection.CreateSession();
ActiveMQQueuequeue=(ActiveMQQueue)session.GetQueue(mqName);
IMessageProducerproducer=session.CreateProducer(queue);
producer.Persistent=true;
//发送消息
IMapMessagerequest=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
{
publicclassMQ
{
/**////<summary>
///发送消息
///</summary>
///<paramname="mqIp">ip地址</param>
///<paramname="mqName">mq名称</param>
///<paramname="mqUsername">用户名</param>
///<paramname="mqPassword">密码</param>
///<paramname="strParam">消息参数</param>
///<paramname="intParam">消息参数</param>
publicstaticvoidSendMessage(stringmqIp,stringmqName,stringmqUsername,stringmqPassword,stringstrParam,intintParam)
{
IConnectionFactoryfactory=newConnectionFactory(newUri("tcp://"+mqIp));
using(IConnectionconnection=factory.CreateConnection(mqUsername,mqPassword))
{
Console.WriteLine("创建一个连接!");
ISessionsession=connection.CreateSession();
ActiveMQQueuequeue=(ActiveMQQueue)session.GetQueue(mqName);
IMessageProducerproducer=session.CreateProducer(queue);
producer.Persistent=true;
//发送消息
IMapMessagerequest=session.CreateMapMessage();
request.Body.SetString("strParam",strParam);//具体参数
request.Body.SetInt("intParam",intParam);
producer.Send(queue,request);
}
}
}
}
4、调用
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lee.Client
{
classProgram
{
staticvoidMain(string[]args)
{
stringmqIp=System.Configuration.ConfigurationManager.AppSettings["MQIP"].ToString();
stringmqName=System.Configuration.ConfigurationManager.AppSettings["MQName"].ToString();
stringmqUsername=System.Configuration.ConfigurationManager.AppSettings["MQUserName"].ToString();
stringmqPassword=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
{
classProgram
{
staticvoidMain(string[]args)
{
stringmqIp=System.Configuration.ConfigurationManager.AppSettings["MQIP"].ToString();
stringmqName=System.Configuration.ConfigurationManager.AppSettings["MQName"].ToString();
stringmqUsername=System.Configuration.ConfigurationManager.AppSettings["MQUserName"].ToString();
stringmqPassword=System.Configuration.ConfigurationManager.AppSettings["MQPassword"].ToString();
MQ.SendMessage(mqIp,mqName,mqUsername,mqPassword,"字符型参数",0);
}
}
}