【问题标题】:Netty 4 not work in more than 4 channelsNetty 4 不能在超过 4 个通道中工作
【发布时间】:2015-05-03 17:02:14
【问题描述】:

我有一些 netty-server 用于在 amazon s3 上上传文件。我尝试用 jmeter 进行一些测试,但我遇到了奇怪的问题:我的服务器只有 4 个文件并开始上传它们。其他文件在这 4 个文件之一完成上传后处理。我认为netty必须与许多线程一起工作,而不是4.对不起我的英语,我需要帮助并希望得到你的建议。 有我的处理程序

 protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject msg) throws Exception {

    if (msg instanceof HttpRequest) {
        System.out.println("request");
        request = (HttpRequest) msg;
        reqURI = request.getUri();
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(reqURI);
        switch (queryStringDecoder.path()) {
            case ("/getfile"):
                sendSimpleResponse(channelHandlerContext, OK, "getfile");
                break;
            case ("/upload"):
                showUploadForm(channelHandlerContext);
                break;
            case ("/formpostmultipart"):
                if (request.getMethod().equals(HttpMethod.POST)) {
                    decoder = new HttpPostRequestDecoder(dataFactory, request);
                    decoder.setDiscardThreshold(0);
                } else {
                    sendSimpleResponse(channelHandlerContext, BAD_REQUEST, "");
                }
                break;
            default:
                sendSimpleResponse(channelHandlerContext, NOT_FOUND, "");

        }
    }

    if (decoder != null) {
        if (msg instanceof HttpContent) {
            HttpContent chunk = (HttpContent) msg;
            decoder.offer(chunk);
            readChunk(channelHandlerContext);
            if (chunk instanceof LastHttpContent) {
                resetPostRequestDecoder();
            }
        }
    }
}
private void readChunk(ChannelHandlerContext ctx) throws IOException {
    while (decoder.hasNext()) {
        InterfaceHttpData data = decoder.next();
        if (data != null) {
            try {
                switch (data.getHttpDataType()) {
                    case Attribute:
                        break;
                    case FileUpload:
                        FileUpload fileUpload = (FileUpload) data;
                        System.out.println("send file call");
                        logger.info("start upload");
                        AWSfunctions aws = new AWSfunctions();
                        aws.sendFile(fileUpload);
                        break;
                }
            } finally {
                data.release();
            }
        }
    }
}

这个类与amasons3一起工作。 (您可以看到我尝试为客户端提供更多连接,但这也不起作用)

public class AWSfunctions {
AmazonS3 connection;

AWSfunctions() {
    Config conf;
    String AccessKeyId;
    String SecretKey;
    AWSCredentials credentials;
    ClientConfiguration clientConfiguration;
    conf = ConfigFactory.load();
    AccessKeyId = conf.getString("AWSCredentials.AWSAccessKeyId");
    SecretKey = conf.getString("AWSCredentials.AWSSecretKey");
    credentials = new BasicAWSCredentials(AccessKeyId, SecretKey);
    clientConfiguration = new ClientConfiguration();
    clientConfiguration.setProtocol(Protocol.HTTP);
    clientConfiguration.setMaxConnections(200);
    System.out.println(clientConfiguration.getMaxConnections());
    connection = new AmazonS3Client(credentials, clientConfiguration);
}

public void sendFile(FileUpload fileUpload) throws IOException {
    Random rnd = new Random();
    File file;
    file = fileUpload.getFile();
    System.out.println("upload started");
    connection.putObject(new PutObjectRequest("test-for-est", String.valueOf(rnd.nextInt()), file));
    System.out.println("Upload completed");
}}

【问题讨论】:

    标签: java file-upload amazon-web-services amazon-s3 netty


    【解决方案1】:

    哦,很简单。我对服务器类做了一些更改...

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ServerInitializer());
            ChannelFuture ch = b.bind(port).sync();
            ch.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
    

    改变

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(20);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 2012-11-06
      • 2021-12-20
      • 1970-01-01
      • 2018-01-11
      相关资源
      最近更新 更多