简体   繁体   中英

cannot instantiate a WebSocket in my grizzly-project under use of createSocket-Method

I am trying to enable WebSockets in a Grizzly based server and make a server-push but I am getting this error:

WARNING: GRIZZLY0013: Exception during FilterChain execution
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletResponse
.
.
.
Caused by: java.lang.ClassNotFoundException:   javax.servlet.http.HttpServletResponse

In my Code, I am not using Servlets, So I do not know, why I am getting this error. Here is my whole code, which is based on this example https://jaxenter.com/tutorial-pushing-browser-updates-using-websockets-in-glassfish-105970.html

1) The Agent-Class that starts my grizzly-server. Here I registred my EchoApp without using Servlets.

public class Agent{



public void runServer() throws UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, KeyStoreException,
                                        IOException, InterruptedException {

    HttpServer server = HttpServer.createSimpleServer();
    final WebSocketAddOn webSocketAddon = new WebSocketAddOn();
    for (NetworkListener listener : server.getListeners()) {
        listener.setChunkingEnabled(false);
        listener.registerAddOn(webSocketAddon);
    }

    EchoApp echoApplication = new EchoApp();
    WebSocketEngine.getEngine().register(echoApplication);

    // start the server
    server.start();
    Thread.currentThread().join();

}
public static void main(String args []){
    Agent agent = new Agent();
    try {
        agent.runServer();
    } catch (UnrecoverableKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

2) Here is my EchoApplication which extends WebSocketApplication:

public class EchoApp extends WebSocketApplication{


/** The logger. */

private static final Logger logger = Logger.getLogger(EchoApp.class.getName());

@Override
public boolean isApplicationRequest(HttpRequestPacket request) {

        if (request.getRequestURI().toString().endsWith("/echo"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

@Override
   public WebSocket createSocket(ProtocolHandlerhandler,HttpRequestPacket requestPacket,
           WebSocketListener... listeners) {
        System.out.println("Websocket will be Created");
        return new EchoSocket(handler, requestPacket,listeners);//The exception will be thrown here!!!!!!!!!

    }
}

3) Here is my EchoSocket-class:

 class EchoSocket extends DefaultWebSocket implements Runnable {

private Thread myThread;
private boolean connected = false;

//handler
EchoSocket (ProtocolHandler handler,HttpRequestPacket requestPacket, WebSocketListener... listeners){
    super(handler,requestPacket,listeners);
    System.out.println("instantiate");
}

@Override    
public void onConnect() {
    System.out.println("connected");
    myThread = new Thread(this);  
    connected = true;
    myThread.start(); 
    super.onConnect();  
}

public void run() {
    if(connected) {
        System.out.println("connected");
        send("push");
    }   
}
}

Here is the dependencies that I need for building my project:

<dependency>
         <groupId>org.glassfish.grizzly</groupId>
         <artifactId>grizzly-websockets</artifactId>
         <version>2.3.22</version>
</dependency>
<dependency>
         <groupId>org.glassfish.grizzly</groupId>
         <artifactId>grizzly-http-server</artifactId>
         <version>2.3.22</version>
</dependency>
<dependency> //I added this dependency when I get the problem but it does not solve it
         <groupId>org.glassfish.grizzly</groupId>
         <artifactId>grizzly-http-servlet</artifactId>
         <version>2.3.22</version>
</dependency>

I never used Servlets and I do not know if I need them in my Code?

If I need Servlets: Where can I use them in my Code

Grizzly has only one module for websocket support, that includes both the client- and the server-side API. The server-side API in its turn has a dependency on servlet-api to be present on the classpath. So please include this maven dependency:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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