spring-4 – 使用Spring 4 Websockets声明并使用RabbitMQ

在我们当前的应用程序中,我们以这种方式使用Spring AMQP:

<rabbit:connection-factory  id="cachingConnectionFactory" 
                            username="${rabbitmq.connection.username}"
                            password="${rabbitmq.connection.password}"
                            host="${rabbitmq.connection.host}" 
                            port="${rabbitmq.connection.port}"
                            executor="rabbitmqPoolTaskExecutor"
                            requested-heartbeat="${rabbitmq.connection.requested-heartbeat}"
                            channel-cache-size="${rabbitmq.connection.channel-cache-size}"
                            virtual-host="${rabbitmq.connection.virtual-host}" />

<rabbit:admin   id="adminRabbit" 
                connection-factory="cachingConnectionFactory"
                auto-startup="true" />

<rabbit:template    id="rabbitTemplate"
                    connection-factory="cachingConnectionFactory"
                    exchange="v1.general.exchange"
                    message-converter="jsonMessageConverter" 
                    encoding="${rabbitmq.template.encoding}"
                    channel-transacted="${rabbitmq.template.channel-transacted}" />

<rabbit:queue id="v1.queue.1" name="v1.queue.1"  />
<rabbit:queue id="v1.queue.2" name="v1.queue.2" />
<rabbit:queue id="v1.queue.3" name="v1.queue.3" />

<fanout-exchange name="v1.general.exchange" xmlns="http://www.springframework.org/schema/rabbit" >
    <bindings>
        <binding queue="v1.queue.1" />
        <binding queue="v1.queue.2" />
        <binding queue="v1.queue.3" />
    </bindings>
</fanout-exchange>

<listener-container xmlns="http://www.springframework.org/schema/rabbit"
                    connection-factory="cachingConnectionFactory" 
                    message-converter="jsonMessageConverter" 
                    task-executor="rabbitmqPoolTaskExecutor"
                    auto-startup="${rabbitmq.listener-container.auto-startup}"
                    concurrency="${rabbitmq.listener-container.concurrency}"
                    channel-transacted="${rabbitmq.listener-container.channel-transacted}"
                    prefetch="${rabbitmq.listener-container.prefetch}"
                    transaction-size="${rabbitmq.listener-container.transaction-size}" >

    <listener id="v1.listener.queue.1" ref="listener1" method="handleMessage" queues="v1.queue.1" />
    <listener id="v1.listener.queue.2" ref="listener2" method="handleMessage" queues="v1.queue.2" />
    <listener id="v1.listener.queue.3" ref="listener3" method="handleMessage" queues="v1.queue.3" />

</listener-container>

<bean id="amqpServerConnection" class="com.sub1.sub2.RabbitGatewayConnectionImpl">
    <property name="rabbitTemplate" ref="rabbitTemplate" />
</bean>

当我在新的基于Spring 4的应用程序中配置websocket时,我不知道如何/在哪里声明交换,队列等…

registry.enableStompBrokerRelay("/example1/", "/example2/")
            .setApplicationLogin("guest")
            .setApplicationPasscode("guest")
            .setAutoStartup(true)
            .setRelayHost("localhost")
            .setRelayPort(5672)
            .setSystemHeartbeatReceiveInterval(10000)
            .setSystemHeartbeatSendInterval(10000);

这个东西必须用AMPQ实现吗?

最佳答案
基本上,客户端建立WebSocket会话并使用STOMP进行消息传递(STOMP over WebSocket),而不是AMQP.在STOMP中,一切都由目标头驱动,由消息代理决定它的含义.例如,检查RabbitMQ STOMP plugin页面以查看Rabbit如何将STOMP目标映射到队列和交换.

因此,如果您将RabbitMQ视为您已在其中管理队列,交换等的消息代理,则在Web应用程序端,消息传递协议是STOMP,客户端可以将消息发送到映射到@MessageMapping控制器方法的STOMP目标或由支持的目标. RabbitMQ的.此外,控制器或注入SimpMessagingTemplate的任何其他组件可以向代理(Rabbit)发送消息,然后代理将广播到连接的Web客户端.

希望这可以帮助.

转载注明原文:spring-4 – 使用Spring 4 Websockets声明并使用RabbitMQ - 代码日志