我正在尝试在Jetty 8.1上运行驼峰服务器。WebSocket运行正常,我可以看到Init()方法中的打印。
但是由于我在驼峰上下文中设置了一个 WebSocket.txt:
<to uri="ws:JettyAlarmEndpoint"/>
我在 Jetty 控制台中遇到此错误:
[ main] ContextLoader ERROR Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [META-INF/spring/camel-context.xml]; nested exception is org.springfr
amework.beans.FatalBeanException: Invalid NamespaceHandler class [org.apache.camel.spring.handler.CamelNamespaceHandler] for namespace [http://camel.apache.org/schema/spring]: problem with handler cla
ss file or dependent class; nested exception is java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
即: java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
我的 WebSocket 很简单,如您所见:
package com.thales.ktdscis.server;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocket.OnTextMessage;
import org.eclipse.jetty.websocket.WebSocketServlet;
import com.thales.ktdscis.utils.JSONUtils;
/**
* Servlet implementation class RiaWebSocket
*/
@WebServlet("/test")
public class JettyEndpoint extends WebSocketServlet {
private static final long serialVersionUID = -1798046558917516877L;
public static final boolean DEBUG = false;
public final Set<ObserverWebSocket> users = new CopyOnWriteArraySet<ObserverWebSocket>();
@Override
public void init() throws ServletException {
System.out.println(" XXX JettyEndpoint().Init() XXX ");
super.init();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
return new ObserverWebSocket(users);
}
public class ObserverWebSocket implements OnTextMessage {
private Connection connection;
private Set<ObserverWebSocket> users;
public ObserverWebSocket(Set<ObserverWebSocket> users) {
this.users = users;
}
public void onMessage(String message) {
System.out.println("Message received : " + message);
unicast(message);
}
private void unicast(String message) {
try {
this.connection.sendMessage(message);
}
catch (IOException e) {
e.printStackTrace();
}
}
private void broadcast(String message) {
for (ObserverWebSocket user : users) {
try {
user.connection.sendMessage(message);
}
catch (Exception e) {
}
}
}
@Override
public void onOpen(Connection connection) {
this.connection = connection;
users.add(this);
}
@Override
public void onClose(int closeCode, String message) {
users.remove(this);
}
}
}
我已经阅读了很多关于类路径中缺少.jar的论坛,当我打开 lib .jar时,我的类路径中有"缺少方法"。
我尝试过javax。WebSocket 和 import org.eclipse.jetty.websocket.WebSocket.总是相同的错误。我想在每次调用"to"路由时在我的 webSocket 中运行一个特定的方法。理想情况下,向客户端列表发送消息。
你知道???吗
你的类路径中有多个 slf4j jar。 可能版本不匹配。
这已经在堆栈溢出中得到了解答:使用logback/slf4j时引发的异常