问题
I am trying to run a Web socket client under the context of Tomcat. So the scenario is, I have a Websocket Server Endpoint deployed on Tomcat, as part of App-1. App-2, which is also deployed on Tomcat (obviously under different context root), want to send a message.
The class in App-2 works fine, if I run it as a stand alone from a main method. But it gives the below error, when it runs under context of tomcat. (I am using Tyrus 1.9 as JSR-356 impl)
java.util.ServiceConfigurationError: javax.websocket.ContainerProvider: Provider org.apache.tomcat.websocket.WsContainerProvider not a subtype
at java.util.ServiceLoader.fail(ServiceLoader.java:239)
at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:66)
The above exception is thrown at this line:
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
Any suggestion on how to fix this will be very helpful.
回答1:
Got it. The issue was, I was including tyrus stand alone client for importing client packages. I replaced the below
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>1.9</version>
</dependency>
with this
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
It solved the issue.
回答2:
For Reference only:
On a Debian or Ubuntu configuration running tomcat 7, the Websocket jars are not packaged with the standard installation of Tomcat. (see https://bugs.launchpad.net/ubuntu/+source/tomcat7/+bug/1326687)
Therefore in order to fix this :
- Do not include the websocket jars in your webapp, this should come from tomcat itself otherwise it will not work. Having a duplicate jar in your webapp lib folder will prevent tomcat from starting properly.
Copy and link the 2 websocket jars in the java/tomcat installation folder
cp javax.websocket-api-1.1.jar /usr/share/java/
ln -s ../../java/javax.websocket-api-1.1.jar /usr/share/tomcat7/lib/javax.websocket-api-1.1.jar
cp tomcat7-websocket.jar /usr/share/java/
ln -s ../../java/tomcat7-websocket.jar /usr/share/tomcat7/lib/tomcat7-websocket.jar
You can find the jar files at:
https://mvnrepository.com/artifact/javax.websocket/javax.websocket-api
https://mvnrepository.com/artifact/org.apache.tomcat/tomcat7-websocket
回答3:
This issue was fixed for me when I switched from Tomcat 7 to Tomcat 8.
I got this issue when running a Web App within Eclipse via the Servers Tab. I was running on Tomcat 7 and after downloading Tomcat 8 and running that instead, this issue went away.
来源:https://stackoverflow.com/questions/33495227/web-socket-client-not-working-in-tomcat