简体   繁体   中英

Secure WebSocket/Jetty server (not embedded)/HTTPS: Service unavailable (503)

I have a very simple WebSocket server from Danny Cowards' "Java WebSocket Programming book" that looks like this:

import javax.websocket.server.ServerEndpoint;
import javax.websocket.OnMessage;

@ServerEndpoint (value="/echo")
public class EchoServer
{
    @OnMessage
    public String echo (String incomingMessage)
    {
    return "I got this (" + incomingMessage + "), so I am sending it back!";
    }
}

I compiled it using gradle 2.7 (see build.gradle below)

apply plugin: 'java'
apply plugin: "war"

repositories {
    mavenCentral()
}

dependencies {
        compile 'javax.websocket:javax.websocket-api:1.1'
}

and placed it in the webapps directory of a plain (HTTP only) Jetty server ( jetty-distribution-9.3.6.v20151106 ) and I could make it talk with the client that accessed the WebSocket interface like so:

            var wsUri = "ws://localhost:8080/echoserver/echo";

Then, I took the same *.war file, and placed it on a secure jetty server (running jetty-9.1.3.v20140225 ) but the deployment didn't work, and my access of the server using (note wss now)

            var wsUri = "wss://localhost:8443/echoserver/echo";

didn't work for an error. The specific error, from the JavaScript console on chrome, is 'WebSocket connection to 'wss://localhost:8443/echoserver/echo' failed: Error during WebSocket handshake: Unexpected Response code: 503' . But I think it is because there was a RunTimeException scanning EchoServer.class that it happened (as evinced in the jetty stderrout.log ):

2015-11-20 15:32:14.320:INFO:oejs.Server:main: jetty-9.1.3.v20140225
2015-11-20 15:32:14.329:INFO:oejs.AbstractNCSARequestLog:main: Opened /opt/jetty/logs/2015_11_20.request.log
2015-11-20 15:32:14.329:INFO:oejs.AbstractNCSARequestLog:main: Opened /opt/jetty/logs/2015_11_20.request.log
2015-11-20 15:32:14.331:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/opt/jetty/webapps/] at interval 1
2015-11-20 15:32:14.421:INFO:oeja.AnnotationConfiguration:main: Scanned 1 container path jars, 1 WEB-INF/lib jars, 1 WEB-INF/classes d
irs in 17ms for context o.e.j.w.WebAppContext@4c70fda8{/echoserver,file:/tmp/jetty-0.0.0.0-8443-echoserver.war-_echoserver-any-7817985
952118790015.dir/webapp/,STARTING}{/echoserver.war}
2015-11-20 15:32:14.421:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@4c70fda8{/echoserver,file:/tmp/j
etty-0.0.0.0-8443-echoserver.war-_echoserver-any-7817985952118790015.dir/webapp/,STARTING}{/echoserver.war}
java.lang.RuntimeException: Error scanning file EchoServer.class
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:705)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parse(AnnotationParser.java:821)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$ParserTask.call(AnnotationConfiguration.java:159)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$1.run(AnnotationConfiguration.java:542)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
    at java.lang.Thread.run(Thread.java:745)
Caused by: 
java.lang.IllegalArgumentException
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.eclipse.jetty.annotations.AnnotationParser.scanClass(AnnotationParser.java:970)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:700)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parse(AnnotationParser.java:821)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$ParserTask.call(AnnotationConfiguration.java:159)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$1.run(AnnotationConfiguration.java:542)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
    at java.lang.Thread.run(Thread.java:745)

My environment is a Ubuntu 14.04/x64/Oracle java 8. BTW, I downloaded Danny Cowards' source code from the website, and it is exactly the same code I have. However, it comes with its own mystery *.war (in the dist directory, but the ant build.xml is clearly referring to all manner of NetBeans dependencies which have no business being there, ergo I could not compile it using ant ) and placed it in the HTTPS jetty and it runs exactly as expected, and I do not get an error when I access it using wss: , so it is not a SSL/self-signed-certificate issue, or so I think.

Can anyone tell me what might be the issue here? Any help with this is deeply appreciated.

Thanks, Sonny.

PS: I have checked out a number of WebSocket/SSL/Jetty posts on SO, but they do not seem to address the problem I am having with Jetty.

Jetty 9.1.x is too old for JSR356 (javax.websocket) use.

Use Jetty 9.2.x or Jetty 9.3.x for JSR356 support.

Since you seem to be using Java 8, use version Jetty version 9.3.6.v20151106 .

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