关于Java:带有Fanout Exchange的Stomp Websocket

Stomp Websocket with Fanout Exchange

我正在使用RabbitMQ和Spring Websockets通过STOMP将消息显示在网页上。 我希望每个网页都能接收所有发送到交易所的消息(扇出)。

当前在网页上接收到消息,但是这种行为就像一个队列(而不是扇出),因为如果打开了2个网页并将10条消息添加到交换中,则每个网页会收到5条消息。

有谁知道需要更改哪些配置才能使用扇出交换?

Java脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);

弹簧

1
2
3
4
5
6
7
8
9
10
11
12
13
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"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":{  

      }
   }
]


感谢Rabbit mq stomp文档的Destinations部分中关于rabbitmq用户组的帖子,我找到了答案。

为了通过名为testExchange的扇出交换机指定对队列的预订,javascript中的连接字符串应为/exchange/testExchange/testQueue。 以下两项更改导致成功订阅,因此所有页面均收到所有消息:

弹簧

1
2
3
4
5
6
7
8
9
10
11
12
13
<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脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);