猿问

Jetty 服务器上的 STOMP sockJS 和 ActiveMQ

我正在尝试在框架中的 Jetty 服务器上使用 STOMP 与 SockJS 和 ActiveMQ karaf osgi。我能够在嵌入式 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>



DIEA
浏览 156回答 2
2回答

慕容3067478

异常表明问题是什么:Caused&nbsp;by:&nbsp;java.lang.ClassNotFoundException:&nbsp;javax.websocket.ClientEndpointConfig$Configurator &nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.net.URLClassLoader.findClass(Unknown&nbsp;Source) &nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.lang.ClassLoader.loadClass(Unknown&nbsp;Source) &nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;sun.misc.Launcher$AppClassLoader.loadClass(Unknown&nbsp;Source) &nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.lang.ClassLoader.loadClass(Unknown&nbsp;Source) &nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;2&nbsp;morejavax.websocket.ClientEndpointConfig$Configurator找不到该类。请确保正确的类加载器可以访问所有必要的类来运行您的应用程序。

守着一只汪

问题是在连接时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 {&nbsp; &nbsp; List<Transport> transports = new ArrayList<Transport>();&nbsp; &nbsp; //transports.add(new WebSocketTransport(new StandardWebSocketClient()));&nbsp;&nbsp; &nbsp; //transports.add(new WebSocketTransport(new JettyWebSocketClient()));&nbsp; &nbsp; transports.add(new RestTemplateXhrTransport());&nbsp; &nbsp; SockJsClient sockJsClient = new SockJsClient(transports);&nbsp; &nbsp; WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);&nbsp; &nbsp; stompClient.setMessageConverter(new MappingJackson2MessageConverter());&nbsp; &nbsp; //stompClient.setMessageConverter(new Mapp);&nbsp; &nbsp; StompSession session = stompClient.connect(socketURL,sessionHandler).get(); //error occurs here&nbsp; &nbsp; System.out.println("Session : " + session);&nbsp; &nbsp; session.subscribe("/queue/msg",&nbsp; sessionHandler);&nbsp; &nbsp; return session;}
随时随地看视频慕课网APP

相关分类

Java
我要回答