org.springframework.scheduling.TaskScheduler.schedule()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(173)

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

TaskScheduler.schedule介绍

[英]Schedule the given Runnable, invoking it at the specified execution time.

Execution will end once the scheduler shuts down or the returned ScheduledFuture gets cancelled.
[中]调度给定的Runnable,在指定的执行时间调用它。
一旦调度程序关闭或返回的ScheduledFuture被取消,执行将结束。

代码示例

代码示例来源:origin: spring-projects/spring-security

@Override
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
  return taskScheduler.schedule(task, startTime);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Schedule the given {@link Runnable}, invoking it at the specified execution time.
 * <p>Execution will end once the scheduler shuts down or the returned
 * {@link ScheduledFuture} gets cancelled.
 * @param task the Runnable to execute whenever the trigger fires
 * @param startTime the desired execution time for the task
 * (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
 * @return a {@link ScheduledFuture} representing pending completion of the task
 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
 * @since 5.0
 * @see #schedule(Runnable, Date)
 */
default ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
  return schedule(task, Date.from(startTime));
}

代码示例来源:origin: spring-projects/spring-security

@Override
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
  return taskScheduler.schedule(task, trigger);
}

代码示例来源:origin: spring-projects/spring-framework

private void scheduleConnectTimeoutTask(ConnectCallback connectHandler) {
  if (this.timeoutScheduler != null) {
    if (logger.isTraceEnabled()) {
      logger.trace("Scheduling connect to time out after " + this.timeoutValue + " ms.");
    }
    Date timeoutDate = new Date(System.currentTimeMillis() + this.timeoutValue);
    this.timeoutScheduler.schedule(connectHandler, timeoutDate);
  }
  else if (logger.isTraceEnabled()) {
    logger.trace("Connect timeout task not scheduled (no TaskScheduler configured).");
  }
}

代码示例来源:origin: org.springframework/spring-context

/**
 * Schedule the given {@link Runnable}, invoking it at the specified execution time.
 * <p>Execution will end once the scheduler shuts down or the returned
 * {@link ScheduledFuture} gets cancelled.
 * @param task the Runnable to execute whenever the trigger fires
 * @param startTime the desired execution time for the task
 * (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
 * @return a {@link ScheduledFuture} representing pending completion of the task
 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
 * @since 5.0
 * @see #schedule(Runnable, Date)
 */
default ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
  return schedule(task, Date.from(startTime));
}

代码示例来源:origin: Netflix/genie

private synchronized void setDisconnected() {
  this.isConnected = false;
  // Schedule a stream reset
  this.taskScheduler.schedule(
    this::resetStreamTask,
    Instant.ofEpochMilli(System.currentTimeMillis() + STREAM_RESET_DELAY_MILLIS)
  );
}

代码示例来源:origin: spring-projects/spring-framework

private void initReceiptHandling() {
  Assert.notNull(getTaskScheduler(), "To track receipts, a TaskScheduler must be configured");
  DefaultStompSession.this.receiptHandlers.put(this.receiptId, this);
  Date startTime = new Date(System.currentTimeMillis() + getReceiptTimeLimit());
  this.future = getTaskScheduler().schedule(this::handleReceiptNotReceived, startTime);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Schedule the specified trigger task, either right away if possible
 * or on initialization of the scheduler.
 * @return a handle to the scheduled task, allowing to cancel it
 * @since 4.3
 */
@Nullable
public ScheduledTask scheduleTriggerTask(TriggerTask task) {
  ScheduledTask scheduledTask = this.unresolvedTasks.remove(task);
  boolean newTask = false;
  if (scheduledTask == null) {
    scheduledTask = new ScheduledTask(task);
    newTask = true;
  }
  if (this.taskScheduler != null) {
    scheduledTask.future = this.taskScheduler.schedule(task.getRunnable(), task.getTrigger());
  }
  else {
    addTriggerTask(task);
    this.unresolvedTasks.put(task, scheduledTask);
  }
  return (newTask ? scheduledTask : null);
}

代码示例来源:origin: spring-projects/spring-framework

protected void scheduleHeartbeat() {
  if (this.heartbeatDisabled) {
    return;
  }
  synchronized (this.responseLock) {
    cancelHeartbeat();
    if (!isActive()) {
      return;
    }
    Date time = new Date(System.currentTimeMillis() + this.config.getHeartbeatTime());
    this.heartbeatTask = new HeartbeatTask();
    this.heartbeatFuture = this.config.getTaskScheduler().schedule(this.heartbeatTask, time);
    if (logger.isTraceEnabled()) {
      logger.trace("Scheduled heartbeat in session " + getId());
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Schedule the specified cron task, either right away if possible
 * or on initialization of the scheduler.
 * @return a handle to the scheduled task, allowing to cancel it
 * (or {@code null} if processing a previously registered task)
 * @since 4.3
 */
@Nullable
public ScheduledTask scheduleCronTask(CronTask task) {
  ScheduledTask scheduledTask = this.unresolvedTasks.remove(task);
  boolean newTask = false;
  if (scheduledTask == null) {
    scheduledTask = new ScheduledTask(task);
    newTask = true;
  }
  if (this.taskScheduler != null) {
    scheduledTask.future = this.taskScheduler.schedule(task.getRunnable(), task.getTrigger());
  }
  else {
    addCronTask(task);
    this.unresolvedTasks.put(task, scheduledTask);
  }
  return (newTask ? scheduledTask : null);
}

代码示例来源:origin: org.springframework/spring-context

/**
 * Schedule the specified trigger task, either right away if possible
 * or on initialization of the scheduler.
 * @return a handle to the scheduled task, allowing to cancel it
 * @since 4.3
 */
@Nullable
public ScheduledTask scheduleTriggerTask(TriggerTask task) {
  ScheduledTask scheduledTask = this.unresolvedTasks.remove(task);
  boolean newTask = false;
  if (scheduledTask == null) {
    scheduledTask = new ScheduledTask(task);
    newTask = true;
  }
  if (this.taskScheduler != null) {
    scheduledTask.future = this.taskScheduler.schedule(task.getRunnable(), task.getTrigger());
  }
  else {
    addTriggerTask(task);
    this.unresolvedTasks.put(task, scheduledTask);
  }
  return (newTask ? scheduledTask : null);
}

代码示例来源:origin: org.springframework/spring-context

/**
 * Schedule the specified cron task, either right away if possible
 * or on initialization of the scheduler.
 * @return a handle to the scheduled task, allowing to cancel it
 * (or {@code null} if processing a previously registered task)
 * @since 4.3
 */
@Nullable
public ScheduledTask scheduleCronTask(CronTask task) {
  ScheduledTask scheduledTask = this.unresolvedTasks.remove(task);
  boolean newTask = false;
  if (scheduledTask == null) {
    scheduledTask = new ScheduledTask(task);
    newTask = true;
  }
  if (this.taskScheduler != null) {
    scheduledTask.future = this.taskScheduler.schedule(task.getRunnable(), task.getTrigger());
  }
  else {
    addCronTask(task);
    this.unresolvedTasks.put(task, scheduledTask);
  }
  return (newTask ? scheduledTask : null);
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void testSchedulerWithRunnableAndInstant() {
  Instant date = Instant.now();
  delegatingSecurityContextTaskScheduler.schedule(runnable, date);
  verify(scheduler).schedule(any(Runnable.class), any(Date.class));
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void testSchedulerWithRunnableAndTrigger() {
  delegatingSecurityContextTaskScheduler.schedule(runnable, trigger);
  verify(scheduler).schedule(any(Runnable.class), any(Trigger.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void receiptNotReceived() {
  TaskScheduler taskScheduler = mock(TaskScheduler.class);
  this.session.afterConnected(this.connection);
  this.session.setTaskScheduler(taskScheduler);
  AtomicReference<Boolean> notReceived = new AtomicReference<>();
  ScheduledFuture future = mock(ScheduledFuture.class);
  when(taskScheduler.schedule(any(Runnable.class), any(Date.class))).thenReturn(future);
  StompHeaders headers = new StompHeaders();
  headers.setDestination("/topic/foo");
  headers.setReceipt("my-receipt");
  Receiptable receiptable = this.session.send(headers, "payload");
  receiptable.addReceiptLostTask(() -> notReceived.set(true));
  ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
  verify(taskScheduler).schedule(taskCaptor.capture(), (Date) notNull());
  Runnable scheduledTask = taskCaptor.getValue();
  assertNotNull(scheduledTask);
  assertNull(notReceived.get());
  scheduledTask.run();
  assertTrue(notReceived.get());
  verify(future).cancel(true);
  verifyNoMoreInteractions(future);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void fallbackAfterTimeout() throws Exception {
  TaskScheduler scheduler = mock(TaskScheduler.class);
  Runnable sessionCleanupTask = mock(Runnable.class);
  DefaultTransportRequest request1 = createTransportRequest(this.webSocketTransport, TransportType.WEBSOCKET);
  DefaultTransportRequest request2 = createTransportRequest(this.xhrTransport, TransportType.XHR_STREAMING);
  request1.setFallbackRequest(request2);
  request1.setTimeoutScheduler(scheduler);
  request1.addTimeoutTask(sessionCleanupTask);
  request1.connect(null, this.connectFuture);
  assertTrue(this.webSocketTransport.invoked());
  assertFalse(this.xhrTransport.invoked());
  // Get and invoke the scheduled timeout task
  ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
  verify(scheduler).schedule(taskCaptor.capture(), any(Date.class));
  verifyNoMoreInteractions(scheduler);
  taskCaptor.getValue().run();
  assertTrue(this.xhrTransport.invoked());
  verify(sessionCleanupTask).run();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void sendHeartbeat() throws Exception {
  this.session.setActive(true);
  this.session.sendHeartbeat();
  assertEquals(1, this.session.getSockJsFramesWritten().size());
  assertEquals(SockJsFrame.heartbeatFrame(), this.session.getSockJsFramesWritten().get(0));
  verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
  verifyNoMoreInteractions(this.taskScheduler);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void scheduleAndCancelHeartbeat() throws Exception {
  ScheduledFuture<?> task = mock(ScheduledFuture.class);
  willReturn(task).given(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
  this.session.setActive(true);
  this.session.scheduleHeartbeat();
  verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
  verifyNoMoreInteractions(this.taskScheduler);
  given(task.isCancelled()).willReturn(false);
  given(task.cancel(false)).willReturn(true);
  this.session.cancelHeartbeat();
  verify(task).cancel(false);
  verifyNoMoreInteractions(task);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void handleRequestXhr() throws Exception {
  XhrPollingTransportHandler transportHandler = new XhrPollingTransportHandler();
  transportHandler.initialize(this.sockJsConfig);
  AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null);
  transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);
  assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString());
  assertEquals("o\n", this.servletResponse.getContentAsString());
  assertFalse("Polling request should complete after open frame", this.servletRequest.isAsyncStarted());
  verify(this.webSocketHandler).afterConnectionEstablished(session);
  resetRequestAndResponse();
  transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);
  assertTrue("Polling request should remain open", this.servletRequest.isAsyncStarted());
  verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
  resetRequestAndResponse();
  transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);
  assertFalse("Request should have been rejected", this.servletRequest.isAsyncStarted());
  assertEquals("c[2010,\"Another connection still open\"]\n", this.servletResponse.getContentAsString());
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public synchronized void start() {
  if (!this.running) {
    Assert.notNull(getTaskScheduler(), "a task scheduler is required");
    this.reaperScheduledFuture =
        getTaskScheduler()
            .schedule(this, new Date(System.currentTimeMillis() + this.reaperDelay));
    this.running = true;
  }
}

相关文章