首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoClassDefFoundError和Netty

NoClassDefFoundError和Netty
EN

Stack Overflow用户
提问于 2011-01-01 19:21:17
回答 1查看 14.1K关注 0票数 2

首先要说的是,我是Java语言中的n00b。我能理解大多数概念,但在我的情况下,我希望有人能帮助我。我使用JBoss Netty处理简单的http请求,并使用MemCachedClient检查memcached中是否存在客户端ip。

代码语言:javascript
复制
import org.jboss.netty.channel.ChannelHandler;
import static org.jboss.netty.handler.codec.http.HttpHeaders.*;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.*;
import static org.jboss.netty.handler.codec.http.HttpVersion.*;
import com.danga.MemCached.*;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.Cookie;
import org.jboss.netty.handler.codec.http.CookieDecoder;
import org.jboss.netty.handler.codec.http.CookieEncoder;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpChunkTrailer;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.QueryStringDecoder;
import org.jboss.netty.util.CharsetUtil;

/**
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
* @author Andy Taylor (andy.taylor@jboss.org)
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
*
* @version $Rev: 2368 $, $Date: 2010-10-18 17:19:03 +0900 (Mon, 18 Oct 2010) $
*/
@SuppressWarnings({"ALL"})
public class HttpRequestHandler extends SimpleChannelUpstreamHandler {

    private HttpRequest request;
    private boolean readingChunks;
    /** Buffer that stores the response content */
    private final StringBuilder buf = new StringBuilder();
    protected MemCachedClient mcc = new MemCachedClient();
    private static SockIOPool poolInstance = null;

    static {

// server list and weights
        String[] servers =
                {
                        "lcalhost:11211"
                };

//Integer[] weights = { 3, 3, 2 };
        Integer[] weights = {1};

// grab an instance of our connection pool
        SockIOPool pool = SockIOPool.getInstance();

// set the servers and the weights
        pool.setServers(servers);
        pool.setWeights(weights);

// set some basic pool settings
// 5 initial, 5 min, and 250 max conns
// and set the max idle time for a conn
// to 6 hours
        pool.setInitConn(5);
        pool.setMinConn(5);
        pool.setMaxConn(250);
        pool.setMaxIdle(21600000); //1000 * 60 * 60 * 6

// set the sleep for the maint thread
// it will wake up every x seconds and
// maintain the pool size
        pool.setMaintSleep(30);

// set some TCP settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
        pool.setNagle(false);
        pool.setSocketTO(3000);
        pool.setSocketConnectTO(0);

// initialize the connection pool
        pool.initialize();


// lets set some compression on for the client
// compress anything larger than 64k
        //mcc.setCompressEnable(true);
        //mcc.setCompressThreshold(64 * 1024);
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        HttpRequest request = this.request = (HttpRequest) e.getMessage();
        if(mcc.get(request.getHeader("X-Real-Ip")) != null)
        {
            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
            response.setHeader("X-Accel-Redirect", request.getUri());
            ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
        }
        else {
            sendError(ctx, NOT_FOUND);
        }

    }

    private void writeResponse(MessageEvent e) {
        // Decide whether to close the connection or not.
        boolean keepAlive = isKeepAlive(request);

        // Build the response object.
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        response.setContent(ChannelBuffers.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
        response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");

        if (keepAlive) {
            // Add 'Content-Length' header only for a keep-alive connection.
            response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
        }

        // Encode the cookie.
        String cookieString = request.getHeader(COOKIE);
        if (cookieString != null) {
            CookieDecoder cookieDecoder = new CookieDecoder();
            Set<Cookie> cookies = cookieDecoder.decode(cookieString);
            if(!cookies.isEmpty()) {
                // Reset the cookies if necessary.
                CookieEncoder cookieEncoder = new CookieEncoder(true);
                for (Cookie cookie : cookies) {
                    cookieEncoder.addCookie(cookie);
                }
                response.addHeader(SET_COOKIE, cookieEncoder.encode());
            }
        }

        // Write the response.
        ChannelFuture future = e.getChannel().write(response);

        // Close the non-keep-alive connection after the write operation is done.
        if (!keepAlive) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
            throws Exception {
        e.getCause().printStackTrace();
        e.getChannel().close();
    }


    private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
         HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
         response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
         response.setContent(ChannelBuffers.copiedBuffer(
                 "Failure: " + status.toString() + "\r\n",
                 CharsetUtil.UTF_8));

         // Close the connection as soon as the error message is sent.
         ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
    }

}

当我尝试发送像http://127.0.0.1:8090/1/2/3这样的请求时,我会收到

代码语言:javascript
复制
java.lang.NoClassDefFoundError: com/danga/MemCached/MemCachedClient
        at httpClientValidator.server.HttpRequestHandler.<clinit>(HttpRequestHandler.java:66)

我相信它与类路径无关。可能它与mcc不存在的上下文有关。感谢您的任何帮助

编辑:原始代码http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/http/snoop/package-summary.html我已经修改了一些部分以满足我的需要。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-01 20:21:14

为什么您认为这与类路径无关?当您需要的jar不可用时,就会出现这种类型的错误。你如何启动你的应用程序?

编辑

对不起-我在eclipse中加载并尝试了java_memcached-release_2.5.2包,到目前为止没有发现任何问题。调试类加载没有发现任何异常。除了一些需要仔细检查的提示之外,我无能为力:

  • 确保您的下载是正确的。再次下载并解压缩。(是com.sloner.* available?)
  • make确保您使用>java1.5
  • 确保您的类路径正确和完整。您所展示的示例不包括netty。它在哪里,

  • ,我不熟悉由向清单中添加类路径而产生的交互。也许可以恢复到普通样式,将所有需要的jar (memcached、netty、您的)添加到类路径中,并引用主类来启动,而不是可启动的jar文件
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4573893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档