java - 无法删除 Camel Quartz 路线

标签 java apache-camel quartz-scheduler

我设置了这样的端点:

from("quartz://GroupName/TimerName?stateful=true&cron=0 0/1 * * * ?")
.to("someProcessorBean");

为了确保没有静态端点,我这样做:

for (Endpoint endpoint : camelContext.getEndpoints()) {
  camelContext.removeEndpoint(endpoint);
}

当尝试以编程方式删除路线时,我正在执行以下操作:

camelContext.stop();
camelContext.removeRoute(routeId);

但是,此处没有返回端点。

即使执行此操作后,作业仍会继续按计划运行。删除路线时如何取消安排?

我使用的是camel-quartz 2.22.2和quartz 1.8.6

以下是 Context 关闭和启动的日志。

关闭端点

12:22:13,008 DEBUG [DefaultManagementAgent] Unregistered MBean with ObjectName: org.apache.camel:context=CamelContext-MyContext_6.1.1-SNAPSHOT,type=endpoints,name="quartz://MY_GROUP/MY_NAME\?cron=0+0%2F1+\*+\*+\*+%3F&stateful=true"
12:22:13,008 INFO  [SpringCamelContext] Apache Camel 2.22.2 (CamelContext: CamelContext-MyContext_6.1.1-SNAPSHOT) is shutting down
12:22:13,008 INFO  [DefaultShutdownStrategy] Starting to graceful shutdown 1 routes (timeout 300 seconds)

停靠路线

12:22:13,033 INFO  [DefaultShutdownStrategy] Starting to graceful shutdown 1 routes (timeout 300 seconds)
12:22:13,033 DEBUG [DefaultExecutorServiceManager] Created new ThreadPool for source: org.apache.camel.impl.DefaultShutdownStrategy@3a44287c with name: ShutdownTask. -> org.apache.camel.util.concurrent.RejectableThreadPoolExecutor@3fc7873f[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0][ShutdownTask]
12:22:13,034 DEBUG [DefaultShutdownStrategy] There are 1 routes to shutdown
12:22:13,034 INFO  [DefaultShutdownStrategy] Graceful shutdown of 1 routes completed in 0 seconds
12:22:13,034 INFO  [SpringCamelContext] Route: MY_CATEGORY.MY_NAME is stopped, was consuming from: quartz://MY_GROUP/MY_NAME?cron=0+0%2F1+*+*+*+%3F&stateful=true
12:22:13,034 DEBUG [RouteService] Shutting down services on route: MY_CATEGORY.MY_NAME
12:22:13,034 DEBUG [RouteService] Shutting down child service on route: MY_CATEGORY.MY_NAME -> FatalFallbackErrorHandler[Channel[DelegateSync[com.crlcorp.oasis.routing.utils.LauncherErrorHandler@7ccfd046]]]
12:22:13,034 DEBUG [RouteService] Shutting down child service on route: MY_CATEGORY.MY_NAME -> DelegateSync[com.crlcorp.oasis.routing.utils.LauncherErrorHandler@7ccfd046]
12:22:13,034 DEBUG [RouteService] Shutting down child service on route: MY_CATEGORY.MY_NAME -> Channel[DelegateSync[com.crlcorp.oasis.routing.utils.LauncherErrorHandler@7ccfd046]]
12:22:13,034 INFO  [SpringCamelContext] Route: MY_CATEGORY.MY_NAME is shutdown and removed, was consuming from: quartz://MY_GROUP/MY_NAME?cron=0+0%2F1+*+*+*+%3F&stateful=true

以“无路由”启动上下文

12:22:13,078 INFO  [QuartzScheduler] Scheduler meta-data: Quartz Scheduler (v1.8.6) 'DefaultQuartzScheduler-CamelContext-MyContext_6.1.1-SNAPSHOT' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

12:22:13,078 INFO  [StdSchedulerFactory] Quartz scheduler 'DefaultQuartzScheduler-CamelContext-MyContext_6.1.1-SNAPSHOT' initialized from an externally provided properties instance.
12:22:13,078 INFO  [StdSchedulerFactory] Quartz scheduler version: 1.8.6
12:22:13,078 DEBUG [QuartzComponent] Using SchedulerFactory org.quartz.impl.StdSchedulerFactory@5efaa50e to get/create Scheduler org.quartz.impl.StdScheduler@6d39162c(0x6d39162c)
12:22:13,078 INFO  [QuartzComponent] Starting Quartz scheduler: DefaultQuartzScheduler-CamelContext-MyContext_6.1.1-SNAPSHOT
12:22:13,078 INFO  [QuartzScheduler] Scheduler DefaultQuartzScheduler-CamelContext-MyContext_6.1.1-SNAPSHOT_$_NON_CLUSTERED started.
12:22:13,078 DEBUG [DefaultManagementLifecycleStrategy] Load performance statistics disabled
12:22:13,078 INFO  [SpringCamelContext] Total 0 routes, of which 0 are started
12:22:13,078 INFO  [SpringCamelContext] Apache Camel 2.22.2 (CamelContext: CamelContext-MyContext_6.1.1-SNAPSHOT) started in 0.036 seconds

quartz 作业起死回生

12:22:13,507 DEBUG [SimpleThreadPool] WorkerThread is shut down.
12:23:00,002 DEBUG [SimpleJobFactory] Producing instance of Job 'DEFAULT.quartz-endpoint1', class=org.apache.camel.component.quartz.StatefulCamelJob
12:23:00,002 DEBUG [JobRunShell] Calling execute on job DEFAULT.quartz-endpoint1
12:23:00,002 DEBUG [CamelJob] Looking up existing QuartzEndpoint with trigger MY_NAME.MY_GROUP
12:23:00,002 DEBUG [QuartzEndpoint] Firing Quartz Job with context: JobExecutionContext: trigger: 'MY_GROUP.MY_NAME job: DEFAULT.quartz-endpoint1 fireTime: 'Mon Jan 07 12:23:00 CST 2019 scheduledFireTime: Mon Jan 07 12:23:00 CST 2019 previousFireTime: 'Mon Jan 07 12:22:00 CST 2019 nextFireTime: Mon Jan 07 12:24:00 CST 2019 isRecovering: false refireCount: 0

最佳答案

选项 1

升级到camel-quartz2而不是camel-quartz。

选项 2

该问题与 Quartz 组件创建两个调度程序有关,并且在停止路由时仅关闭一个调度程序。这可能是由于路由是有状态的。为了解决这个问题,您可以手动访问与该组件相关的所有调度程序并停止所有作业。

log.info("Removing existing endpoints");
for (Endpoint endpoint : camelContext.getEndpoints()) {
  log.info("Removing: " + endpoint.getEndpointUri());
    
  if (endpoint instanceof QuartzEndpoint) {
    QuartzEndpoint quartzEndpoint = (QuartzEndpoint) endpoint;
    
    SchedulerFactory factory = quartzEndpoint.getComponent().getFactory();
    log.debug("Found factory " + factory);
    
    @SuppressWarnings("unchecked")
    Collection<Scheduler> allSchedulers = factory.getAllSchedulers();
    for (Scheduler scheduler : allSchedulers) {
      log.debug("Found Quartz Scheduler" + scheduler.getSchedulerName());
      for (String groupName : scheduler.getJobGroupNames()) {
        for (String jobName : scheduler.getJobNames(groupName)) {
          log.debug(String.format("Unscheduling job %s.%s", groupName, jobName));
          scheduler.deleteJob(jobName, groupName);
        }
      }
    }
  }
  camelContext.removeEndpoint(endpoint);
}
    
log.info("Stopping Camel Context");
camelContext.stop();
    
log.info("Removing existing routes");
RoutesDefinition routes = getRouteCollection();
routes.getOnExceptions().clear();
for (RouteDefinition route : routes.getRoutes()) {
  String routeId = route.getId();
  try {
    log.info("Removing: " + routeId);
    camelContext.removeRoute(routeId);
  } catch (Exception e) {
    log.warn("Could not stop route" + routeId + ": " + e.getMessage());
  }
}
routes.getRoutes().clear();

关于java - 无法删除 Camel Quartz 路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54077730/

相关文章:

java - Nashorn 不再使用 BigDecimal

java - 使用 Jukito 测试时 GWT 计时器 UnsatisfiedLinkError

java - Apache Camel 邮件错误

java - Camel Spring DSL 在处理完目录中的所有文件后调用 bean 上的方法

apache-camel - Camel 路由在 ServiceMix 中部署时不会记录

spring - 将 Quartz 与 Spring Boot 结合使用 - 注入(inject)顺序根据方法的返回类型而变化

java - Android 应用程序客户端与 java 服务器的相互 TLS

java - 创建一个文件

grails - 通过Grails 1.3.7中的 quartz 插件0.4.2停止cron作业

java - Spring Quartz Scheduler 竞争条件