lettuce的Cannot encode command. Please close the connection as the connection state may be outof sync

博客中提到的问题是由于尝试将Integer类型的数据作为String传递给RedisCommands的set方法导致的。这个异常通常发生在编码过程中,由于类型不匹配,使得连接状态可能已失去同步。解决方法是确保传递给set方法的参数是字符串类型。
摘要由CSDN通过智能技术生成

Exception in thread "main" io.lettuce.core.RedisException: io.netty.handler.codec.EncoderException: Cannot encode command. Please close the connection as the connection state may be out of sync.
    at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:129)
    at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:69)
    at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80)
    at com.sun.proxy.$Proxy0.set(Unknown Source)
    at com.outworld.outworld_chatroom.ChatServer.Test.main(Test.java:53)
Caused by: io.netty.handler.codec.EncoderException: Cannot encode command. Please close the connection as the connection state may be out of sync.
    at io.lettuce.core.protocol.CommandEncoder.encode(CommandEncoder.java:94)
    at io.lettuce.core.protocol.CommandEncoder.encode(CommandEncoder.java:76)
    at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:107)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:716)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:708)
    at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:791)
    at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:701)
    at io.lettuce.core.protocol.CommandHandler.writeSingleCommand(CommandHandler.java:421)
    at io.lettuce.core.protocol.CommandHandler.write(CommandHandler.java:353)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:716)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:708)
    at io.netty.channel.AbstractChannelHandlerContext.access$1700(AbstractChannelHandlerContext.java:56)
    at io.netty.channel.AbstractChannelHandlerContext$AbstractWriteTask.write(AbstractChannelHandlerContext.java:1102)
    at io.netty.channel.AbstractChannelHandlerContext$WriteAndFlushTask.write(AbstractChannelHandlerContext.java:1149)
    at io.netty.channel.AbstractChannelHandlerContext$AbstractWriteTask.run(AbstractChannelHandlerContext.java:1073)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:510)
    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:413)
    at io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1050)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    at io.lettuce.core.codec.StringCodec.encodeValue(StringCodec.java:39)
    at io.lettuce.core.protocol.CommandArgs$ValueArgument.encode(CommandArgs.java:703)
    at io.lettuce.core.protocol.CommandArgs.encode(CommandArgs.java:347)
    at io.lettuce.core.protocol.Command.encode(Command.java:120)
    at io.lettuce.core.protocol.AsyncCommand.encode(AsyncCommand.java:185)
    at io.lettuce.core.protocol.CommandEncoder.encode(CommandEncoder.java:91)
    ... 21 more

Process finished with exit code 1
 

原因出在RedisCommands的set方法,虽然写int编译通过,但是还是得传字符串

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值