ActiveMQ系列(1) - 使用入门
- 1.背景:
- 2.系统环境:
windows 10 企业版本 64位
- 3.获取 与 安装 :
a) http://activemq.apache.org/ 自行选择中意版本(或 文章底部,我上传了,是目前的最新版本), 注意看域名,apache,所以肯定离不开 jdk了, jdk获取:


- 4 安装使用:







- 5. 参考 p2p 案例代码:
namespace TestActiveMQ
{
class Program
{
private static IConnectionFactory factory //需要引用 Apache.NMS;
= new ConnectionFactory("tcp://localhost:61616");//需要引用Apache.NMS.ActiveMQ; static void Main(string[] args)
{
//建立连接
using (IConnection connection = factory.CreateConnection())
{
//通过连接创建session会话
using (ISession session = connection.CreateSession())
{
//通过会话创建生产者,方法里面new出来的是MQ中的Queue
IMessageProducer producer = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"));
//创建一个发送的消息对象
ITextMessage message = producer.CreateTextMessage(); //给这个对象赋实际的消息
message.Text = "";
//设置消息对象的属性,这个很重要哦,是Queue的过滤条件,也是P2P消息的唯一指定属性
message.Properties.SetString("filter", "demo");
//生产者把消息发送出去,几个枚举参数MsgDeliveryMode是否长链,MsgPriority消息优先级别,发送最小单位,当然还有其他重载
producer.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue); Console.WriteLine("发送成功!");
Console.ReadLine();
}
} }
}
}
客户端:
public partial class Form1 : Form
{
public Form1()
{
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();
//通过会话创建一个消费者,这里就是Queue这种会话类型的监听参数设置
IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"), "filter='demo'");
//注册监听事件
consumer.Listener += new MessageListener(consumer_Listener);
//connection.Stop();
//connection.Close(); } void consumer_Listener(IMessage message)
{
ITextMessage msg = (ITextMessage)message;
//异步调用下,否则无法回归主线程
tbReceiveMessage.Invoke(new DelegateRevMessage(RevMessage), msg); } public delegate void DelegateRevMessage(ITextMessage message); public void RevMessage(ITextMessage message)
{
tbReceiveMessage.Text += string.Format(@"接收到:{0}{1}", message.Text, Environment.NewLine);
} }
执行结果:

ActiveMQ系列(1) - 使用入门的更多相关文章
- 分布式学习系列【dubbo入门实践】
分布式学习系列[dubbo入门实践] dubbo架构 组成部分:provider,consumer,registry,monitor: provider,consumer注册,订阅类似于消息队列的注册 ...
- 「译」JUnit 5 系列:基础入门
原文地址:http://blog.codefx.org/libraries/junit-5-basics/ 原文日期:25, Feb, 2016 译文首发:Linesh 的博客:JUnit 5 系列: ...
- ActiveMQ系列之五:ActiveMQ的Transport
连接到ActiveMQ Connector:ActiveMQ提供的,用来实现连接通讯的功能.包括:client-to-broker.broker-to-broker. ActiveMQ允许客户端使用多 ...
- ActiveMQ系列之四:用ActiveMQ构建应用
Broker:相当于一个ActiveMQ服务器实例 命令行启动参数示例如下: 1:activemq start :使用默认的activemq.xml来启动 2:activemq start xbean ...
- ActiveMQ系列之三:理解和掌握JMS
JMS是什么 JMS Java Message Service,Java消息服务,是Java EE中的一个技术. JMS规范 JMS定义了Java 中访问消息中间件的接口,并没有给予实现,实现JMS ...
- ActiveMQ系列之二:ActiveMQ安装和基本使用
下载并安装ActiveMQ服务器端 1:从http://activemq.apache.org/download.html下载最新的ActiveMQ 2:直接解压,然后拷贝到你要安装的位置就好了 启动 ...
- ActiveMQ系列之一:ActiveMQ简介
ActiveMQ是什么 ActiveMQ是Apache推出的,一款开源的,完全支持JMS1.1和J2EE 1.4规范的JMS Provider实现的消息中间件 (Message Oriented ...
- SpringBoot系列: RestTemplate 快速入门
====================================相关的文章====================================SpringBoot系列: 与Spring R ...
- Go基础系列:channel入门
Go channel系列: channel入门 为select设置超时时间 nil channel用法示例 双层channel用法示例 指定goroutine的执行顺序 channel基础 chann ...
- elk系列1之入门安装与基本操作【转】
preface 我们每天都要查看服务器的日志,一方面是为了开发的同事翻找日志,另一方面是巡检服务器查看日志,而随着服务器数量以及越来越多的业务上线,日志越来越多,人肉运维相当痛苦了,此时,参考现在非常 ...
随机推荐
- EPUB弹出窗口式脚注
网上搜到一些国学典籍的EPUB版,虽有古人的注解,但正文和注解混排在一起,当我只想迅速读正文的时候比较碍眼.于是研究了一下 EPUB3 中有关脚注(footnote)的规格定义,写了一个 Python ...
- lightoj 1012
水题,dfs #include<cstdio> #include<string> #include<cstring> #include<iostream> ...
- 大数加法,A+B
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- asp.net基础
这篇主要讲述以下基础知识: Request对象 Response对象 Server对象 Cookie对象 Application对象 ViewState对象 <%%>与<%=%> ...
- JZ2440开发笔记(5)——通过按键点亮LED
在JZ2440中,点亮LED就是给LED的控制位设置为输出,数据位设置为低电平,而通过按键点亮LED,就需要将按键对应的控制位设置为输出. 下面是JZ2440的3个LED电路图: 下面是JZ2440的 ...
- 微软Azure运营方世纪互联遭做空后强劲反弹
http://www.cnblogs.com/sennly/p/4135594.html 最近几天微软Azure的运营方世纪互联被Trinity做空,股价震荡巨大,先让我们回顾下整个事件: 9月11日 ...
- HW3.19
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- devexpress中gridcontrol头部添加垂直线(右边框)
winform开发,用devexpress中的gridcontrol控件,头部默认是3D样式,当客户希望像内容一样扁平化显示且需要添加垂直线(右边框)时恶梦开始了..经过一阵摸索发现可以这样解决: 1 ...
- A Tour of Go For is Go's "while"
At that point you can drop the semicolons(分号): C's while is spelled for in Go. package main import & ...
- HTML几类标签的应用总结
打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background 背景壁纸.图片 text 文字颜色 topmargin 上边距 leftm ...