使用扇出交换来踩踏Websocket

时间:2017-06-30 12:47:22

标签: java spring websocket rabbitmq stomp

我正在使用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":{  

      }
   }
]

1 个答案:

答案 0 :(得分:3)

我在post on rabbitmq user group的“目的地”部分感谢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>

<强>的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("/exchange/testExchange/testQueue", function(message) {
        document.body.innerHTML += "<p>" + message + "</p>";
    }, { });
};
var errorCallback = function(frame) {
    console.log("Connection Error"); 
};
stompClient.connect(headers, connectCallback, errorCallback);