0

I'm trying to implement a jsr 356 websocket connection from within a war deployed in jetty.

I'm using this as a guide: http://aredko.blogspot.com/2013/11/java-websockets-jsr-356-on-jetty-91.html

(I can find lots of tutorials on basically using jsr 356 with an embedded jetty server, but none on building a websocket connection within an existing server - are there any good ones out there?)

Whenever I run the code listed above, I get the following error:

java.lang.LinkageError: loader constraint violation: when resolving method "org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.configureContext(Lorg/eclipse/jetty/servlet/ServletContextHandler;)Lorg/eclipse/jetty/websocket/jsr356/server/ServerContainer;" the class loader (instance of org/eclipse/jetty/webapp/WebAppClassLoader) of the current class, com/me/stuff/data/DataServer, and the class loader (instance of org/eclipse/jetty/start/Classpath$Loader) for resolved class, org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer, have different Class objects for the type y.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.configureContext(Lorg/eclipse/jetty/servlet/ServletContextHandler;)Lorg/eclipse/jetty/websocket/jsr356/server/ServerContainer; used in the signature

which refers to the code

ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);

I'm at a loss to figure this out. I'm using the following lines from gradle to bring in the websocket stuff:

    providedCompile (
    [ group: 'javax.websocket', name: 'javax.websocket-api', version: '+'],
    [ group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '+' ]
    )

    compile (
    [ group: 'org.eclipse.jetty.websocket', name: 'javax-websocket-server-impl', version: '+'],
    [ group: 'org.eclipse.jetty.websocket', name: 'javax-websocket-client-impl', version: '+']
    )

I see that both the javax-websocket-server-impl jar and the javax.websocket-api jar contain ServerContainer classes...but I'm only including one of those in my war file. Is jetty pulling from its own internal older version? Or am I completely misreading the source of the issue?

The jetty version I have is: jetty-9.2.1.v20140609

user2197116
  • 667
  • 3
  • 8
  • 21

1 Answers1

1

The websocket jars should not be in your WAR.

Remove the following jars from your WAR's WEB-INF/lib/ directory.

  • javax.websocket-api-*.jar - the javax.websocket API jar
  • javax-websocket-server-impl-*.jar - the jetty server javax.websocket.server.* implementation jar
  • javax-websocket-client-impl - the jetty javax.websocket.* (client) jar

This is because you are coding/compiling against javax.websocket.* API classes.

The API and the implementation of the API are provided by Jetty itself.

This is no different than when you code against the servlet-api. (namely, that you don't include the servlet-api and the implementation of the servlet-api in your WAR)

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136