//amqp connection
var conn = require('amqp').createConnection({
url : 'amqp://qiaoxincheng:qiaoxincheng@localhost:5672', //账号:密码@主机:端口
vhost : '/', //虚拟主机
connectionTimeout : 0,//超时
heartbeat : 10 //心跳
}, {
reconnect : true //重连
});
conn.on('ready', function() {
console.log('ready');
conn.exchange('exchange-node', {
type : 'fanout', //路由方式
durable : true, //持久化
autoDelete : false //消息是否自动消毁
}, function(exchange) {
conn.queue('node-queue', {}, function(queue) {
queue.bind(exchange.name, '', function() {
console.log(' amqp exchenage bind: ' + exchange.name);
queue.subscribe(function(message, headers, deliveryInfo, messageObject) {
var raw = message.data.toString();
console.log(' msg: ' + raw); //消息输出
});
});
});
});
}).on('error', function(e) {
try {
conn.disconnect();
} catch (e) {
console.log(e.toString()); //错误输出
};
var conn = require('amqp').createConnection({
url : 'amqp://qiaoxincheng:qiaoxincheng@localhost:5672', //账号:密码@主机:端口
vhost : '/', //虚拟主机
connectionTimeout : 0,//超时
heartbeat : 10 //心跳
}, {
reconnect : true //重连
});
conn.on('ready', function() {
console.log('ready');
conn.exchange('exchange-node', {
type : 'fanout', //路由方式
durable : true, //持久化
autoDelete : false //消息是否自动消毁
}, function(exchange) {
conn.queue('node-queue', {}, function(queue) {
queue.bind(exchange.name, '', function() {
console.log(' amqp exchenage bind: ' + exchange.name);
queue.subscribe(function(message, headers, deliveryInfo, messageObject) {
var raw = message.data.toString();
console.log(' msg: ' + raw); //消息输出
});
});
});
});
}).on('error', function(e) {
try {
conn.disconnect();
} catch (e) {
console.log(e.toString()); //错误输出
};
});
<?php
$conn = new AMQPConnection(); //建立连接
if (!$conn->connect())
die('Cannot connect');
$channel = new AMQPChannel($conn); //生成一个会话
$exchange = new AMQPExchange($channel); //声明一个交换机
$exchange->setName('exchange-node'); //exchange名
$exchange->setType(AMQP_EX_TYPE_FANOUT); //交换类型
$exchange->setFlags(AMQP_DURABLE); //设置持久化
$exchange->declare(); //定义生成
$exchange->publish(json_encode(array('hello world'))); //生产消息到交换机,因为交换机声明的类型所以要接收的话还要绑定同样的KEY