简单尝试了node下的ActiveMQ
1、下载apache-activemq-5.9.0,执行bat文件;
2、登录http://localhost:8161/admin可查看其管理后台;
3、安装stomp客户端 npm install stomp-client
4、修改基础代码如下:
var Stomp = require('stomp-client'); var readline = require('readline');
//创建两个不同的topic var topic1 = '/topic/topic1'; var topic2 = '/topic/topic2'; var rl = readline.createInterface({ input : process.stdin, output : process.stdout, terminal : false }); //1. publisher-subscribe 定义一个或者多个topic,然后给这些topic发送消息 //2. p2p 双方通过一个quene来发送、接收消息,一个quene只有一个发送、一个接收 //3. request-response 双方各有一个quene作为彼此的收发,相当于两个p2p var publisher = new Stomp('127.0.0.1', 61613, '', ''); //发布者 var subscriberA = new Stomp('127.0.0.1', 61613, '', ''); //订阅者A var subscriberB = new Stomp('127.0.0.1', 61613, '', ''); //订阅者B publisher.connect(function(sessionId) { rl.on('line', function(line) { switch (line.split(":")[0]) { case 'topic1': publisher.publish(topic1, line.split(":")[1]); break; case 'topic2': publisher.publish(topic2, line.split(":")[1]); break; default: console.log('commond error `' + line.trim() + '`'); break; } rl.prompt(); }).on('close', function() { console.log('close test project!'); process.exit(0); }); }); subscriberA.connect(function(sessionId) { subscriberA.subscribe(topic1, function(body, headers) { console.log('subscriberA From Topic1:', body); }); subscriberA.subscribe(topic2, function(body, headers) { console.log('subscriberA From Topic2:', body); }); }); subscriberB.connect(function(sessionId) { subscriberB.subscribe(topic1, function(body, headers) { console.log('subscriberB From Topic1:', body); }); subscriberB.subscribe(topic2, function(body, headers) { console.log('subscriberB From Topic2:', body); }); });
输入不同命令后,控制台输出如下:
同时在管理后台的topic栏目下,查看创建的topic