【发布时间】:2017-06-30 12:47:21
【问题描述】:
我正在使用 RabbitMQ 和 Spring Websockets 来通过 STOMP 在网页上显示消息。我希望每个网页都能接收发送到交易所的所有消息(扇出)。
当前在网页上接收消息,但其行为类似于队列(而不是扇出),如果打开 2 个网页并将 10 条消息添加到交换中,则每个网页会收到 5 条消息。
有人知道使用扇出交换需要更改什么配置吗?
Javascript
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>
RabbitMQ
"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":{
}
}
]
【问题讨论】:
-
您是否在两个网页的同一个队列中收听?如果是,那么您不能向两个侦听器发送相同的消息。是load balanced
-
这是相同的队列定义@RaiyanMohammed,但我想通过扇出交换连接,以便所有订阅者收到相同的消息。我现在已经解决了这个问题 - 请参阅下面的答案
标签: java spring websocket rabbitmq stomp