2013-04-10 22 views
0

我試圖建立這樣一個項目,我有: - JBoss作爲主容器網絡 - 項目叫PWS與碼頭,支持web套接字在JBoss中 跑了 - 使用Web Socket連接以使用Atmosphere框架。沒有大氣處理程序發現 - 碼頭的WebSocket的Jboss

我programmtic Jetty服務器配置:

JettyIntegration.java

public final class JettyIntegration implements ServletContextListener { 

    private Server server; 

    public void contextInitialized(ServletContextEvent sce) { 
     String jbossWebHost; 


     MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); 
     try { 
      jbossWebHost = "localhost"; 
      // ObjectName http = 
      // new ObjectName("jboss.as:socket-binding-group=standard" + 
      // "-sockets,socket-binding=http"); 
      // jbossWebHost = 
      // (String) mBeanServer.getAttribute(http, "boundAddress"); 
      // jbossWebPort = (Integer) mBeanServer.getAttribute(http, 
      // "boundPort"); 

     } catch (Exception e) { 
      throw new Error(e); 
     } 

     server = new Server(); 

     SelectChannelConnector connector = new SelectChannelConnector(); 
     connector.setHost(jbossWebHost); 
     connector.setPort(8181); 
     server.addConnector(connector); 

     ContextHandlerCollection contexts = new ContextHandlerCollection(); 
     contexts.setHandlers(new Handler[] { createJerseyRestServletHandler() }); 

     server.setHandler(contexts); 

     try { 
      server.start(); 
     } catch (Exception e) { 
      sce.getServletContext().log("An error occurred while starting Jetty", e); 
     } 
    } 

    public void contextDestroyed(ServletContextEvent sce) { 
     if (server != null) { 
      try { 
       server.stop(); 
      } catch (Exception e) { 
       sce.getServletContext().log("An error occurred while shutting down Jetty", e); 
      } 
     } 
    } 

    private ServletContextHandler createJerseyRestServletHandler() { 
     MeteorServlet servlet = new MeteorServlet(); 
     ServletHolder atmosphereServletHolder = new ServletHolder(servlet); 

     // atmosphere 
     atmosphereServletHolder.setInitParameter("org.atmosphere.useWebSocket", "true"); 
     atmosphereServletHolder.setInitParameter("WebSocketProtocol", "PWSHandler"); 
     atmosphereServletHolder.setInitParameter("org.atmosphere.cpr.atmosphereHandlerPath", "/WEB-INF/classes/com/sis/sante/centaure/pws/web"); 

     atmosphereServletHolder.setAsyncSupported(true); 

     FilterHolder filterHolder = new FilterHolder(CrossOriginFilter.class); 
     filterHolder.setInitParameter("allowedOrigins", "*"); 
     filterHolder.setInitParameter("allowedMethods", "GET, POST"); 

     ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); 
     servletHandler.addServlet(atmosphereServletHolder, "/pws/*"); 
     servletHandler.addFilter(filterHolder, "/*", null); 
     servletHandler.setInitParameter("org.atmosphere.cpr.atmosphereHandlerPath", "/WEB-INF/classes/com/sis/sante/centaure/pws/web"); 

     return servletHandler; 
    } 


} 

PWSHandler.java

@WebSocketHandlerService(path = "/pws", broadcaster = SimpleBroadcaster.class) 
public class PWSHandler extends WebSocketHandlerAdapter { 

    private final ObjectMapper mapper = new ObjectMapper(); 

    @Override 
    public void onOpen(WebSocket webSocket) { 
     System.out.println("on Open"); 
     webSocket.resource().setBroadcaster(BroadcasterFactory.getDefault().lookup("/pws", true)); 
    } 

    @Override 
    public void onTextMessage(WebSocket webSocket, String data) { 
     System.out.println("on Message"); 
     AtmosphereResource r = webSocket.resource(); 
     Broadcaster b = r.getBroadcaster(); 
     b.broadcast("test message" + data); 
     //b.broadcast(mapper.writeValueAsString(mapper.readValue(message, Data.class))); 
    } 

} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app ...> 
    <listener> 
     <listener-class>com.sis.sante.centaure.pws.web.JettyIntegration</listener-class> 
    </listener> 
</web-app> 

當J部署下老大,我可以連接到Web Socket作爲大氣客戶端響應: 使用URL:ws:// localhost:8181/pws?X-Atmosphere-tracking-id = 0 & X-Atmosphere-Framework = 1.0 & X-Atmosphere-交通運輸= WebSocket的& X-緩存日期= 0 &內容類型=應用程序/ JSON jquery.atmosphere.js:2301 的WebSocket成功打開

但是服務器扔我的日誌:

16:55:29,618 INFO [org.eclipse.jetty.server.Server] (MSC service thread 1-5) jetty-8.1.2.v20120308 
16:55:29,652 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Atmosphere is using org.atmosphere.cpr.DefaultAnnotationProcessor for processing annotation 
16:55:29,655 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Auto detecting atmosphere handlers /WEB-INF/classes/com/sis/sante/centaure/pws/web 
    16:55:29,656 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Auto detecting WebSocketHandler in /WEB-INF/classes/com/sis/sante/centaure/pws/web 
    16:55:29,656 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Installed WebSocketProtocol org.atmosphere.websocket.protocol.SimpleHttpProtocol 
    16:55:29,667 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Atmosphere is using async support: org.atmosphere.container.JettyAsyncSupportWithWebSocket running under container: jetty/8.1.2.v20120308 with WebSocket enabled. 
    16:55:29,668 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Installing Default AtmosphereInterceptor 
    16:55:29,669 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) org.atmosphere.interceptor.JavaScriptProtocol : Atmosphere JavaScript Protocol 
    16:55:29,670 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) org.atmosphere.interceptor.JSONPAtmosphereInterceptor : JSONP Interceptor Support 
    16:55:29,671 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) org.atmosphere.interceptor.SSEAtmosphereInterceptor : SSE Interceptor Support 
    16:55:29,672 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) org.atmosphere.interceptor.AndroidAtmosphereInterceptor : Android Interceptor Support 
    16:55:29,673 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) org.atmosphere.interceptor.StreamingAtmosphereInterceptor : Streaming Interceptor Support 
    16:55:29,675 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) org.atmosphere.interceptor.DefaultHeadersInterceptor : Default Response's Headers Interceptor 
    16:55:29,675 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Installed Default AtmosphereInterceptor [Default Response's Headers Interceptor, Streaming Interceptor Support, Android Interceptor Support, SSE Interceptor Support, JSONP Interceptor Support, Atmosphere JavaScript Protocol]. Set org.atmosphere.cpr.AtmosphereInterceptor.disableDefaults in your xml to disable them. 
    16:55:29,678 WARN [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) No BroadcasterCache configured. Broadcasted message between client reconnection will be LOST. It is recommended to configure the org.atmosphere.cache.EventCacheBroadcasterCache 
    16:55:29,679 WARN [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Neither TrackMessageSizeInterceptor or TrackMessageSizeFilter are installed. atmosphere.js may receive glued and incomplete message. 
    16:55:29,679 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) HttpSession supported: false 
    16:55:29,680 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Using BroadcasterFactory: org.atmosphere.cpr.DefaultBroadcasterFactory 
    16:55:29,680 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Using WebSocketProcessor: org.atmosphere.websocket.DefaultWebSocketProcessor 
    16:55:29,681 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Using Broadcaster: org.atmosphere.cpr.DefaultBroadcaster 
    16:55:29,682 INFO [org.atmosphere.cpr.AtmosphereFramework] (MSC service thread 1-5) Atmosphere Framework 1.1.0.beta3 started. 
    16:55:29,705 INFO [org.eclipse.jetty.server.AbstractConnector] (MSC service thread 1-5) Started [email protected]:8181 
    16:55:29,715 INFO [org.jboss.web] (MSC service thread 1-5) JBAS018210: Registering web context: /pws 
    16:55:29,798 INFO [org.jboss.as.server] (HttpManagementService-threads - 17) JBAS018559: Deployed "pws.war" 
    16:55:30,114 ERROR [org.atmosphere.cpr.AsynchronousProcessor] (qtp292954387-257) No AtmosphereHandler found. Make sure you define it inside WEB-INF/atmosphere.xml or annotate using @AtmosphereHandlerService 
    16:55:30,115 WARN [org.atmosphere.websocket.DefaultWebSocketProcessor] (qtp292954387-257) Failed invoking AtmosphereFramework.doCometSupport(): org.atmosphere.cpr.AtmosphereMappingException: No AtmosphereHandler found. Make sure you define it inside WEB-INF/atmosphere.xml or annotate using @AtmosphereHandlerService 
     at org.atmosphere.cpr.AsynchronousProcessor.action(AsynchronousProcessor.java:188) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.cpr.AsynchronousProcessor.suspended(AsynchronousProcessor.java:165) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.container.JettyWebSocketUtil.doService(JettyWebSocketUtil.java:78) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.container.JettyAsyncSupportWithWebSocket.service(JettyAsyncSupportWithWebSocket.java:69) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.cpr.AtmosphereFramework.doCometSupport(AtmosphereFramework.java:1403) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:360) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.websocket.DefaultWebSocketProcessor.open(DefaultWebSocketProcessor.java:145) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.container.JettyWebSocketHandler.onOpen(JettyWebSocketHandler.java:106) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.eclipse.jetty.websocket.WebSocketConnectionRFC6455.onWebSocketOpen(WebSocketConnectionRFC6455.java:435) [jetty-websocket-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.websocket.WebSocketServletConnectionRFC6455.handshake(WebSocketServletConnectionRFC6455.java:57) [jetty-websocket-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:288) [jetty-websocket-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:361) [jetty-websocket-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.atmosphere.container.JettyWebSocketUtil.doService(JettyWebSocketUtil.java:65) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.container.JettyAsyncSupportWithWebSocket.service(JettyAsyncSupportWithWebSocket.java:69) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.cpr.AtmosphereFramework.doCometSupport(AtmosphereFramework.java:1403) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.cpr.AtmosphereServlet.doPost(AtmosphereServlet.java:294) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at org.atmosphere.cpr.AtmosphereServlet.doGet(AtmosphereServlet.java:280) [atmosphere-runtime-1.1.0.beta3.jar:1.1.0.beta3] 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final] 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final] 
     at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:594) [jetty-servlet-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1366) [jetty-servlet-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.servlets.CrossOriginFilter.handle(CrossOriginFilter.java:212) [jetty-servlets-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.servlets.CrossOriginFilter.doFilter(CrossOriginFilter.java:179) [jetty-servlets-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1337) [jetty-servlet-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:484) [jetty-servlet-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:413) [jetty-servlet-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.Server.handle(Server.java:351) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634) [jetty-http-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230) [jetty-http-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77) [jetty-server-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609) [jetty-io-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:45) [jetty-io-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599) [jetty-util-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534) [jetty-util-8.1.2.v20120308.jar:8.1.2.v20120308] 
     at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_07] 

    16:55:30,130 WARN [org.atmosphere.websocket.protocol.SimpleHttpProtocol] (qtp292954387-257) org.atmosphere.cpr.AtmosphereMappingException: No AtmosphereHandler found. Make sure you define it inside WEB-INF/atmosphere.xml or annotate using @AtmosphereHandlerService Status 500 Message Server Error 

什麼可能會出錯?

+0

你爲什麼不直接在jboss中使用web套接字?你需要碼頭什麼? – eis 2013-04-10 15:13:04

+0

只是一個說明,你不能使用jboss的jetty websockets。不起作用。因爲websocket實現需要訪問碼頭內部連接管理才能正常工作。 – 2013-04-10 15:47:41

+0

或只是使用普通老碼頭:) – 2013-04-10 22:22:52

回答

1

而不是嘗試使用Jetty,安裝websockets到JBoss AS 7.2/JBoss EAP 6.1.Alpha,你應該沒問題。

這裏是instructions,但根據我的經驗,他們有點過時(過時)。你真正需要的是1 APR啓用,所以切換

<subsystem xmlns="urn:jboss:domain:web:1.1" 
     default-virtual-server="default-host" native="true"> 

到:

<subsystem xmlns="urn:jboss:domain:web:1.1" 
     default-virtual-server="default-host" native="true"> 

在你的web服務器配置(domain.xml中或standalone.xml),和2 使用maven依賴

<dependency> 
    <groupId>org.atmosphere.jboss.as</groupId> 
    <artifactId>jboss-as-websockets</artifactId> 
    <version>0.4</version> 
</dependency> 

就是這樣,那麼你應該很好去。只需在應用程序中擴展org.atmosphere.jboss.as.websockets.servlet.WebSocketServlet。

編輯:對於感興趣的,我有一個簡單的示例應用源代碼in github

+0

你有鏈接到一個項目的地方嗎?網頁。xml,java實現,pom.xml? – 2013-04-11 12:27:59

+0

@JulienBRENELIERE我只是做了一個實現,但不幸的是我把它提交給了公司內部編程競賽,所以許可證不允許我提供鏈接:)但這並不是很難做到。它似乎在這[其他線程](http://stackoverflow.com/questions/13812087/is-there-an-example-for-utilization-of-websockets-in-jboss-as7)它也被要求所以我猜我應該做它的另一個版本 – eis 2013-04-11 12:50:00

+0

也許你可以PM項目的例子,而不是一個鏈接:-) – 2013-04-11 12:52:15

相關問題