Search in sources :

Example 1 with IdleStateAwareChannelHandler

use of org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler in project canal by alibaba.

the class ClientAuthenticationHandler method messageReceived.

public void messageReceived(final ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
    final Packet packet = Packet.parseFrom(buffer.readBytes(buffer.readableBytes()).array());
    switch(packet.getVersion()) {
        case SUPPORTED_VERSION:
        default:
            final ClientAuth clientAuth = ClientAuth.parseFrom(packet.getBody());
            // 如果存在订阅信息
            if (StringUtils.isNotEmpty(clientAuth.getDestination()) && StringUtils.isNotEmpty(clientAuth.getClientId())) {
                ClientIdentity clientIdentity = new ClientIdentity(clientAuth.getDestination(), Short.valueOf(clientAuth.getClientId()), clientAuth.getFilter());
                try {
                    MDC.put("destination", clientIdentity.getDestination());
                    embeddedServer.subscribe(clientIdentity);
                    // 设置状态数据
                    ctx.setAttachment(clientIdentity);
                    // 尝试启动,如果已经启动,忽略
                    if (!embeddedServer.isStart(clientIdentity.getDestination())) {
                        ServerRunningMonitor runningMonitor = ServerRunningMonitors.getRunningMonitor(clientIdentity.getDestination());
                        if (!runningMonitor.isStart()) {
                            runningMonitor.start();
                        }
                    }
                } finally {
                    MDC.remove("destination");
                }
            }
            NettyUtils.ack(ctx.getChannel(), new ChannelFutureListener() {

                public void operationComplete(ChannelFuture future) throws Exception {
                    logger.info("remove unused channel handlers after authentication is done successfully.");
                    ctx.getPipeline().remove(HandshakeInitializationHandler.class.getName());
                    ctx.getPipeline().remove(ClientAuthenticationHandler.class.getName());
                    int readTimeout = defaultSubscriptorDisconnectIdleTimeout;
                    int writeTimeout = defaultSubscriptorDisconnectIdleTimeout;
                    if (clientAuth.getNetReadTimeout() > 0) {
                        readTimeout = clientAuth.getNetReadTimeout();
                    }
                    if (clientAuth.getNetWriteTimeout() > 0) {
                        writeTimeout = clientAuth.getNetWriteTimeout();
                    }
                    IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer, readTimeout, writeTimeout, 0);
                    ctx.getPipeline().addBefore(SessionHandler.class.getName(), IdleStateHandler.class.getName(), idleStateHandler);
                    IdleStateAwareChannelHandler idleStateAwareChannelHandler = new IdleStateAwareChannelHandler() {

                        public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
                            logger.warn("channel:{} idle timeout exceeds, close channel to save server resources...", ctx.getChannel());
                            ctx.getChannel().close();
                        }
                    };
                    ctx.getPipeline().addBefore(SessionHandler.class.getName(), IdleStateAwareChannelHandler.class.getName(), idleStateAwareChannelHandler);
                }
            });
            break;
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) IdleStateEvent(org.jboss.netty.handler.timeout.IdleStateEvent) Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) ClientAuth(com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth) IdleStateAwareChannelHandler(org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

ServerRunningMonitor (com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor)1 ClientAuth (com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth)1 Packet (com.alibaba.otter.canal.protocol.CanalPacket.Packet)1 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)1 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1 ChannelFuture (org.jboss.netty.channel.ChannelFuture)1 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)1 IdleStateAwareChannelHandler (org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler)1 IdleStateEvent (org.jboss.netty.handler.timeout.IdleStateEvent)1 IdleStateHandler (org.jboss.netty.handler.timeout.IdleStateHandler)1