when using Netty jvm to communicate, try to report a connection exception using LocalServerChannel and LocalChannel,. The code is as follows:
server code:
public void server() throws InterruptedException {
final EchoServerHandler serverHandler = new EchoServerHandler();
EventLoopGroup group = new DefaultEventLoop();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group, group)
.channel(LocalServerChannel.class)
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(serverHandler);
}
});
ChannelFuture channelFuture = bootstrap.bind(new LocalAddress("foo")).sync();
System.out.println(EchoServer.class.getName() + "--started and listening for connections on--" + channelFuture.channel().localAddress());
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
client code:
public void client() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(LocalChannel.class)
.handler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect(new LocalAddress("foo")).sync();
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
error message:
Exception in thread "main" io.netty.channel.ChannelException: connection refused
at io.netty.channel.local.LocalChannel$LocalUnsafe.connect(LocalChannel.java:375)
at io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1245)
at io.netty.channel.ChannelHandlerInvokerUtil.invokeConnectNow(ChannelHandlerInvokerUtil.java:118)
at io.netty.channel.DefaultChannelHandlerInvoker.invokeConnect(DefaultChannelHandlerInvoker.java:238)
at io.netty.channel.PausableChannelEventExecutor.invokeConnect(PausableChannelEventExecutor.java:107)
at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:493)
at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:487)
at io.netty.channel.DefaultChannelPipeline.connect(DefaultChannelPipeline.java:1018)
at io.netty.channel.AbstractChannel.connect(AbstractChannel.java:241)
at io.netty.bootstrap.Bootstrap$3.run(Bootstrap.java:230)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:328)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)
After reported this exception, I searched Google and checked the Demo related to Github. It didn"t solve the problem, it was on the verge of collapse. Now throw questions on codeshelper and stackoverflow, hoping to get answers.