Jetty 服务器上的 STOMP sockJS 和 ActiveMQ

STOMP sockJS and ActiveMQ over Jetty server

我正在尝试在 karaf osgi 框架中通过 Jetty 服务器将 STOMP 与 SockJS 和 ActiveMQ 结合使用。我能够在嵌入式 Tomcat 服务器上 运行 WebSocket,但在与 Jetty 服务器一起使用时出现问题。我的 karaf 在内部使用 Jetty 服务器。

 transports.add(new WebSocketTransport(new StandardWebSocketClient())); // Got error for this in jetty

所以把StandardWebSocketClient改成了JettyWebSocketClient

public static void main( String[] args ) throws Exception {
    StompSession session = createStompWSConnection(WEBSOCKET_URI, new MyStompSessionHandler());
    System.out.println("Session = " + session.getSessionId());
}  

public static StompSession createStompWSConnection(String socketURL,StompSessionHandler sessionHandler) throws Exception {
    List<Transport> transports = new ArrayList<Transport>();

    //transports.add(new WebSocketTransport(new StandardWebSocketClient())); 
    transports.add(new WebSocketTransport(new JettyWebSocketClient()));

    SockJsClient sockJsClient = new SockJsClient(transports);
    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    //stompClient.setMessageConverter(new Mapp);

    StompSession session = stompClient.connect(socketURL,sessionHandler).get(); //error occurs here
    System.out.println("Session : " + session);
    session.subscribe("/queue/msg",  sessionHandler);
    return session;
}

我收到这个错误:

Caused by: java.lang.ClassNotFoundException: javax.websocket.ClientEndpointConfig$Configurator
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

编辑 2 -

添加了 Jetty Websocket 客户端依赖项,现在我收到此错误 -

<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.3.2.v20150730</version>
</dependency>

异常 -

Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.IllegalStateException: WebSocketClient@2092066706 is not started
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.springframework.util.concurrent.SettableListenableFuture.get(SettableListenableFuture.java:122)
    at com.spring.jms.sockJS.App.createStompWSConnection(App.java:44)
    at com.spring.jms.sockJS.App.main(App.java:27)

异常表明问题所在:

Caused by: java.lang.ClassNotFoundException: javax.websocket.ClientEndpointConfig$Configurator
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

找不到classjavax.websocket.ClientEndpointConfig$Configurator。请确保正确的 classloader 可以访问 运行 您的应用程序所需的所有 classes。

连接到 stompClient.connect(socketURL,sessionHandler).get(); 时出现问题,我还在实际发生错误的地方添加了注释。我通过将 transports.add(new WebSocketTransport(new JettyWebSocketClient())); 替换为 transports.add(new RestTemplateXhrTransport());

解决了这个问题

更改如下:

public static StompSession createStompWSConnection(String socketURL,StompSessionHandler sessionHandler) throws Exception {
    List<Transport> transports = new ArrayList<Transport>();

    //transports.add(new WebSocketTransport(new StandardWebSocketClient())); 
    //transports.add(new WebSocketTransport(new JettyWebSocketClient()));
    transports.add(new RestTemplateXhrTransport());
    SockJsClient sockJsClient = new SockJsClient(transports);
    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    //stompClient.setMessageConverter(new Mapp);

    StompSession session = stompClient.connect(socketURL,sessionHandler).get(); //error occurs here
    System.out.println("Session : " + session);
    session.subscribe("/queue/msg",  sessionHandler);
    return session;
}