Newer
Older
rain_receiver / src / main / java / com / casic / handler / ReceiverServerHandler.java
chaizhuang on 23 Mar 2023 1 KB 西工大雨量计
package com.casic.handler;


import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @description: 消息处理handler
 * @author: Stone
 * @create: 2019-01-10 20:01
 **/
@Slf4j
@Component
@ChannelHandler.Sharable
public class ReceiverServerHandler extends ChannelInboundHandlerAdapter {

    public ReceiverServerHandler() {
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
//        log.info("[{}] : msg = {}", ctx.channel().remoteAddress(), msg);
//        msgProducerService.sendMsg(Common.ACTIVEMQ_QUEUE_NAME,(String)msg);
        log.info("[{}] : msg = {}", ctx.channel().remoteAddress(), msg);
        String msgStr = msg.toString();
        // 以固定行分隔符结尾分割,头部可能有不符合接口协议的字段, 去掉
        int idx = msgStr.indexOf("##");
        if(idx >= 0) {
            msgStr = msgStr.substring(idx);
        }
//        msgProducerService.sendMsg(Common.ACTIVEMQ_QUEUE_NAME, msgStr);

    }

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

}