Files
jsowell-charger-web/jsowell-pile/src/main/java/com/jsowell/pile/rpc/RpcRequestHandler.java

38 lines
1.3 KiB
Java
Raw Normal View History

2024-08-01 18:03:21 +08:00
package com.jsowell.pile.rpc;
2024-07-31 16:48:29 +08:00
2024-08-01 18:03:21 +08:00
import com.jsowell.common.protocol.RpcRequest;
import com.jsowell.common.protocol.RpcResponse;
2024-07-31 16:48:29 +08:00
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
public class RpcRequestHandler extends SimpleChannelInboundHandler<RpcRequest> {
private final static EventLoopGroup worker = new DefaultEventLoopGroup(Runtime.getRuntime().availableProcessors() + 1);
@Override
protected void channelRead0(ChannelHandlerContext ctx, RpcRequest msg) throws Exception {
// 为避免占用网络io此处异步进行处理
worker.submit(() -> {
System.out.println("[RpcRequestHandler] "+ Thread.currentThread().getName() +" 处理请求msg " + msg);
// 模拟处理耗时
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
RpcResponse rpcResponse = new RpcResponse();
rpcResponse.setId(msg.getId());
rpcResponse.setResult("处理" + msg.getParam());
ctx.writeAndFlush(rpcResponse);
});
}
}