Java Code Examples for io.netty.handler.timeout.IdleStateEvent

The following are top voted examples for showing how to use io.netty.handler.timeout.IdleStateEvent. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: happylifeplat-transaction   File: NettyClientMessageHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //心跳配置
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            SpringBeanUtils.getInstance().getBean(NettyClientService.class).doConnect();
        } else if (event.state() == IdleState.WRITER_IDLE) {
            //表示已经多久没有发送数据了
            HEART_BEAT.setAction(NettyMessageActionEnum.HEART.getCode());
            ctx.writeAndFlush(HEART_BEAT);
            LogUtil.debug(LOGGER, () -> "向服务端发送的心跳");
        } else if (event.state() == IdleState.ALL_IDLE) {
            //表示已经多久既没有收到也没有发送数据了
            SpringBeanUtils.getInstance().getBean(NettyClientService.class).doConnect();
        }
    }
}
 
Example 2
Project: fastdfs-spring-boot   File: FastdfsHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    // read idle event.
    if (evt == IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT
            || evt == IdleStateEvent.READER_IDLE_STATE_EVENT) {

        if (null != operation) {
            throw new FastdfsReadTimeoutException(
                    String.format(
                            "execute %s read timeout.",
                            operation
                    )
            );
        }

        return;
    }

    // all idle event.
    if (evt == IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT
            || evt == IdleStateEvent.ALL_IDLE_STATE_EVENT) {
        throw new FastdfsTimeoutException("fastdfs channel was idle timeout.");
    }
}
 
Example 3
Project: os   File: ChatServerHandler.java   View source code 6 votes vote down vote up
/**
 * 心跳处理
 *
 * @param ctx 连接上下文
 * @param evt 状态事件
 * @throws Exception 异常
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        switch (event.state()) {
            case READER_IDLE:
                //读超时 直接关闭连接
                ctx.close();
                LOG.info("READER_IDLE 读超时");
                break;
            case WRITER_IDLE:
                //写超时 TODO 重新发送心跳包
                LOG.info("WRITER_IDLE 写超时");
                break;
            case ALL_IDLE:
                //总超时 直接关闭连接
                ctx.close();
                LOG.info("ALL_IDLE 总超时");
                break;
        }
    }
}
 
Example 4
Project: JavaQuarkBBS   File: UserAuthHandler.java   View source code 6 votes vote down vote up
/**
 * 内部链路检测
 * @param ctx
 * @param evt
 * @throws Exception
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //当通道空闲时由IdleStateHandler触发的用户事件
    if (evt instanceof IdleStateEvent){
       IdleStateEvent event = (IdleStateEvent) evt;
        // 判断Channel是否读空闲, 读空闲时移除Channel
        if (event.state().equals(IdleState.READER_IDLE)) {
            final String address = NettyUtil.parseChannelRemoteAddr(ctx.channel());
            logger.warn("Netty Server UserAuthHandler: IDLE exception :{}", address);
            manager.removeChannel(ctx.channel());
            //广播用户数量
            manager.broadMessage(QuarkChatProtocol.buildSysUserInfo(manager.getUsers()));
        }
    }
}
 
Example 5
Project: tx-lcn   File: TransactionHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //心跳配置
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            //表示已经多久没有收到数据了
            //ctx.close();
        } else if (event.state() == IdleState.WRITER_IDLE) {
            //表示已经多久没有发送数据了
            SocketUtils.sendMsg(ctx, heartJson);
            logger.info("心跳数据---" + heartJson);
        } else if (event.state() == IdleState.ALL_IDLE) {
            //表示已经多久既没有收到也没有发送数据了

        }
    }
}
 
Example 6
Project: rocketmq-rocketmq-all-4.1.0-incubating   File: NettyRemotingClient.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress, ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 7
Project: rocketmq-rocketmq-all-4.1.0-incubating   File: NettyRemotingServer.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
    	/**
         *IdleStateEvent事件,在指定时间没有进行读写,会进行回调
         */
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());  //关闭channel
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress, ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 8
Project: GoPush   File: NodeChannelInBoundHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    Channel channel = ctx.channel();
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.ALL_IDLE) {
            //发送心跳
            channel.writeAndFlush(PING);
        }
        if (event.state() == IdleState.READER_IDLE) {
            //发送心跳
            channel.writeAndFlush(PING);
        }
        if (event.state() == IdleState.WRITER_IDLE) {
            channel.writeAndFlush(PING);
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 9
Project: GoPush   File: NodeChannelInBoundHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    Channel channel = ctx.channel();
    dataCenterChannelStore.isDcChannelToSave(channel);
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.ALL_IDLE) {
            //发送心跳
            channel.writeAndFlush(PING);
        }
        if (event.state() == IdleState.READER_IDLE) {
            //发送心跳
            channel.writeAndFlush(PING);
        }
        if (event.state() == IdleState.WRITER_IDLE) {
            channel.writeAndFlush(PING);
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 10
Project: GoPush   File: DeviceChannelInboundHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            ctx.writeAndFlush(PING);
        }
        if (event.state() == IdleState.WRITER_IDLE) {
            ctx.writeAndFlush(PING);
        }
        if (event.state() == IdleState.ALL_IDLE) {
            ctx.writeAndFlush(PING);
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 11
Project: reading-and-annotate-rocketmq-3.4.6   File: NettyRemotingClient.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this.putNettyEvent(new NettyEvent(NettyEventType.IDLE,
                    remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 12
Project: reading-and-annotate-rocketmq-3.4.6   File: NettyRemotingServer.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 13
Project: ConfigCenter   File: NettyRemotingClient.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this
                        .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 14
Project: ConfigCenter   File: NettyRemotingServer.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                        .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 15
Project: HappyChat   File: UserAuthHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        // 判断Channel是否读空闲, 读空闲时移除Channel
        if (evnet.state().equals(IdleState.READER_IDLE)) {
            final String remoteAddress = NettyUtil.parseChannelRemoteAddr(ctx.channel());
            logger.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            UserInfoManager.removeChannel(ctx.channel());
            UserInfoManager.broadCastInfo(ChatCode.SYS_USER_COUNT,UserInfoManager.getAuthUserCount());
        }
    }
    ctx.fireUserEventTriggered(evt);
}
 
Example 16
Project: qonduit   File: WebSocketRequestDecoder.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent idle = (IdleStateEvent) evt;
        if (idle.state() == IdleState.READER_IDLE) {
            // We have not read any data from client in a while, let's close
            // the subscriptions for this context.
            LOG.info("Client {} is idle", ctx.channel());
        }
    } else if (evt instanceof SslCompletionEvent) {
        SslCompletionEvent ssl = (SslCompletionEvent) evt;
        if (!ssl.isSuccess()) {
            LOG.error("SSL error: {}", ssl.getClass().getSimpleName(), ssl.cause());
        }
    } else {
        LOG.warn("Received unhandled user event {}", evt);
    }
}
 
Example 17
Project: azeroth   File: FastdfsHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    // read idle event.
    if (evt == IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT
        || evt == IdleStateEvent.READER_IDLE_STATE_EVENT) {

        if (null != operation) {
            throw new FastdfsReadTimeoutException(
                String.format("execute %s read timeout.", operation));
        }

        return;
    }

    // all idle event.
    if (evt == IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT
        || evt == IdleStateEvent.ALL_IDLE_STATE_EVENT) {
        throw new FastdfsTimeoutException("fastdfs channel was idle timeout.");
    }
}
 
Example 18
Project: NioSmtpClient   File: KeepAliveHandler.java   View source code 6 votes vote down vote up
@Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
  LOG.debug("[{}] Sending NOOP to keep the connection alive", connectionId);

  if (expectingNoopResponse) {
    LOG.warn("[{}] Did not receive a response to our last NOOP, will not send another", connectionId);
    return;
  }

  Optional<String> debugString = responseHandler.getPendingResponseDebugString();
  if (debugString.isPresent()) {
    LOG.warn("[{}] Waiting for a response to [{}], will not send a NOOP to keep the connection alive", connectionId, debugString.get());
  } else {
    LOG.debug("[{}] Sending NOOP", connectionId);
    ctx.channel().writeAndFlush(new DefaultSmtpRequest(SmtpCommand.NOOP));
    expectingNoopResponse = true;
  }
}
 
Example 19
Project: sds   File: SocketHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //心跳配置
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())&& socketService.getSocketEventService().hasOpenHeartCheck()) {
        IdleStateEvent event = (IdleStateEvent) evt;
        String uniqueKey = ctx.channel().remoteAddress().toString();
        if (event.state() == IdleState.READER_IDLE) {
            //表示已经多久没有收到数据了

            socketService.getSocketEventService().onHeartNoReadDataListener(ctx,uniqueKey);

        } else if (event.state() == IdleState.WRITER_IDLE) {
            //表示已经多久没有发送数据了

            socketService.getSocketEventService().onHeartNoWriteDataListener(ctx,uniqueKey);

        } else if (event.state() == IdleState.ALL_IDLE) {
            //表示已经多久既没有收到也没有发送数据了

        }
    }
}
 
Example 20
Project: rmq4note   File: NettyRemotingClient.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 21
Project: rmq4note   File: NettyRemotingServer.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 22
Project: jeesupport   File: NettyHandler.java   View source code 6 votes vote down vote up
/**
 * 利用事件变化来做心跳检测
 */
@Override
public void userEventTriggered( ChannelHandlerContext _ctx , Object _obj ) throws Exception {
	logger.debug( _handler_info( _ctx , "userEventTriggered" ) );

	if ( _obj instanceof IdleStateEvent ) {
		IdleStateEvent event = ( IdleStateEvent ) _obj;
		if ( event.state() == IdleState.READER_IDLE ) {
			lost++;
			logger.debug( _handler_info( _ctx , " inactive with=" + lost ) );
			if ( lost > 2 ) {
				handler.stand( _ctx );
				logger.debug( _handler_info( _ctx , " was stand with=" + lost ) );
			}
		}
	} else {
		logger.warn( _handler_info( _ctx , " was discard" ) );
	}
}
 
Example 23
Project: fastdfs-client   File: FastdfsHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    // read idle event.
    if (evt == IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT
            || evt == IdleStateEvent.READER_IDLE_STATE_EVENT) {

        if (null != operation) {
            throw new FastdfsReadTimeoutException(
                    String.format(
                            "execute %s read timeout.",
                            operation
                    )
            );
        }

        return;
    }

    // all idle event.
    if (evt == IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT
            || evt == IdleStateEvent.ALL_IDLE_STATE_EVENT) {
        throw new FastdfsTimeoutException("fastdfs channel was idle timeout.");
    }
}
 
Example 24
Project: java_learn   File: ServerHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, Object evt)
		throws Exception {
	if(evt instanceof IdleStateEvent) {
		IdleStateEvent 	event = (IdleStateEvent) evt;
		if (event.state() == IdleState.READER_IDLE) {
               System.out.println("--- Reader Idle ---");
               ctx.writeAndFlush("读取等待:客户端你在吗... ...\r\n");
           } else if (event.state() == IdleState.WRITER_IDLE) {
               System.out.println("--- Write Idle ---");
               ctx.writeAndFlush("写入等待:客户端你在吗... ...\r\n");
           } else if (event.state() == IdleState.ALL_IDLE) {
               System.out.println("--- All_IDLE ---");
               ctx.writeAndFlush("全部时间:客户端你在吗... ...\r\n");
           }
	}else{
		super.userEventTriggered(ctx, evt);
	}
}
 
Example 25
Project: fastdfs-spring-boot   File: FastdfsHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    // read idle event.
    if (evt == IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT
            || evt == IdleStateEvent.READER_IDLE_STATE_EVENT) {

        if (null != operation) {
            throw new FastdfsReadTimeoutException(
                    String.format(
                            "execute %s read timeout.",
                            operation
                    )
            );
        }

        return;
    }

    // all idle event.
    if (evt == IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT
            || evt == IdleStateEvent.ALL_IDLE_STATE_EVENT) {
        throw new FastdfsTimeoutException("fastdfs channel was idle timeout.");
    }
}
 
Example 26
Project: esjc   File: HeartbeatHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        synchronized (timeoutTaskLock) {
            if (timeoutTask == null) {
                ctx.writeAndFlush(TcpPackage.newBuilder()
                    .command(TcpCommand.HeartbeatRequestCommand)
                    .correlationId(UUID.randomUUID())
                    .build());
                timeoutTask = ctx.executor().schedule(() -> {
                    logger.info("Closing TCP connection [{}, L{}] due to HEARTBEAT TIMEOUT.", ctx.channel().remoteAddress(), ctx.channel().localAddress());
                    ctx.close();
                }, timeoutMillis, MILLISECONDS);
            }
        }
    }
}
 
Example 27
Project: sailfish   File: HeartbeatChannelHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
	if (evt instanceof IdleStateEvent) {
		int maxIdleTimeout = ctx.channel().attr(ChannelAttrKeys.maxIdleTimeout).get();
		long expireTime = System.currentTimeMillis() - ctx.channel().attr(ChannelAttrKeys.lastReadTimeMillis).get();
		if (expireTime >= maxIdleTimeout * 1000) {
			logger.warn("readIdleTimeout exceed maxIdleTimeout, real timeout {}, this channel[{}] will be closed",
					expireTime, ctx.channel().toString());
			ChannelUtil.closeChannel(ctx.channel());
		} else if (ChannelUtil.clientSide(ctx)) {
			// send heart beat to remote peer
			ctx.writeAndFlush(RequestProtocol.newHeartbeat());
		}
	} else {
		ctx.fireUserEventTriggered(evt);
	}
}
 
Example 28
Project: study-netty   File: HeartbeatServerHandler.java   View source code 6 votes vote down vote up
/**
 * 判断是否是IdleStateEvent事件,是则处理
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
	if(evt instanceof IdleStateEvent) {
		IdleStateEvent event = (IdleStateEvent) evt;
		String type = "";
		if(event.state() == IdleState.READER_IDLE) 
			type = "read idle";
		else if(event.state() == IdleState.WRITER_IDLE)
			type = "write idle";
		else if(event.state() == IdleState.ALL_IDLE)
			type = "all idle";
		//
		ChannelFuture f = ctx.writeAndFlush(HEARTBEAT_SEQUENCE.duplicate());
		f.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
		
		System.out.println(ctx.channel().remoteAddress() + "超时类型 : " + type);
	} else {
		super.userEventTriggered(ctx, evt);
	}
}
 
Example 29
Project: timely   File: WebSocketRequestDecoder.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent idle = (IdleStateEvent) evt;
        if (idle.state() == IdleState.READER_IDLE) {
            // We have not read any data from client in a while, let's close
            // the subscriptions for this context.
            String subscriptionId = ctx.channel().attr(SubscriptionRegistry.SUBSCRIPTION_ID_ATTR).get();
            if (!StringUtils.isEmpty(subscriptionId)) {
                if (SubscriptionRegistry.get().containsKey(subscriptionId)) {
                    LOG.info("Closing subscription with subscription id {} due to idle event", subscriptionId);
                    SubscriptionRegistry.get().get(subscriptionId).close();
                }
            } else {
                LOG.warn("Channel idle, but no subscription id found on context. Unable to close subscriptions");
            }
        }
    } else if (evt instanceof SslCompletionEvent) {
        SslCompletionEvent ssl = (SslCompletionEvent) evt;
        if (!ssl.isSuccess()) {
            LOG.error("SSL error: {}", ssl.getClass().getSimpleName(), ssl.cause());
        }
    } else {
        LOG.warn("Received unhandled user event {}", evt);
    }
}
 
Example 30
Project: ConfigCenter   File: NettyRemotingClient.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this
                        .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 31
Project: ConfigCenter   File: NettyRemotingServer.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                        .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 32
Project: riposte   File: DownstreamIdleChannelTimeoutHandler.java   View source code 6 votes vote down vote up
@Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
    if (!disabled && shouldKillIdleChannelNowSupplier.get()) {
        String reason = (isActiveDownstreamCallTimer)
                        ? "Throwing call timeout error because the active downstream call took longer than the "
                          + "allowed timeout value."
                        : "Closing downstream channel because it was sitting unused for too long between calls.";

        AsyncNettyHelper.runnableWithTracingAndMdc(
            () -> logger.debug("{} custom_handler_id={}, idle_timeout_millis={}, worker_channel_throwing_error={}",
                               reason, customHandlerIdForLogs, idleTimeoutMillis, ctx.channel().toString()),
            traceStackForLogging,
            mdcInfoForLogging
        ).run();

        throw new DownstreamIdleChannelTimeoutException(idleTimeoutMillis, ctx.channel());
    }
}
 
Example 33
Project: riposte   File: IncompleteHttpCallTimeoutHandler.java   View source code 6 votes vote down vote up
@Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
    if (alreadyTriggeredException) {
        runnableWithTracingAndMdc(
            () -> logger.error(
                "IncompleteHttpCallTimeoutHandler triggered multiple times - this should not happen."
            ),
            ctx
        ).run();
        return;
    }

    channelIdleTriggered(ctx, evt);

    alreadyTriggeredException = true;
    throw new IncompleteHttpCallTimeoutException(idleTimeoutMillis);
}
 
Example 34
Project: netty-tutorials   File: NettyConnHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.READER_IDLE) {
            logger.info("READER_IDLE 事件触发, 关闭连接");/*读超时*/
            ctx.close();
        } else if (e.state() == IdleState.WRITER_IDLE) {
            logger.info("WRITER_IDLE 事件触发");
            ctx.writeAndFlush(new PingMessage());
        } else if (e.state() == IdleState.ALL_IDLE) {
            logger.info("ALL_IDLE 事件触发, 关闭连接");
            ctx.close();
        }
    }
}
 
Example 35
Project: jeesuite-libs   File: FastdfsHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    // read idle event.
    if (evt == IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT
            || evt == IdleStateEvent.READER_IDLE_STATE_EVENT) {

        if (null != operation) {
            throw new FastdfsReadTimeoutException(
                    String.format(
                            "execute %s read timeout.",
                            operation
                    )
            );
        }

        return;
    }

    // all idle event.
    if (evt == IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT
            || evt == IdleStateEvent.ALL_IDLE_STATE_EVENT) {
        throw new FastdfsTimeoutException("fastdfs channel was idle timeout.");
    }
}
 
Example 36
Project: rocketmq-commet   File: NettyRemotingClient.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this.putNettyEvent(new NettyEvent(NettyEventType.IDLE,
                        remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 37
Project: rocketmq-commet   File: NettyRemotingServer.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent evnet = (IdleStateEvent) evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);

            RemotingUtil.closeChannel(ctx.channel());

            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                        .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 38
Project: coco   File: HeartBeatHandler.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    super.userEventTriggered(ctx, evt);
    // 拦截链路空闲事件并处理心跳
    if (evt instanceof IdleStateEvent) {
        // 心跳处理
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.READER_IDLE)) {
            // 未进行读操作, 服务器端主动关闭连接
            LOGGER.warn("READER_IDLE,client maybe not exist,we will close the chnnel");
            if (ctx.channel().isOpen()) {
                ctx.close();
            }
            ctx.close();
        } else if (event.state().equals(IdleState.WRITER_IDLE)) {
            // 未进行写操作, nothing todo
            // LOGGER.info("WRITER_IDLE, long time not write something to client");
        } else if (event.state().equals(IdleState.ALL_IDLE)) {
            // 未进行读写
            // LOGGER.info("ALL_IDLE, long time not to write or read");
            // 发送心跳消息
            // MsgHandleService.getInstance().sendMsgUtil.sendHeartMessage(ctx);
        }
    }
}
 
Example 39
Project: light-task-scheduler   File: NettyRemotingClient.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;

        Channel channel = new NettyChannel(ctx);

        final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel);

        if (event.state().equals(io.netty.handler.timeout.IdleState.ALL_IDLE)) {
            LOGGER.warn("CLIENT : IDLE [{}]", remoteAddress);
            closeChannel(channel);
        }

        if (channelEventListener != null) {
            RemotingEventType remotingEventType = RemotingEventType.valueOf(event.state().name());
            putRemotingEvent(new RemotingEvent(remotingEventType,
                    remoteAddress, channel));
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 40
Project: light-task-scheduler   File: NettyRemotingServer.java   View source code 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;

        com.lts.remoting.Channel channel = new NettyChannel(ctx);

        final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel);

        if (event.state().equals(IdleState.ALL_IDLE)) {
            LOGGER.warn("SERVER: IDLE [{}]", remoteAddress);
            RemotingHelper.closeChannel(channel);
        }

        if (channelEventListener != null) {
            RemotingEventType remotingEventType = RemotingEventType.valueOf(event.state().name());
            putRemotingEvent(new RemotingEvent(remotingEventType,
                    remoteAddress, channel));
        }
    }

    ctx.fireUserEventTriggered(evt);
}