io.netty.util.concurrent.ScheduledFuture类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(161)

本文整理了Java中io.netty.util.concurrent.ScheduledFuture类的一些代码示例,展示了ScheduledFuture类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ScheduledFuture类的具体详情如下:
包路径:io.netty.util.concurrent.ScheduledFuture
类名称:ScheduledFuture

ScheduledFuture介绍

[英]The result of an scheduled asynchronous operation.
[中]计划的异步操作的结果。

代码示例

代码示例来源:origin: redisson/redisson

public void stop() {
  if (monitorFuture != null) {
    monitorFuture.cancel(true);
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  if (in.readableBytes() < 1) {
    return;
  }
  // read one byte to guess protocol
  final int magic = in.getByte(in.readerIndex());
  ChannelPipeline p = ctx.pipeline();
  p.addLast(new LocalHostPermitHandler(acceptForeignIp));
  if (isHttp(magic)) {
    // no welcome output for http protocol
    if (welcomeFuture != null && welcomeFuture.isCancellable()) {
      welcomeFuture.cancel(false);
    }
    p.addLast(new HttpServerCodec());
    p.addLast(new HttpObjectAggregator(1048576));
    p.addLast(new HttpProcessHandler());
    p.remove(this);
  } else {
    p.addLast(new LineBasedFrameDecoder(2048));
    p.addLast(new StringDecoder(CharsetUtil.UTF_8));
    p.addLast(new StringEncoder(CharsetUtil.UTF_8));
    p.addLast(new IdleStateHandler(0, 0, 5 * 60));
    p.addLast(new TelnetProcessHandler());
    p.remove(this);
  }
}

代码示例来源:origin: yu199195/Raincat

if (!schedule.isDone()) {
  schedule.cancel(false);

代码示例来源:origin: apache/pulsar

if (keyGeneratorTask != null && !keyGeneratorTask.isCancelled()) {
  keyGeneratorTask.cancel(false);

代码示例来源:origin: com.github.msemys/esjc

private boolean isRunning() {
  return timer != null && !timer.isDone();
}

代码示例来源:origin: org.ow2.petals/petals-bc-gateway

if (polling != null && !polling.isCancelled()) {
  scheduleNextPolling(nextDelay, accel, maxDelay);

代码示例来源:origin: com.nike.riposte/riposte-core

@Override
public PipelineContinuationBehavior doChannelActive(ChannelHandlerContext ctx) throws Exception {
  // New channel opening. See if we have too many open channels.
  int actualOpenChannelsCount = openChannelsGroup.size();
  if (actualOpenChannelsCount >= maxOpenChannelsThreshold) {
    Channel channel = ctx.channel();
    // Mark this channel as needing to be closed.
    ctx.channel().attr(TOO_MANY_OPEN_CONNECTIONS_THIS_CHANNEL_SHOULD_CLOSE).set(actualOpenChannelsCount);
    // Schedule a double-check event to make sure the channel gets closed.
    ScheduledFuture doubleCheckScheduledFuture = ctx.channel().eventLoop().schedule(() -> {
      if (channel.isOpen())
        channel.close();
    }, 100, TimeUnit.MILLISECONDS);
    // Add a channel close future listener to cancel the double-check scheduled event immediately if the channel
    //      is closed quickly. Even though the double-check event will execute in 100 milliseconds that's 100
    //      milliseconds of potential garbage accumulating when it shouldn't. Could be a lot for a high traffic
    //      server (which this likely is if the open channels limit is being hit).
    channel.closeFuture().addListener(future -> {
      if (!doubleCheckScheduledFuture.isDone())
        doubleCheckScheduledFuture.cancel(false);
    });
  }
  else {
    // Not at the threshold. Add this channel to the open channel group.
    openChannelsGroup.add(ctx.channel());
  }
  return PipelineContinuationBehavior.CONTINUE;
}

代码示例来源:origin: Cool-Coding/remote-desktop-control

private void check() throws IOException{
  if(isUnderControlled()){
    if(tasks.get(heartBeatTask)!=null && !tasks.get(heartBeatTask).isCancelled()){
      tasks.get(heartBeatTask).cancel(true);
    }
    if (tasks.get(screenSnapShotTask)==null || tasks.get(screenSnapShotTask).isCancelled()){
      tasks.put(screenSnapShotTask,startScreenSnapShotTask());
    }
  }else{
    if(tasks.get(screenSnapShotTask)!=null && !tasks.get(screenSnapShotTask).isCancelled()){
      tasks.get(screenSnapShotTask).cancel(true);
    }
    if (tasks.get(heartBeatTask)==null || tasks.get(heartBeatTask).isCancelled()){
      tasks.put(heartBeatTask,startHeartBeatTask());
    }
  }
}

代码示例来源:origin: msemys/esjc

private boolean isRunning() {
  return timer != null && !timer.isDone();
}

代码示例来源:origin: redisson/redisson

public void stop() {
  if (dnsMonitorFuture != null) {
    dnsMonitorFuture.cancel(true);
  }
}

代码示例来源:origin: Nike-Inc/riposte

@Override
public PipelineContinuationBehavior doChannelActive(ChannelHandlerContext ctx) throws Exception {
  // New channel opening. See if we have too many open channels.
  int actualOpenChannelsCount = openChannelsGroup.size();
  if (actualOpenChannelsCount >= maxOpenChannelsThreshold) {
    Channel channel = ctx.channel();
    // Mark this channel as needing to be closed.
    ctx.channel().attr(TOO_MANY_OPEN_CONNECTIONS_THIS_CHANNEL_SHOULD_CLOSE).set(actualOpenChannelsCount);
    // Schedule a double-check event to make sure the channel gets closed.
    ScheduledFuture doubleCheckScheduledFuture = ctx.channel().eventLoop().schedule(() -> {
      if (channel.isOpen())
        channel.close();
    }, 100, TimeUnit.MILLISECONDS);
    // Add a channel close future listener to cancel the double-check scheduled event immediately if the channel
    //      is closed quickly. Even though the double-check event will execute in 100 milliseconds that's 100
    //      milliseconds of potential garbage accumulating when it shouldn't. Could be a lot for a high traffic
    //      server (which this likely is if the open channels limit is being hit).
    channel.closeFuture().addListener(future -> {
      if (!doubleCheckScheduledFuture.isDone())
        doubleCheckScheduledFuture.cancel(false);
    });
  }
  else {
    // Not at the threshold. Add this channel to the open channel group.
    openChannelsGroup.add(ctx.channel());
  }
  return PipelineContinuationBehavior.CONTINUE;
}

代码示例来源:origin: NationalSecurityAgency/timely

@Override
  public void onError(Session session, Throwable t) {
    super.onError(session, t);
    LOG.error(t.getMessage(), t);
    Exception e = new TimelyException(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), t.getMessage(), "");
    WsRelayHandler.sendErrorResponse(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, e);
    if (!ping.isCancelled()) {
      ping.cancel(false);
    }
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  if (in.readableBytes() < 1) {
    return;
  }
  // read one byte to guess protocol
  final int magic = in.getByte(in.readerIndex());
  ChannelPipeline p = ctx.pipeline();
  p.addLast(new LocalHostPermitHandler(acceptForeignIp));
  if (isHttp(magic)) {
    // no welcome output for http protocol
    if (welcomeFuture != null && welcomeFuture.isCancellable()) {
      welcomeFuture.cancel(false);
    }
    p.addLast(new HttpServerCodec());
    p.addLast(new HttpObjectAggregator(1048576));
    p.addLast(new HttpProcessHandler());
    p.remove(this);
  } else {
    p.addLast(new LineBasedFrameDecoder(2048));
    p.addLast(new StringDecoder(CharsetUtil.UTF_8));
    p.addLast(new StringEncoder(CharsetUtil.UTF_8));
    p.addLast(new IdleStateHandler(0, 0, 5 * 60));
    p.addLast(new TelnetProcessHandler());
    p.remove(this);
  }
}

代码示例来源:origin: Nike-Inc/riposte

@Before
public void beforeMethod() {
  channelMock = mock(Channel.class);
  ctxMock = mock(ChannelHandlerContext.class);
  tooManyOpenConnectionsAttributeMock = mock(Attribute.class);
  doReturn(channelMock).when(ctxMock).channel();
  doReturn(tooManyOpenConnectionsAttributeMock).when(channelMock)
                         .attr(TOO_MANY_OPEN_CONNECTIONS_THIS_CHANNEL_SHOULD_CLOSE);
  doReturn(true).when(channelMock).isOpen();
  eventLoopMock = mock(EventLoop.class);
  closeFutureMock = mock(ChannelFuture.class);
  doReturn(eventLoopMock).when(channelMock).eventLoop();
  doReturn(closeFutureMock).when(channelMock).closeFuture();
  doubleCheckScheduledFutureMock = mock(ScheduledFuture.class);
  doubleCheckRunnableCaptor = ArgumentCaptor.forClass(Runnable.class);
  closeFutureListenerCaptor = ArgumentCaptor.forClass(GenericFutureListener.class);
  doReturn(doubleCheckScheduledFutureMock).when(eventLoopMock)
                      .schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
  doReturn(false).when(doubleCheckScheduledFutureMock).isDone();
  channelGroupMock = mock(ChannelGroup.class);
  maxOpenChannelsThreshold = 42;
  handler = new OpenChannelLimitHandler(channelGroupMock, maxOpenChannelsThreshold);
}

代码示例来源:origin: redisson/redisson

@Override
  public void operationComplete(Future<R> future) throws Exception {
    scheduledFuture.cancel(false);
  }
});

代码示例来源:origin: Nike-Inc/riposte

private void verifyCloseFutureListener(GenericFutureListener closeFutureListener) throws Exception {
  // If the double-check ScheduledFuture is not done then it cancels it.
  reset(doubleCheckScheduledFutureMock);
  doReturn(false).when(doubleCheckScheduledFutureMock).isDone();
  closeFutureListener.operationComplete(null);
  verify(doubleCheckScheduledFutureMock).isDone();
  verify(doubleCheckScheduledFutureMock).cancel(false);
  verifyNoMoreInteractions(doubleCheckScheduledFutureMock);
  // And when the double-check ScheduledFuture is done, then nothing happens.
  reset(doubleCheckScheduledFutureMock);
  doReturn(true).when(doubleCheckScheduledFutureMock).isDone();
  closeFutureListener.operationComplete(null);
  verify(doubleCheckScheduledFutureMock).isDone();
  verifyNoMoreInteractions(doubleCheckScheduledFutureMock);
}

代码示例来源:origin: NationalSecurityAgency/timely

@Override
public void onClose(Session session, CloseReason reason) {
  super.onClose(session, reason);
  if (!reason.getCloseCode().equals(CloseReason.CloseCodes.NORMAL_CLOSURE)) {
    LOG.error("Abnormal close: " + reason.getReasonPhrase());
    Exception e = new TimelyException(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), reason.getReasonPhrase(),
        "");
    WsRelayHandler.sendErrorResponse(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, e);
  }
  if (!ping.isCancelled()) {
    ping.cancel(false);
  }
}

代码示例来源:origin: org.apache.dubbo/dubbo

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  if (in.readableBytes() < 1) {
    return;
  }
  // read one byte to guess protocol
  final int magic = in.getByte(in.readerIndex());
  ChannelPipeline p = ctx.pipeline();
  p.addLast(new LocalHostPermitHandler(acceptForeignIp));
  if (isHttp(magic)) {
    // no welcome output for http protocol
    if (welcomeFuture != null && welcomeFuture.isCancellable()) {
      welcomeFuture.cancel(false);
    }
    p.addLast(new HttpServerCodec());
    p.addLast(new HttpObjectAggregator(1048576));
    p.addLast(new HttpProcessHandler());
    p.remove(this);
  } else {
    p.addLast(new LineBasedFrameDecoder(2048));
    p.addLast(new StringDecoder(CharsetUtil.UTF_8));
    p.addLast(new StringEncoder(CharsetUtil.UTF_8));
    p.addLast(new IdleStateHandler(0, 0, 5 * 60));
    p.addLast(new TelnetProcessHandler());
    p.remove(this);
  }
}

代码示例来源:origin: redisson/redisson

public void stop() {
  if (dnsMonitorFuture != null) {
    dnsMonitorFuture.cancel(true);
  }
}

代码示例来源:origin: Nike-Inc/riposte

@Test
public void doChannelRead_cancels_timeout_check_if_response_finishes_before_timeout_check_occurs() throws Exception {
  // given
  ScheduledFuture timeoutCheckMock = mock(ScheduledFuture.class);
  doReturn(timeoutCheckMock).when(eventLoopMock).schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class));
  handlerSpy.doChannelRead(ctxMock, msg);
  ArgumentCaptor<BiConsumer> timeoutCheckCancellationLogicArgumentCaptor = ArgumentCaptor.forClass(BiConsumer.class);
  // The 2nd whenComplete is for cancelling the timeout check if the response finishes before the timeout
  verify(futureThatWillBeAttachedToSpy, times(2)).whenComplete(timeoutCheckCancellationLogicArgumentCaptor.capture());
  BiConsumer<ResponseInfo<?>, Throwable> timeoutCheckCancellationLogic = timeoutCheckCancellationLogicArgumentCaptor.getAllValues().get(1);
  // when: the timeout check scheduled future is not yet complete when the response finishes
  doReturn(false).when(timeoutCheckMock).isDone();
  timeoutCheckCancellationLogic.accept(mock(ResponseInfo.class), null);
  // then: timeout check scheduled future should be cancelled
  verify(timeoutCheckMock).cancel(false);
}

相关文章

微信公众号

最新文章

更多