简体   繁体   English

在jetty中部署webapps和websockets

[英]Deploy webapps and websockets in jetty

I have embedded Jetty into my application. 我已将Jetty嵌入到我的应用程序中。 I have one webapp and I successfully deployed that webapp in the Jetty. 我有一个webapp,我在Jetty中成功部署了该webapp。 Now I got another requirement on Websockets. 现在我对Websockets有了另一个要求。 I followed this link for deploying websockets in Jetty. 我按照此链接在Jetty中部署websockets。 After deploying the Websockets into my server the first webapp is not working (the home page is not opening). 将Websockets部署到我的服务器后,第一个webapp无法正常工作(主页未打开)。 Here is the image 这是图像

在此输入图像描述

Here is the code (Its very hard to paste the whole code here. So I am pasting the webapp and websocket deployment code). 这是代码(这里很难粘贴整个代码。所以我粘贴了webapp和websocket部署代码)。

Webapp Deployment: Webapp部署:

List<Handler> handlersList = new ArrayList<Handler>();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setResourceBase(webApp.appDir);
webAppContext.setDescriptor(webApp.appDir + "/WEB-INF/web.xml");
webAppContext.setContextPath(webApp.contextPath);
webAppContext.setParentLoaderPriority(true);
// webAppContext.setWar(webApp.appDir);
webAppContext.setVirtualHosts(webApp.hostName);
handlersList.add(webAppContext);
argNewServer.setHandler(handlersList);

WebSocket Deployment: WebSocket部署:

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/echo");
argServer.setHandler(context);

// Initialize javax.websocket layer
ServerContainer wscontainer;
try {
    wscontainer = WebSocketServerContainerInitializer.configureContext(context);
    // Add WebSocket endpoint to javax.websocket layer
    wscontainer.addEndpoint(WebSocketServer.class);
} catch (ServletException | DeploymentException e) {
    throw new RuntimeException("Exception while adding websocket endpoint: ", e);
}

WebSocketServer.java: WebSocketServer.java:

package com.tdg.chat;

import java.io.IOException;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/events/")
public class WebSocketServer {
    @OnOpen
    public void onWebSocketConnect(Session sess) {
        System.out.println("Socket Connected: " + sess);
    }

    @OnMessage
    public void onWebSocketText(Session argSession, String argMessage) throws IOException {
        System.out.println("Received TEXT message: " + argMessage);
        argSession.getBasicRemote().sendText("From Server: ");
    }

    @OnClose
    public void onWebSocketClose(CloseReason reason) {
        System.out.println("Socket Closed: " + reason);
    }

    @OnError
    public void onWebSocketError(Throwable cause) {
        cause.printStackTrace(System.err);
    }
}

If I include the aforementioned websocket code the actual webapp is not working, but the websocket code is working. 如果我包含上述websocket代码,实际的webapp不起作用,但websocket代码正在工作。

So how to deploy a webapp and websocket in one Jetty server? 那么如何在一个Jetty服务器中部署webapp和websocket呢?

Updated: 更新:

I tried to include the Websockets in the current webapp itself but I am getting the following exception. 我试图将Websockets包含在当前的webapp本身中,但我得到以下异常。

java.lang.NullPointerException: null at org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.configureContext(WebSocketServerContainerInitializer.java:65) ~[javax-websocket-server-impl-9.3.6.v20151106.jar:9.3.6.v20151106] at com.tdg.daemon.server.RiseServer.getWebappHandlers(RiseServer.java:122) ~[classes/:na] at com.tdg.daemon.server.RiseServer.addWebApps(RiseServer.java:77) ~[classes/:na] at com.tdg.daemon.server.RiseServer.init(RiseServer.java:59) ~[classes/:na] at com.tdg.daemon.server.RiseServerConfiguration.build(RiseServerConfiguration.java:475) ~[classes/:na] at com.tdg.daemon.server.launcher.DefaultTdgServerLauncher.start(DefaultTdgServerLauncher.java:78) [classes/:na] at com.tdg.daemon.server.launcher.DefaultTdgServerLauncher.main(DefaultTdgServerLauncher.java:140) [classes/:na] java.lang.NullPointerException at org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.configureContext(WebSocketServerContainerInitiali java.lang.NullPointerException:null org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.configureContext(WebSocketServerContainerInitializer.java:65)〜[javax-websocket-server-impl-9.3.6.v20151106.jar: 9.3.6.v20151106] com.tdg.daemon.server.RiseServer.getWebappHandlers(RiseServer.java:122)〜[classes /:na] at com.tdg.daemon.server.RiseServer.addWebApps(RiseServer.java:77 )〜[classes /:na] at com.tdg.daemon.server.RiseServer.init(RiseServer.java:59)〜[classes /:na] at com.tdg.daemon.server.RiseServerConfiguration.build(RiseServerConfiguration.java) :475)〜[classes /:na] at com.tdg.daemon.server.launcher.DefaultTdgServerLauncher.start(DefaultTdgServerLauncher.java:78)[classes /:na] at com.tdg.daemon.server.launcher.DefaultTdgServerLauncher。 main(DefaultTdgServerLauncher.java:140)[classes /:na] java.lang.NullPointerException org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.configureContext(WebSocketServerContainerInitiali) zer.java:65) at com.tdg.daemon.server.RiseServer.getWebappHandlers(RiseServer.java:122) at com.tdg.daemon.server.RiseServer.addWebApps(RiseServer.java:77) at com.tdg.daemon.server.RiseServer.init(RiseServer.java:59) at com.tdg.daemon.server.RiseServerConfiguration.build(RiseServerConfiguration.java:475) at com.tdg.daemon.server.launcher.DefaultTdgServerLauncher.start(DefaultTdgServerLauncher.java:78) at com.tdg.daemon.server.launcher.DefaultTdgServerLauncher.main(DefaultTdgServerLauncher.java:140) [] 12/02/2015 17:50:06.985 [main - ] INFO ctdslDefaultTdgServerLauncher - RiseServer started on port 80 and 443. zer.java:65)com.tdg.daemon.server.RiseServer.getWebappHandlers(RiseServer.java:122)at com.tdg.daemon.server.RiseServer.addWebApps(RiseServer.java:77)at com.tdg.daemon位于com.tdg.daemon.server.RiseServerConfiguration.build(RiseServerConfiguration.java:475)的.server.RiseServer.init(RiseServer.java:59)位于com.tdg.daemon.server.launcher.DefaultTdgServerLauncher.start(DefaultTdgServerLauncher.java) :78)at com.tdg.daemon.server.launcher.DefaultTdgServerLauncher.main(DefaultTdgServerLauncher.java:140)[] 12/02/2015 17:50:06.985 [main - ] INFO ctdslDefaultTdgServerLauncher - RiseServer在端口80和443上启动。

The WebSocketServerContainerInitializer.configureContext() requires knowledge about the Server that it will be run under. WebSocketServerContainerInitializer.configureContext()需要有关将在其下运行的Server知识。

There's 2 ways to accomplish this, choose either approach before you call WebSocketServerContainerInitializer.configureContext() 有两种方法可以实现这一点,在调用WebSocketServerContainerInitializer.configureContext()之前选择任一种方法

  1. Add the ServletContextHandler to the Server instance via its Server.setHandler(Handler) call before you attempt to configure the context. 在尝试配置上下文之前,通过Server.setHandler(Handler)调用将ServletContextHandler添加到Server实例。 (the ServletContextHandler can be part of a larger Handler tree and this will work). ServletContextHandler可以是更大的Handler树的一部分,这将起作用)。 The mere act of calling Server.setHandler(Handler) will populate the Server reference for all of the Handler instances you have declared. 仅调用Server.setHandler(Handler)将填充您声明的所有Handler实例的Server引用。 (This is the recommended approach) (这是推荐的方法)
  2. Manually call ServletContextHandler.setServer(server) - but be careful to not swap out/change the Server instance you use later in Server.setHandler(Handler) call. 手动调用ServletContextHandler.setServer(server) - 但要小心不要换出/更改稍后在Server.setHandler(Handler)调用中使用的Server实例。 Also, once you do this, you cannot change things in the Server, such as the Thread Pool, Executors, Buffer Pools, Schedulers, etc. (This is an undesirable, but functional, approach) 此外,一旦执行此操作,您就无法更改服务器中的内容,例如线程池,执行程序,缓冲池,调度程序等(这是一种不受欢迎但功能正常的方法)

@Joakim Erdfelt, Its my mistake and I agree with your second point. @Joakim Erdfelt,我的错误,我同意你的第二点。 What I did is, First I added my webapp handler to the server and then I added websocket handler to the server (Based on aforementioned example). 我做的是,首先我将我的webapp处理程序添加到服务器,然后我将websocket处理程序添加到服务器(基于上述示例)。

argNewServer.setHandler(webAppContext);
argNewServer.setHandler(context);

As you mentioned in the second point once if we set the handlers to the server we can't modify the things in the server. 正如你在第二点中提到的那样,如果我们将处理程序设置为服务器,我们就无法修改服务器中的内容。 That's true, I modified my code like I added all my handlers in single shot. 这是真的,我修改了我的代码,就像我一次性添加了所有处理程序一样。 Then my webapp starts working and my websocket is also working. 然后我的webapp开始工作,我的websocket也正常工作。 Here is the modified code 这是修改后的代码

List<Handler> handlersList = new ArrayList<Handler>();
handlersList.add(webAppContext);
handlersList.add(context);
argNewServer.setHandler(handlersList);

Thank you you very much @Joakim Erdfelt. 非常感谢@Joakim Erdfelt。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM