.net客户端调用activeMQ代码

<!--[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 >

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
ExpandedBlockStart.gifContractedBlock.gif
{
publicclassMQ
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<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)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
IConnectionFactoryfactory
=newConnectionFactory(newUri("tcp://"+mqIp));

using(IConnectionconnection=factory.CreateConnection(mqUsername,mqPassword))
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
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
ExpandedBlockStart.gifContractedBlock.gif
{
classProgram
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
staticvoidMain(string[]args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
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);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值