Jetty,websockets和java.lang.NoSuchMethodError

时间:2013-09-26 15:31:16

标签: websocket jetty

我正在尝试在Jetty 8.1上运行Camel Server。 WebSocket运行正常,我可以在Init()方法中看到打印。

但是因为我在camel-context.txt中设置了一个WebSocket:

<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);
        }

    }

}

我在classpath中读过很多关于缺少.jar的论坛,当我打开lib .jar时,我的classpath中有“缺少方法”。

我尝试使用javax.WebSocket并导入org.eclipse.jetty.websocket.WebSocket。总是一样的错误。我想在每次调用“to”路由时在我的webSocket中运行一个特定的方法。理想情况下,将消息发送到客户列表。

你有什么想法???

1 个答案:

答案 0 :(得分:0)

您的类路径中有多个slf4j jar。可能与版本不匹配。

Stack Overflow已经回答了这个问题:Exception thrown while using logback/slf4j