我在JEE套接字项目SLF4J中收到错误:无法加载类“org.slf4j.impl.StaticLoggerBinder”

时间:2017-01-03 09:08:18

标签: java maven sockets

当我尝试运行我的服务器代码时..

服务器代码

package Socket;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class MinaTimeServer
{

    private static final int PORT = 1865;

    public static void main( String[] args ) throws IOException
    {
        IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
        acceptor.setHandler( new TimeServerHandler() );
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        acceptor.bind( new InetSocketAddress(PORT) );
    }
}

**Server Handler Code**

package Socket;

import java.util.Date;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class TimeServerHandler extends IoHandlerAdapter   
{

@Override
public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
{
    cause.printStackTrace();
}
@Override
public void messageReceived( IoSession session, Object message ) throws Exception
{
    String str = message.toString();
    System.out.println(str);
    if( str.trim().equalsIgnoreCase("quit") ) {
        session.close();
        return;
    }
    Date date = new Date();
    session.write( "Thank you client"+date.toString() );
    System.out.println("Message written...");
}
@Override
public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
{
    System.out.println( "IDLE " + session.getIdleCount( status ));
}
}

我的Maven pom xml文件存在

<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>2.0.4</version>
</dependency>
 <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

<exclusions>
  <exclusion> 
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
  </exclusion>
  <exclusion> 
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
  </exclusion>
</exclusions> 

但仍然服务器处于运行状态,当客户端与Server连接时,重复打印IDLE 1,IDLE 2,IDLE 3,IDLE 4 .... 而不是打印消息。

1 个答案:

答案 0 :(得分:0)

您的代码将输出:

  

SLF4J:无法加载类&#34; org.slf4j.impl.StaticLoggerBinder&#34;。   SLF4J:默认为无操作(NOP)记录器实现   SLF4J:有关详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder

要说org.slf4j.impl.StaticLoggerBinder类无法加载到内存中。如果在类路径上找不到合适的 SLF4J 绑定,则会发生这种情况。
    来自SLF4J warning or error messages and their meanings

参考

因此,另一种解决方案是在pom.xml中添加此依赖项:

 <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.22</version>
</dependency>