【问题标题】:Stomp Websocket with Fanout Exchange使用 Fanout Exchange 踩踏 Websocket
【发布时间】: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


【解决方案1】:

感谢rabbit mq stomp documentation 的“目的地”部分中的post on rabbitmq user group,我找到了答案。

为了通过名为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);

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2015-07-09
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多