Netty没有收到预期的消息量

时间:2015-04-27 12:27:27

标签: java netty

当我尝试向我的Netty服务器写入特定数量的消息时,只收到1-3封邮件。我确信该频道是可写的。

这是我的测试代码:

Sending the messages

Channel channel = PluginMessageClient.getClient().getChannel();

for(int i = 0; i < 10; i++)
    channel.writeAndFlush(Unpooled.wrappedBuffer(new byte[]{1}));

Receiving the messages

public class PluginMessageServerHandler extends SimpleChannelInboundHandler<ByteBuf>
{
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception
    {
        System.out.println("test"); //Being printed 1-3 times (varies every time I run the message sending method).
    }
}

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

你说你的频道是可写的,但是连接还活着吗? 你应该使用这个类:

 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  new Intent(this, MainActivity.class), 0);

而不是:

ChannelGroup channels;

因为PluginMessageClient.getClient().getChannel(); 自动管理频道的生命周期。