本文介绍了无法将DefaultHttpResponse写入netty的通道:“不支持的消息类型:类org.jboss.netty.handler.codec.http.DefaultHttpResponse"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此特定问题与.

This specific question is related to a possible resolution to the following question.

我有两个管道,其中一个通道接收一个客户端请求,然后在第二个通道触发一个请求,然后将响应内容复制到新的DefaultHttpResponse中,然后尝试将其写入原始通道.但是,这导致以下异常:

I have two pipelines in which I'm receiving a client request one one channel, then firing off a request on a second channel, then copying the response content into a new DefaultHttpResponse, which I attempt to write to the original channel. However, this results in the following exception:

java.lang.IllegalArgumentException: unsupported message type: class org.jboss.netty.handler.codec.http.DefaultHttpResponse
at org.jboss.netty.channel.socket.nio.SocketSendBufferPool.acquire(SocketSendBufferPool.java:53)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.write0(AbstractNioWorker.java:468)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromTaskLoop(AbstractNioWorker.java:432)
at org.jboss.netty.channel.socket.nio.AbstractNioChannel$WriteTask.run(AbstractNioChannel.java:366)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processWriteTaskQueue(AbstractNioWorker.java:350)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:246)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

有人知道为什么会这样吗?作为参考,这是我的两个管道的代码(我知道我在这里正在做一些线程不安全的事情,但是在继续之前,我正在尝试使此过程适用于单线程客户端):

Does anyone know why this might be happening? For reference, here's the code for my two pipelines (I know I'm doing some thread unsafe things here, but I'm trying to get this working for a single-threaded client before I move on):

private static Channel channel;
private static Map<Channel, Channel> proxyToClient = new ConcurrentHashMap<Channel, Channel>();

public static void main(String[] args) throws Exception {
    ChannelFactory clientFactory =
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool());
    final ClientBootstrap cb = new ClientBootstrap(clientFactory);
    cb.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            return Channels.pipeline(
                    new HttpRequestEncoder(),
                    new HttpResponseDecoder(),
                    new ResponseHandler());
        }
    });
    ChannelFuture cf = cb.connect(new InetSocketAddress("localhost", 18080));
    channel = cf.awaitUninterruptibly().getChannel();

    ChannelFactory factory =
            new NioServerSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool());
    ServerBootstrap sb = new ServerBootstrap(factory);

    sb.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            return Channels.pipeline(
                    new HttpRequestDecoder(),
                    new RequestHandler());
        }
    });

    sb.setOption("child.tcpNoDelay", true);
    sb.setOption("child.keepAlive", true);

    sb.bind(new InetSocketAddress(2080));
}

private static class ResponseHandler extends SimpleChannelHandler {

    @Override
    public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) {
        final HttpResponse proxyResponse = (HttpResponse) e.getMessage();
        Channel clientChannel = proxyToClient.get(e.getChannel());
        HttpResponse clientResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        clientResponse.setContent(proxyResponse.getContent());
        clientChannel.write(clientResponse).addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
                Channel ch = future.getChannel();
                ch.close();
            }
        });
    }
}

private static class RequestHandler extends SimpleChannelHandler {

    @Override
    public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) {
        final HttpRequest request = (HttpRequest) e.getMessage();
        proxyToClient.put(channel, e.getChannel());
        channel.write(request);
    }
}

推荐答案

如何在服务器管道中添加HttpResponseEncoder?您的服务器应该知道如何将DefaultHttpResponse对象转换为字节,以便可以通过电线发送它.

How about adding a HttpResponseEncoder in your server pipeline? Your server should know how to convert the DefaultHttpResponse object to bytes so it can send it over the wire.

这篇关于无法将DefaultHttpResponse写入netty的通道:“不支持的消息类型:类org.jboss.netty.handler.codec.http.DefaultHttpResponse"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!