tomcat 7上的Websockets不起作用

时间:2016-01-26 08:38:42

标签: java websocket java-8 tomcat7

我尝试使用java 8在tomcat 7下运行websocket客户端,但它会抛出异常

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/inter] threw exception [Handler processing failed; nested exception is java.util.ServiceConfigurationError: javax.websocket.ContainerProvider: Provider org.apache.tomcat.websocket.WsContainerProvider not a subtype] with root cause
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)
	at com.shop.inter.service.WebsocketClientEndpoint.<init>(WebsocketClientEndpoint.java:29)
 

...

它适用于tomcat 8但不适用于tomcat 7。 这是我的websocket类:

@ClientEndpoint
public class WebsocketClientEndpoint {

	private static final Logger LOGGER = LoggerFactory.getLogger(WebsocketClientEndpoint.class);
	
	private Session userSession = null;
	private MessageHandler messageHandler;

	public WebsocketClientEndpoint(URI endpointURI) {
		try {
			WebSocketContainer container = ContainerProvider.getWebSocketContainer();
			container.connectToServer(this, endpointURI);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Callback hook for Connection open events.
	 * 
	 * @param userSession
	 *            the userSession which is opened.
	 */
	@OnOpen
	public void onOpen(Session userSession) {
		LOGGER.info("opening websocket");
		this.userSession = userSession;
	}

	/**
	 * Callback hook for Connection close events.
	 * 
	 * @param userSession
	 *            the userSession which is getting closed.
	 * @param reason
	 *            the reason for connection close
	 */
	@OnClose
	public void onClose(Session userSession, CloseReason reason) {
		LOGGER.info("closing websocket");
		this.userSession = null;
	}

	/**
	 * Callback hook for Message Events. This method will be invoked when a client send a message.
	 * 
	 * @param message
	 *            The text message
	 */
	@OnMessage
	public void onMessage(String message) {
		if (this.messageHandler != null) {
			this.messageHandler.handleMessage(message);
		}
	}

	/**
	 * Register message handler
	 */
	public void addMessageHandler(MessageHandler msgHandler) {
		this.messageHandler = msgHandler;
	}

	/**
	 * Send a message.
	 */
	public void sendMessage(String message) {
		this.userSession.getAsyncRemote().sendText(message);
	}

	public void closeUserSession() throws IOException {
		this.userSession.close();
	}

	/**
	 * Message handler.
	 * 
	 * @author Jiji_Sasidharan
	 */
	public static interface MessageHandler {

		public void handleMessage(String message);
	}
}

唯一的依赖是javax.websocket-api 1.1

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

根据建议here,尝试将javaee-api添加到您的依赖项中:

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-api</artifactId>
  <version>7.0</version>
</dependency>