无法在使用 createSocket 方法的情况下在我的灰熊项目中实例化 WebSocket
cannot instantiate a WebSocket in my grizzly-project under use of createSocket-Method
我正在尝试在基于 Grizzly 的服务器中启用 WebSockets 并进行服务器推送,但出现此错误:
WARNING: GRIZZLY0013: Exception during FilterChain execution
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletResponse
.
.
.
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletResponse
在我的代码中,我没有使用 Servlet,所以我不知道为什么会出现此错误。这是我的全部代码,它基于这个例子 https://jaxenter.com/tutorial-pushing-browser-updates-using-websockets-in-glassfish-105970.html
1) 启动我的 grizzly-server 的 Agent-Class。在这里,我在不使用 Servlet 的情况下注册了我的 EchoApp。
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) 这是我扩展 WebSocketApplication 的 EchoApplication:
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) 这是我的 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");
}
}
}
这是构建项目所需的依赖项:
<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>
我从未使用过 Servlet,我不知道我的代码中是否需要它们?
如果我需要 Servlet:我可以在我的代码中的什么地方使用它们
Grizzly 只有一个模块支持 websocket,包括客户端和服务器端 API。服务器端 API 又依赖于 servlet-api 出现在类路径中。所以请包括这个 Maven 依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
我正在尝试在基于 Grizzly 的服务器中启用 WebSockets 并进行服务器推送,但出现此错误:
WARNING: GRIZZLY0013: Exception during FilterChain execution
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletResponse
.
.
.
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletResponse
在我的代码中,我没有使用 Servlet,所以我不知道为什么会出现此错误。这是我的全部代码,它基于这个例子 https://jaxenter.com/tutorial-pushing-browser-updates-using-websockets-in-glassfish-105970.html
1) 启动我的 grizzly-server 的 Agent-Class。在这里,我在不使用 Servlet 的情况下注册了我的 EchoApp。
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) 这是我扩展 WebSocketApplication 的 EchoApplication:
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) 这是我的 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");
}
}
}
这是构建项目所需的依赖项:
<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>
我从未使用过 Servlet,我不知道我的代码中是否需要它们?
如果我需要 Servlet:我可以在我的代码中的什么地方使用它们
Grizzly 只有一个模块支持 websocket,包括客户端和服务器端 API。服务器端 API 又依赖于 servlet-api 出现在类路径中。所以请包括这个 Maven 依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>