Search code examples
springapacheapache-camelschedulerquartz

Apache Camel Quartz2


I am trying to implement a scheduler using Apache Camel Quartz2 which executes the routes every minute and does some task as expected. I'm using spring DSL to achieve the routing associated with apache camel as below :

<camelContext
        xmlns="http://camel.apache.org/schema/spring">
        <propertyPlaceholder id="properties"
            location="classpath:application.properties" />
        <route>
            <from uri="quartz2://spring?cron=0+*+*+?+*+*" />
            <multicast parallelProcessing="true">
                <to uri="direct:DomainsWithFTPUsers" />
                <to uri="direct:DomainsWithoutFTPUsers" />
            </multicast>

The scheduler when executed gives away the right time i.e. it is scheduled for next minute as per logs, however as I expect it to behave like a cronjob on Linux system, the application is exiting without continuing to work as a scheduler and does not even execute the multicasted routes further at all.

Please do correct me if my understanding of the Quartz scheduler is incorrect for WINDOWS OS as i'm trying to execute it first on my windows system.

Below are the logs of apache camel execution for reference:

14:33:50.725 [main] INFO  org.springframework.context.support.ClassPathXmlApplicationContext  - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a46a697: startup date [Wed Feb 13 14:33:50 IST 2019]; root of context hierarchy
14:33:50.788 [main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [applicationContext.xml]
14:33:52.523 [main] INFO  org.springframework.jdbc.datasource.DriverManagerDataSource  - Loaded JDBC driver: org.h2.Driver
14:33:52.842 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Apache Camel 2.15.1 (CamelContext: camel-1) is starting
14:33:52.842 [main] INFO  org.apache.camel.management.ManagedManagementStrategy  - JMX is enabled
14:33:52.998 [main] INFO  org.apache.camel.impl.converter.DefaultTypeConverter  - Loaded 183 type converters
14:33:53.076 [main] INFO  org.apache.camel.component.quartz2.QuartzComponent  - Create and initializing scheduler.
14:33:53.076 [main] INFO  org.apache.camel.component.quartz2.QuartzComponent  - Setting org.quartz.scheduler.jmx.export=true to ensure QuartzScheduler(s) will be enlisted in JMX.
14:33:53.108 [main] INFO  org.quartz.impl.StdSchedulerFactory  - Using default implementation for ThreadExecutor
14:33:53.123 [main] INFO  org.quartz.simpl.SimpleThreadPool  - Job execution threads will use class loader of thread: main
14:33:53.139 [main] INFO  org.quartz.core.SchedulerSignalerImpl  - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
14:33:53.139 [main] INFO  org.quartz.core.QuartzScheduler  - Quartz Scheduler v.2.2.1 created.
14:33:53.139 [main] INFO  org.quartz.simpl.RAMJobStore  - RAMJobStore initialized.
14:33:53.139 [main] INFO  org.quartz.core.QuartzScheduler  - Scheduler meta-data: Quartz Scheduler (v2.2.1) 'DefaultQuartzScheduler-camel-1' 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.

14:33:53.139 [main] INFO  org.quartz.impl.StdSchedulerFactory  - Quartz scheduler 'DefaultQuartzScheduler-camel-1' initialized from an externally provided properties instance.
14:33:53.139 [main] INFO  org.quartz.impl.StdSchedulerFactory  - Quartz scheduler version: 2.2.1
14:33:53.186 [main] INFO  org.apache.camel.component.quartz2.QuartzEndpoint  - Job Camel_camel-1.spring (triggerType=CronTriggerImpl, jobClass=CamelJob) is scheduled. Next fire date is Wed Feb 13 14:34:00 IST 2019
14:33:53.405 [main] INFO  org.apache.camel.spring.SpringCamelContext  - AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
14:33:53.405 [main] INFO  org.apache.camel.spring.SpringCamelContext  - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
14:33:53.521 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Route: route1 started and consuming from: Endpoint[quartz2://spring?cron=0+*+*+%3F+*+*]
14:33:53.524 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Route: route2 started and consuming from: Endpoint[direct://DomainsWithFTPUsers]
14:33:53.528 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Route: route3 started and consuming from: Endpoint[direct://DomainsWithoutFTPUsers]
14:33:53.532 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Route: route4 started and consuming from: Endpoint[direct://scpToDBServer]
14:33:53.533 [main] INFO  org.apache.camel.component.quartz2.QuartzComponent  - Starting scheduler.
14:33:53.533 [main] INFO  org.quartz.core.QuartzScheduler  - Scheduler DefaultQuartzScheduler-camel-1_$_NON_CLUSTERED started.
14:33:53.533 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Total 4 routes, of which 4 is started.
14:33:53.535 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Apache Camel 2.15.1 (CamelContext: camel-1) started in 0.691 seconds
14:33:53.535 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Apache Camel 2.15.1 (CamelContext: camel-1) is starting
14:33:53.535 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Total 4 routes, of which 4 is started.
14:33:53.535 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Apache Camel 2.15.1 (CamelContext: camel-1) started in 0.000 seconds
14:33:56.538 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Apache Camel 2.15.1 (CamelContext: camel-1) is shutting down
14:33:56.540 [main] INFO  org.apache.camel.impl.DefaultShutdownStrategy  - Starting to graceful shutdown 4 routes (timeout 300 seconds)
14:33:56.540 [Camel (camel-1) thread #0 - ShutdownTask] INFO  org.apache.camel.component.quartz2.QuartzEndpoint  - Pausing trigger Camel_camel-1.spring
14:33:56.556 [Camel (camel-1) thread #0 - ShutdownTask] INFO  org.apache.camel.impl.DefaultShutdownStrategy  - Route: route4 shutdown complete, was consuming from: Endpoint[direct://scpToDBServer]
14:33:56.556 [Camel (camel-1) thread #0 - ShutdownTask] INFO  org.apache.camel.impl.DefaultShutdownStrategy  - Route: route3 shutdown complete, was consuming from: Endpoint[direct://DomainsWithoutFTPUsers]
14:33:56.556 [Camel (camel-1) thread #0 - ShutdownTask] INFO  org.apache.camel.impl.DefaultShutdownStrategy  - Route: route2 shutdown complete, was consuming from: Endpoint[direct://DomainsWithFTPUsers]
14:33:56.556 [Camel (camel-1) thread #0 - ShutdownTask] INFO  org.apache.camel.impl.DefaultShutdownStrategy  - Route: route1 shutdown complete, was consuming from: Endpoint[quartz2://spring?cron=0+*+*+%3F+*+*]
14:33:56.556 [main] INFO  org.apache.camel.impl.DefaultShutdownStrategy  - Graceful shutdown of 4 routes completed in 0 seconds
14:33:56.571 [main] INFO  org.apache.camel.component.quartz2.QuartzEndpoint  - Deleting job Camel_camel-1.spring
14:33:56.571 [main] INFO  org.apache.camel.component.quartz2.QuartzComponent  - Shutting down scheduler. (will wait for all jobs to complete first.)
14:33:56.571 [main] INFO  org.quartz.core.QuartzScheduler  - Scheduler DefaultQuartzScheduler-camel-1_$_NON_CLUSTERED shutting down.
14:33:56.571 [main] INFO  org.quartz.core.QuartzScheduler  - Scheduler DefaultQuartzScheduler-camel-1_$_NON_CLUSTERED paused.
14:33:56.681 [main] INFO  org.quartz.core.QuartzScheduler  - Scheduler unregistered from name 'quartz:type=QuartzScheduler,name=DefaultQuartzScheduler-camel-1,instance=NON_CLUSTERED' in the local MBeanServer.
14:33:56.681 [main] INFO  org.quartz.core.QuartzScheduler  - Scheduler DefaultQuartzScheduler-camel-1_$_NON_CLUSTERED shutdown complete.
14:33:56.681 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Apache Camel 2.15.1 (CamelContext: camel-1) uptime 3.146 seconds
14:33:56.681 [main] INFO  org.apache.camel.spring.SpringCamelContext  - Apache Camel 2.15.1 (CamelContext: camel-1) is shutdown in 0.143 seconds

As per logs it is not logging the messages logged for the routes further i.e. direct:DomainsWithFTPUsers and so on. Kindly guide on how to achieve the same.


Solution

  • As per suggestion by Claus, i made the following changes to my main method and it is working as expected now. Previously i was using the below code for initializing my context and starting up routes and thus JVM was stopping it's execution :

    public static void main(String[] args) throws Exception {
            ApplicationContext appContext = new ClassPathXmlApplicationContext(
                    "applicationContext.xml");
            CamelContext camelContext = SpringCamelContext.springCamelContext(
                    appContext, false);
        }
    

    Corrected way of keeping JVM Running :

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        main.setFileApplicationContextUri("classpath:applicationContext.xml");
        main.run();
    }