我正在使用RabbitMQ和Spring Websockets通过STOMP将消息显示在网页上。我希望每个网页都能接收所有发送到交易所的消息(扇出)。
当前在网页上接收到消息,但是这种行为就像一个队列(而不是扇出),因为如果打开了2个网页并将10条消息添加到交换中,则每个网页会收到5条消息。
有谁知道需要更改哪些配置才能使用扇出交换?
Java脚本
var socket = new WebSocket("ws://localhost:8080/messaging-example/portfolio/websocket");
var stompClient = Stomp.over(socket);
var headers = {};
var connectCallback = function(frame) {
stompClient.subscribe("/queue/testQueue", function(message) {
document.body.innerHTML += "<p>" + message + "</p>";
}, { });
};
var errorCallback = function(frame) {
console.log("Connection Error");
};
stompClient.connect(headers, connectCallback, errorCallback);
弹簧
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/portfolio">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:stomp-broker-relay
relay-host="x.x.x.x"
relay-port="61613"
system-login="user1"
system-passcode="password"
client-login="user1"
client-passcode="password"
prefix="/queue"/>
</websocket:message-broker>
兔子MQ
"queues":[
{
"name":"testQueue",
"vhost":"/",
"durable":true,
"auto_delete":false,
"arguments":{
}
}
],
"exchanges":[
{
"name":"testExchange",
"vhost":"/",
"type":"fanout",
"durable":true,
"auto_delete":false,
"internal":false,
"arguments":{
}
}
],
"bindings":[
{
"source":"testExchange",
"vhost":"/",
"destination":"testQueue",
"destination_type":"queue",
"routing_key":"",
"arguments":{
}
}
]
最佳答案
我通过post on rabbitmq user group的Destinations部分中的rabbit mq stomp documentation找到了答案。
为了通过名为testExchange
的扇出交换机指定对队列的预订,javascript中的连接字符串应为/exchange/testExchange/testQueue
。以下两项更改导致成功订阅,因此所有页面均收到所有消息:
弹簧
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/portfolio">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:stomp-broker-relay
relay-host="x.x.x.x"
relay-port="61613"
system-login="user1"
system-passcode="password"
client-login="user1"
client-passcode="password"
prefix="/exchange"/>
</websocket:message-broker>
Java脚本
var socket = new WebSocket("ws://localhost:8080/messaging-example/portfolio/websocket");
var stompClient = Stomp.over(socket);
var headers = {};
var connectCallback = function(frame) {
stompClient.subscribe("/exchange/testExchange/testQueue", function(message) {
document.body.innerHTML += "<p>" + message + "</p>";
}, { });
};
var errorCallback = function(frame) {
console.log("Connection Error");
};
stompClient.connect(headers, connectCallback, errorCallback);