Search in sources :

Example 1 with QuartzComponent

use of org.apache.camel.component.quartz2.QuartzComponent in project camel by apache.

the class QuartzComponentAutoConfiguration method configureQuartzComponent.

@Lazy
@Bean(name = "quartz2-component")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(QuartzComponent.class)
public QuartzComponent configureQuartzComponent(CamelContext camelContext, QuartzComponentConfiguration configuration) throws Exception {
    QuartzComponent component = new QuartzComponent();
    component.setCamelContext(camelContext);
    Map<String, Object> parameters = new HashMap<>();
    IntrospectionSupport.getProperties(configuration, parameters, null, false);
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        Object value = entry.getValue();
        Class<?> paramClass = value.getClass();
        if (paramClass.getName().endsWith("NestedConfiguration")) {
            Class nestedClass = null;
            try {
                nestedClass = (Class) paramClass.getDeclaredField("CAMEL_NESTED_CLASS").get(null);
                HashMap<String, Object> nestedParameters = new HashMap<>();
                IntrospectionSupport.getProperties(value, nestedParameters, null, false);
                Object nestedProperty = nestedClass.newInstance();
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), nestedProperty, nestedParameters);
                entry.setValue(nestedProperty);
            } catch (NoSuchFieldException e) {
            }
        }
    }
    IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), component, parameters);
    return component;
}
Also used : HashMap(java.util.HashMap) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) HashMap(java.util.HashMap) Map(java.util.Map) QuartzComponent(org.apache.camel.component.quartz2.QuartzComponent) Lazy(org.springframework.context.annotation.Lazy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with QuartzComponent

use of org.apache.camel.component.quartz2.QuartzComponent in project camel by apache.

the class QuartzScheduledPollConsumerScheduler method doStart.

@Override
protected void doStart() throws Exception {
    ObjectHelper.notEmpty(cron, "cron", this);
    if (quartzScheduler == null) {
        // get the scheduler form the quartz component
        QuartzComponent quartz = getCamelContext().getComponent("quartz2", QuartzComponent.class);
        setQuartzScheduler(quartz.getScheduler());
    }
    JobDataMap map = new JobDataMap();
    // do not store task as its not serializable, if we have route id
    if (routeId != null) {
        map.put("routeId", routeId);
    } else {
        map.put("task", runnable);
    }
    map.put(QuartzConstants.QUARTZ_TRIGGER_TYPE, "cron");
    map.put(QuartzConstants.QUARTZ_TRIGGER_CRON_EXPRESSION, getCron());
    map.put(QuartzConstants.QUARTZ_TRIGGER_CRON_TIMEZONE, getTimeZone().getID());
    job = JobBuilder.newJob(QuartzScheduledPollConsumerJob.class).usingJobData(map).build();
    // store additional information on job such as camel context etc
    QuartzHelper.updateJobDataMap(getCamelContext(), job, null);
    String id = triggerId;
    if (id == null) {
        id = "trigger-" + getCamelContext().getUuidGenerator().generateUuid();
    }
    trigger = TriggerBuilder.newTrigger().withIdentity(id, triggerGroup).withSchedule(CronScheduleBuilder.cronSchedule(getCron()).inTimeZone(getTimeZone())).build();
    LOG.debug("Scheduling job: {} with trigger: {}", job, trigger.getKey());
    quartzScheduler.scheduleJob(job, trigger);
}
Also used : JobDataMap(org.quartz.JobDataMap) QuartzComponent(org.apache.camel.component.quartz2.QuartzComponent)

Example 3 with QuartzComponent

use of org.apache.camel.component.quartz2.QuartzComponent in project camel by apache.

the class CronScheduledRoutePolicy method doOnInit.

protected void doOnInit(Route route) throws Exception {
    QuartzComponent quartz = route.getRouteContext().getCamelContext().getComponent("quartz2", QuartzComponent.class);
    setScheduler(quartz.getScheduler());
    if (getRouteStopGracePeriod() == 0) {
        setRouteStopGracePeriod(10000);
    }
    if (getTimeUnit() == null) {
        setTimeUnit(TimeUnit.MILLISECONDS);
    }
    // validate time options has been configured
    if ((getRouteStartTime() == null) && (getRouteStopTime() == null) && (getRouteSuspendTime() == null) && (getRouteResumeTime() == null)) {
        throw new IllegalArgumentException("Scheduled Route Policy for route {} has no start/stop/suspend/resume times specified");
    }
    registerRouteToScheduledRouteDetails(route);
    if (getRouteStartTime() != null) {
        scheduleRoute(Action.START, route);
    }
    if (getRouteStopTime() != null) {
        scheduleRoute(Action.STOP, route);
    }
    if (getRouteSuspendTime() != null) {
        scheduleRoute(Action.SUSPEND, route);
    }
    if (getRouteResumeTime() != null) {
        scheduleRoute(Action.RESUME, route);
    }
}
Also used : QuartzComponent(org.apache.camel.component.quartz2.QuartzComponent)

Example 4 with QuartzComponent

use of org.apache.camel.component.quartz2.QuartzComponent in project wildfly-camel by wildfly-extras.

the class QuartzIntegrationTest method testManualComponentConfig.

@Test
public void testManualComponentConfig() throws Exception {
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch procLatch = new CountDownLatch(3);
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addComponent("quartz2", new QuartzComponent() {

        @Override
        public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception {
            super.onCamelContextStarted(context, alreadyStarted);
            if (!alreadyStarted) {
                startLatch.countDown();
            }
        }
    });
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("quartz2://mytimer?trigger.repeatCount=3&trigger.repeatInterval=100").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    procLatch.countDown();
                }
            }).to("mock:result");
        }
    });
    camelctx.start();
    try {
        Assert.assertEquals("StartLatch is zero", 0, startLatch.getCount());
        Assert.assertTrue("ProcLatch reached zero", procLatch.await(500, TimeUnit.MILLISECONDS));
    } finally {
        camelctx.stop();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) QuartzComponent(org.apache.camel.component.quartz2.QuartzComponent) Test(org.junit.Test)

Example 5 with QuartzComponent

use of org.apache.camel.component.quartz2.QuartzComponent in project camel by apache.

the class SimpleScheduledRoutePolicy method doOnInit.

protected void doOnInit(Route route) throws Exception {
    QuartzComponent quartz = route.getRouteContext().getCamelContext().getComponent("quartz2", QuartzComponent.class);
    setScheduler(quartz.getScheduler());
    if (getRouteStopGracePeriod() == 0) {
        setRouteStopGracePeriod(10000);
    }
    if (getTimeUnit() == null) {
        setTimeUnit(TimeUnit.MILLISECONDS);
    }
    // validate time options has been configured
    if ((getRouteStartDate() == null) && (getRouteStopDate() == null) && (getRouteSuspendDate() == null) && (getRouteResumeDate() == null)) {
        throw new IllegalArgumentException("Scheduled Route Policy for route {} has no start/stop/suspend/resume times specified");
    }
    registerRouteToScheduledRouteDetails(route);
    if (getRouteStartDate() != null) {
        scheduleRoute(Action.START, route);
    }
    if (getRouteStopDate() != null) {
        scheduleRoute(Action.STOP, route);
    }
    if (getRouteSuspendDate() != null) {
        scheduleRoute(Action.SUSPEND, route);
    }
    if (getRouteResumeDate() != null) {
        scheduleRoute(Action.RESUME, route);
    }
}
Also used : QuartzComponent(org.apache.camel.component.quartz2.QuartzComponent)

Aggregations

QuartzComponent (org.apache.camel.component.quartz2.QuartzComponent)7 CountDownLatch (java.util.concurrent.CountDownLatch)3 CamelContext (org.apache.camel.CamelContext)3 Exchange (org.apache.camel.Exchange)3 Processor (org.apache.camel.Processor)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IllegalStateException (javax.resource.spi.IllegalStateException)1 JobDataMap (org.quartz.JobDataMap)1 Scheduler (org.quartz.Scheduler)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1 Lazy (org.springframework.context.annotation.Lazy)1