netty-服务器和客户端 互相读写数据.md

字符串类型 报错

1.报错

java.lang.UnsupportedOperationException: unsupported message type: String (expected: ByteBuf, FileRegion)

2.应用程序代码

官方demo

/*

 * Copyright 2012 The Netty Project

 *

 * The Netty Project licenses this file to you under the Apache License,

 * version 2.0 (the "License"); you may not use this file except in compliance

 * with the License. You may obtain a copy of the License at:

 *

 *   http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

 * License for the specific language governing permissions and limitations

 * under the License.

 */

package io.netty.example.echo;



import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;



/**

 * Handler implementation for the echo client.  It initiates the ping-pong

 * traffic between the echo client and server by sending the first message to

 * the server.

 */

public class EchoClientHandler extends ChannelInboundHandlerAdapter {



    private final ByteBuf buffer;



    /**

     * Creates a client-side handler.

     */

    public EchoClientHandler() {

    	//大小

        buffer = Unpooled.buffer(EchoClient.SIZE);

        for (int i = 0; i < buffer.capacity(); i ++) {

            buffer.writeByte((byte) i);

        }

        

        //如何写数据到ByteBuf?

//        for (int i = 0; i < buffer.capacity(); i ++) {

//            byte b = buffer.writeByte(i);

//            System.out.println((char) b);

//        }

        

        //打印

        for (int i = 0; i < buffer.capacity(); i ++) {

            byte b = buffer.getByte(i);

            System.out.println((char) b);

        }

    }



    @Override

    public void channelActive(ChannelHandlerContext ctx) {

        ctx.writeAndFlush(buffer); //writeAndFlush()方法不能写字符串类型 会报错

    } 



    @Override

    public void channelRead(ChannelHandlerContext ctx, Object msg) {

        ctx.write((String)msg + 1);

    }



    @Override

    public void channelReadComplete(ChannelHandlerContext ctx) {

       ctx.flush();

    }



    @Override

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

        // Close the connection when an exception is raised.

        cause.printStackTrace();

        ctx.close();

    }

}



复制代码
3.netty源码
public abstract class AbstractChannel extends DefaultAttributeMap implements Channel {



@Override

        public final void write(Object msg, ChannelPromise promise) {

            assertEventLoop();



            ChannelOutboundBuffer outboundBuffer = this.outboundBuffer;

            if (outboundBuffer == null) {

                // If the outboundBuffer is null we know the channel was closed and so

                // need to fail the future right away. If it is not null the handling of the rest

                // will be done in flush0()

                // See https://github.com/netty/netty/issues/2362

                safeSetFailure(promise, WRITE_CLOSED_CHANNEL_EXCEPTION);

                // release message now to prevent resource-leak

                ReferenceCountUtil.release(msg);

                return;

            }



            int size;

            try {

                msg = filterOutboundMessage(msg); //报错

                size = pipeline.estimatorHandle().size(msg);

                if (size < 0) {

                    size = 0;

                }

            } catch (Throwable t) { //捕获异常:直接把异常吃掉了 不会再抛出 也不会向上抛出

                safeSetFailure(promise, t);

                ReferenceCountUtil.release(msg);

                return;

            }



            outboundBuffer.addMessage(msg, size, promise);

        }


复制代码
@Override

    protected final Object filterOutboundMessage(Object msg) {

        if (msg instanceof ByteBuf) { //判断是否ByteBuf类型

            ByteBuf buf = (ByteBuf) msg;

            if (buf.isDirect()) {

                return msg;

            }



            return newDirectBuffer(buf);

        }



        if (msg instanceof FileRegion) { //判断是否是FileRegion类型 如果2种都不是 

            return msg;

        }



        throw new UnsupportedOperationException( //向上抛异常

                "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES);

    }


复制代码

ByteBuf类型

1.应用程序代码

官方demo

/*

 * Copyright 2012 The Netty Project

 *

 * The Netty Project licenses this file to you under the Apache License,

 * version 2.0 (the "License"); you may not use this file except in compliance

 * with the License. You may obtain a copy of the License at:

 *

 *   http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

 * License for the specific language governing permissions and limitations

 * under the License.

 */

package io.netty.example.factorial;



import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelFutureListener;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;



import java.math.BigInteger;

import java.util.concurrent.BlockingQueue;

import java.util.concurrent.LinkedBlockingQueue;



/**

 * Handler for a client-side channel.  This handler maintains stateful

 * information which is specific to a certain channel using member variables.

 * Therefore, an instance of this handler can cover only one channel.  You have

 * to create a new handler instance whenever you create a new channel and insert

 * this handler to avoid a race condition.

 */

public class FactorialClientHandler extends SimpleChannelInboundHandler<BigInteger> {



    private ChannelHandlerContext ctx;

    private int receivedMessages;

    private int next = 1;

    final BlockingQueue<BigInteger> answer = new LinkedBlockingQueue<BigInteger>();



    public BigInteger getFactorial() {

        boolean interrupted = false;

        try {

            for (;;) {

                try {

                    return answer.take();

                } catch (InterruptedException ignore) {

                    interrupted = true;

                }

            }

        } finally {

            if (interrupted) {

                Thread.currentThread().interrupt();

            }

        }

    }



    @Override

    public void channelActive(ChannelHandlerContext ctx) {

        this.ctx = ctx;

        sendNumbers();

    }



    @Override

    public void channelRead0(ChannelHandlerContext ctx, final BigInteger msg) {

        receivedMessages ++;

        if (receivedMessages == FactorialClient.COUNT) {

            // Offer the answer after closing the connection.

            ctx.channel().close().addListener(new ChannelFutureListener() {

                @Override

                public void operationComplete(ChannelFuture future) {

                    boolean offered = answer.offer(msg);

                    assert offered;

                }

            });

        }

    }



    @Override

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

        cause.printStackTrace();

        ctx.close();

    }



    private void sendNumbers() {

        // Do not send more than 4096 numbers.

        ChannelFuture future = null;

        for (int i = 0; i < 4096 && next <= FactorialClient.COUNT; i++) {

            future = ctx.write(Integer.valueOf(next)); //write()方法可以写Integer类型

            next++;

        }

        if (next <= FactorialClient.COUNT) {

            assert future != null;

            future.addListener(numberSender);

        }

        ctx.flush();

    }



    private final ChannelFutureListener numberSender = new ChannelFutureListener() {

        @Override

        public void operationComplete(ChannelFuture future) throws Exception {

            if (future.isSuccess()) {

                sendNumbers();

            } else {

                future.cause().printStackTrace();

                future.channel().close();

            }

        }

    };

}


复制代码
2.netty源码

@Override

public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

    ByteBuf buf = null;

    try {

        if (acceptOutboundMessage(msg)) { //在这里 不同的数据类型 会进行转换 1.字符串 没有转换 具体来说 就是没有转换为ByteBuf类型 所以导致后面报错 2.Integer 会被转换为ByteBuf的子类 所以不会报错

            @SuppressWarnings("unchecked")

            I cast = (I) msg;

            buf = allocateBuffer(ctx, cast, preferDirect);

            try {

                encode(ctx, cast, buf);

            } finally {

                ReferenceCountUtil.release(cast);

            }



            if (buf.isReadable()) {

                ctx.write(buf, promise);

            } else {

                buf.release();

                ctx.write(Unpooled.EMPTY_BUFFER, promise);

            }

            buf = null;

        } else {

            ctx.write(msg, promise);

        }

    } catch (EncoderException e) {

        throw e;

    } catch (Throwable e) {

        throw new EncoderException(e);

    } finally {

        if (buf != null) {

            buf.release();

        }

    }

}
复制代码

转载于:https://juejin.im/post/5c54627ee51d457fa277e130

2023-07-14 15:19:01.215 WARN 7308 --- [sson-netty-2-15] io.netty.util.concurrent.DefaultPromise : An exception was thrown by org.redisson.misc.RedissonPromise$$Lambda$888/0x00000008008f7440.operationComplete() java.lang.NullPointerException: null 2023-07-14 15:19:01.216 ERROR 7308 --- [sson-netty-2-15] o.r.c.SentinelConnectionManager : Can't execute SENTINEL commands on /172.24.107.11:26379 org.redisson.client.RedisException: ERR No such master with that name. channel: [id: 0x2d66827d, L:/172.23.9.103:46812 - R:/172.24.107.11:26379] command: (SENTINEL SLAVES), params: [mymaster] at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:365) ~[redisson-3.13.3.jar:3.13.3] at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:196) ~[redisson-3.13.3.jar:3.13.3] at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:134) ~[redisson-3.13.3.jar:3.13.3] at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:104) ~[redisson-3.13.3.jar:3.13.3] at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:501) ~[netty-codec-4.1.51.Final.jar:4.1.51.Final] at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366) ~[netty-codec-4.1.51.Final.jar:4.1.51.Final] at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276) ~[netty-codec-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.51.Final.jar:4.1.51.Final] at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.51.Final.jar:4.1.51.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.51.Final.jar:4.1.51.Final] at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na] 解决方法
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值