Using Netty 4, how do I write a handler that can inject messages on a periodic basis and still pass messages through? -


i trying write class injects message channel every minute. have figured out how accomplish using code below, think flush method wrong. after flushing upstream messages, noticing socket gets closed.

public class pinger extends channeloutboundmessagehandleradapter<bytebuf> {     private static final bytebuf dummy = unpooled.wrappedbuffer("dummy".getbytes());      @override     public void connect(channelhandlercontext ctx, socketaddress remoteaddress, socketaddress localaddress, channelpromise promise) throws exception{         super.connect(ctx, remoteaddress, localaddress, promise);          ctx.executor().scheduleatfixedrate(new repeattask(ctx), 0, 60, timeunit.seconds);     }      private final class repeattask implements runnable {         private final channelhandlercontext ctx;          public repeattask(channelhandlercontext ctx){             this.ctx = ctx;         }          public void run() {             if(ctx.channel().isactive()){                 ctx.write(dummy.copy());             }         }     }      @override     public void flush(channelhandlercontext ctx, bytebuf msg) throws exception {         ctx.nextoutboundmessagebuffer().add(msg);         ctx.flush();     } } 

i note handler in middle of complex pipeline.

i think figured out how make work. channelstatehandleradapter better class inherit from.

public class pinger extends channelstatehandleradapter {     private static final bytebuf dummy = unpooled.wrappedbuffer("dummy".getbytes());      @override     public void channelactive(channelhandlercontext ctx) throws exception {         ctx.firechannelactive();         ctx.executor().scheduleatfixedrate(new pingtask(ctx), 0, 60, timeunit.seconds);     }      private final class repeattask implements runnable {         private final channelhandlercontext ctx;          public repeattask(channelhandlercontext ctx){             this.ctx = ctx;         }          public void run() {             if(ctx.channel().isactive()){                 ctx.write(dummy.copy());             }         }     }      @override     public void inboundbufferupdated(channelhandlercontext ctx)             throws exception {         ctx.fireinboundbufferupdated();     } } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -