官方网站下载地点:
解压缩apache-activemq-5.10.0-bin.zip,然后双击apache-activemq-5.10.0\bin\win32\activemq.bat运行ActiveMQ措施。
启动ActiveMQ以后,,可以使用浏览器登陆::8161/admin/验证, 默认用户名是:admin 暗码是:admin
(前提是配置好好JDK环境,能打开console代表MQ运行告成)
同时下载.net版Dll:Apache.NMS-1.6.0-bin.zip和Apache.NMS.ActiveMQ-1.6.4-bin.zip
都从这里下载:
引用ActiveMQ类库:
(1)Apache.NMS.dll路径:\Apache.NMS.ActiveMQ-1.7.2-bin\lib\Apache.NMS\net-3.5
(2)Apache.NMS.ActiveMQ.dll路径:\Apache.NMS.ActiveMQ-1.7.2-bin\build\net-3.5\debug
3.MQ例子(出产者)public Form1()
{
InitializeComponent();
InitProducer();
}
private IConnectionFactory factory;
public void InitProducer()
{
try
{
//初始化工厂
factory = new ConnectionFactory("tcp://localhost:61616");
}
catch
{
lbMessage.Text = "初始化掉败";
}
}
private void button1_Click(object sender, EventArgs e)
{
//成立工厂连接
using (IConnection connection = factory.CreateConnection())
{
//通过工厂连接创建Session会话
using (ISession session = connection.CreateSession())
{
//通过会话创建出产者,要领里new出来MQ的Queue
IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"));
//创建一个发送动静的东西
ITextMessage message = prod.CreateTextMessage();
XmlDocument Doc = new XmlDocument();
Doc.LoadXml("<?xml version=‘1.0‘ encoding=‘UTF-8‘?><flightroute><flight><flightinfo><acid>CCA1501</acid><runway>13L</runway><gate>N115</gate><cockpitdirection>180</cockpitdirection><deparr>DEP</deparr></flightinfo></flight</flightroute>");
message.Text = Doc.InnerXml; //给这个动静东西赋实际的动静
//设置动静东西的属性,是Queue的过滤条件也是P2P的独一指定属性
message.Properties.SetString("filter","demo");
prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
lbMessage.Text = "发送告成!";
Text.Text = "";
Text.Focus();
}
}
}
可以点击按钮发送一些对象
接收端代码(consumer)
public Form2()
{
InitializeComponent();
InitConsumer();
}
public void InitConsumer()
{
//创建连接工厂
IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
//通过工厂创建连接
IConnection connection = factory.CreateConnection();
//连接处事器真个标识
connection.ClientId = "firstQueueListener";
//启动连接
connection.Start();
//通过连接创建对话
ISession session = connection.CreateSession();
//通过会话创建一个消费者
IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"),"filter = ‘demo‘");
//注册监听事件
consumer.Listener += new MessageListener(consumer_Listener);
}
void consumer_Listener(IMessage message)
{
ITextMessage msg = (ITextMessage)message;
ReceiveMessage.Invoke(new DelegateRevMessage(RevMessage), msg);
}
public delegate void DelegateRevMessage(ITextMessage message);
public void RevMessage(ITextMessage message)
{
ReceiveMessage.Text += string.Format(@"接收到:{0}{1}", message.Text, Environment.NewLine);