使用Spring 4 WebSocket的Stomp sockjs无法连接到ActiveMQ

时间:2016-01-10 07:53:19

标签: spring websocket activemq stomp sockjs

我无法管理通过WebSocket侦听STOMP消息的服务器。

我使用sockjs + stomp js + spring 4(.0.5~ .1.2~2.2)+ activemq 5.13 + jdk 1.7.0_17 + tomcat 7.0.67。

客户抛出"经纪人不可用"错误!

客户js:

var ws = new SockJS(basePath + "/websocket");
var client = Stomp.over(ws);
client.heartbeat.outgoing=10000;
client.heartbeat.incoming=10000;
client.connect({},function(frame){
    setConnect(true);
    console.log('connectd:' + frame);
    client.subscribe('/topic/ggg',function(data){
        alert(JSON.parse(data.body).content);
    });
},function(error){
    console.log('stomp:' + error);
    setTimeout(connect,10000);
    console.log('stomp: Reconnecting in 10 seconds');
});

my pom.xml:spring - * - version(4.2.2)activemq-core-version(5.7.0)

    <!-- JMS begin -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-core</artifactId>
        <version>${activemq.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.activemq.protobuf</groupId>
                <artifactId>activemq-protobuf</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.osgi</groupId>
                <artifactId>spring-osgi-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.fusesource.mqtt-client</groupId>
                <artifactId>mqtt-client</artifactId>
            </exclusion>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- JMS end -->
    <!-- websocket -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>
        <version>2.0.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-net</artifactId>
        <version>2.0.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor.spring</groupId>
        <artifactId>reactor-spring-context</artifactId>
        <version>2.0.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.33.Final</version>
    </dependency>

config.java

@Configuration
@EnableWebSocketMessageBroker 
@DependsOn({"applicationProperties"})
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> arg0) {

}

@Override
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler>     arg0) {

}

@Override
public void configureClientInboundChannel(ChannelRegistration registration)  {
     registration.taskExecutor().corePoolSize(4) 
                    .maxPoolSize(8)
                    .keepAliveSeconds(60);
}

@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
     registration.taskExecutor().corePoolSize(4).maxPoolSize(8);
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
     registry.enableStompBrokerRelay("/topic")
.setRelayHost(ApplicationProperties.getProproperty("jms.websocket.ip"))
.setRelayPort(Integer.valueOf(ApplicationProperties.getProproperty("jms.websocket.port"))) 
             .setSystemHeartbeatReceiveInterval(2000) 
             .setSystemHeartbeatSendInterval(2000); 
     registry.setApplicationDestinationPrefixes("/ws");
}

@Override
public boolean configureMessageConverters(List<MessageConverter> arg0) {
    return true;
}

@Override
public void configureWebSocketTransport(WebSocketTransportRegistration   registry) {
    registry.setMessageSizeLimit(8192)
            .setSendBufferSizeLimit(8192)
            .setSendTimeLimit(10000);
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
     registry.addEndpoint("/websocket")
            .withSockJS();
}
}

application.properties

jms.broker_url=tcp://127.0.0.1:61616
jms.websocket.ip=stomp://127.0.0.1
jms.websocket.port=61613

配置此代码,然后我运行activemq,运行tomcat,打开chrome for url

控制台日志:

Opening web socket...
web socket opened...
>>>CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000

<<<ERROR
message:Broker not avalibale.
content-length:0

stomp:ERROR
content-length:0
message:Broker not available.

stomp:Reconnecting in 10 seconds
Whoops! lost connection to http://localhost:8080/example/websocket

activemq conf:

<transportConnectors>
        <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
        <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    </transportConnectors>

当我使用spring JmsTemplate时,sendTopic()没问题,收到了MQ!

我追踪了代码StompBrokerRelayMessageHandler&amp; AbstractBrokerMessageHandler发现isBrokerAvailable()方法返回false!但是mq正在运行!

如何设置brokerAvailable为真?

Reactor2TcpClient不支持ActiveMQ吗?为什么我必须设置传输协议stomp://如果没有抛出错误....

请指出这段代码的问题?如何使用AMQ通过WebSocket / Stomp以编程方式配置主题?

部份!

0 个答案:

没有答案