Могу ли я очистить значение карты в NetHy ChannelHandler с помощью периодической задачи таймера? - PullRequest
0 голосов
/ 25 декабря 2018

Я определяю задачу расписания в главном потоке, хочу очистить значение карты, которая кэширует информацию о клиенте моего проекта в ChannelHandler.Но это не работает.Что я сделал не так?

Это основной код приложения, в котором я планирую задачу.

public class Server {  

    public static void main(String[] args) throws Exception {

        EventLoopGroup boss = new NioEventLoopGroup();

        EventLoopGroup work = new NioEventLoopGroup();

        final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);

        ServerBootstrap b = new ServerBootstrap();

        //init() code Omitted

         ScheduledFuture<?> sf = ctx.executor().scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {

                HashSet<String> clients = new HashSet<>();
                Map<String,String> map = LoginAuthRespHandler.getNodeCheck();
                System.out.println(map.size());

                for (String key:map.keySet()) {

                    clients.add(map.get(key));
                }

                try{

                    //doSomething();

                }catch (Exception e){

                    e.printStackTrace();
                }

                map.clear();
                clients.clear();
            }
        },10,10,TimeUnit.SECONDS);

        ChannelFuture cf = b.bind(NettyConstant.REMOTEIP,NettyConstant.PORT).sync();

        System.out.println("Netty server start ok on: "
                + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT));

        cf.channel().closeFuture().sync();
        work.shutdownGracefully();
        boss.shutdownGracefully();
    }
}

А это код ChannelHandler.

public class LoginAuthRespHandler extends ChannelInboundHandlerAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginAuthRespHandler.class);

    private static  Map<String, String> nodeCheck = new ConcurrentHashMap<String, String>();

    private String[] whiteList = { "127.0.0.1", "192.168.56.1" };

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        AlarmMessage message = (AlarmMessage) msg;

        if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_REQ.value()) {
            String nodeIndex = ctx.channel().remoteAddress().toString();
            AlarmMessage loginResp = null;
            if (nodeCheck.containsKey(nodeIndex)) {
                loginResp = buildResponse(ResultType.FAIL);
            } else {
                InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
                String ip = address.getAddress().getHostAddress();
                boolean isOK = false;
                for (String WIP : whiteList) {
                    if (WIP.equals(ip)) {
                        isOK = true;
                        break;
                    }
                }
                loginResp = isOK ? buildResponse(ResultType.SUCCESS) : buildResponse(ResultType.FAIL);
                if (isOK)
                    //add a client value to  the map
                    nodeCheck.put(nodeIndex, message.getBody().toString());
            }
            ctx.writeAndFlush(loginResp);
        } else {
            ctx.fireChannelRead(msg);
        }
    }
    private AlarmMessage buildResponse(ResultType result) {
        AlarmMessage message = new AlarmMessage();
        Header header = new Header();
        header.setType(MessageType.LOGIN_RESP.value());
        message.setHeader(header);
        message.setBody(result.value());
        return message;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        String nodeIndex = ctx.channel().remoteAddress().toString();
        ctx.close();

        if(nodeCheck.containsKey(nodeIndex)){

           nodeCheck.remove(nodeIndex);

        }
    }

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        //nodeCheck.remove(ctx.channel().remoteAddress().toString());
        ctx.close();
        ctx.fireExceptionCaught(cause);
    }

    public synchronized static Map<String, String> getNodeCheck() {
        return nodeCheck;
    }
}
...