All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.lightflame.nsqconsumer.NsqConsumerHandler Maven / Gradle / Ivy

The newest version!
package io.lightflame.nsqconsumer;

import java.util.Queue;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * NsqConsumerHandler
 */
public class NsqConsumerHandler extends SimpleChannelInboundHandler{

    private MessageProcessing messageProcessing;


    public NsqConsumerHandler(NsqConfig config) {
        this.messageProcessing = new MessageProcessing(config);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx){
        messageProcessing.magic(ctx).sub(ctx);
    }


    @Override
    public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception{
        Queue q = messageProcessing
            .addBuffer(in)
            .buildQueue()
            .getQueue();

        while (!q.isEmpty()){
            FrameType f = q.poll();
            f.proccess(ctx);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
        cause.printStackTrace();
        ctx.close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy