The Timer component is used to generate message exchanges when a timer fires. The Timer is a simple, non persistence timer using the JDK's in built timer mechanism.

package com.sample;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.DataFormat;

public class Timer {

	public static void main(String args[]) throws Exception {
		CamelContext context = new DefaultCamelContext();

		context.addRoutes(new RouteBuilder() {
			public void configure() throws Exception {
				from("timer://foo?period=1000").process(new Processor() {
					@Override
					public void process(Exchange exchange) throws Exception {
						System.out.println("Hello world  :"
								+ new java.util.Date().toString());
					}
				});
			}

		});
		context.start();
		Thread.sleep(10000);
		context.stop();
	}
}

Using Quartz to fire timed based events

Quartz uses the Quartz library which uses a database to store timer events and supports distributed timers and cron notation

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
 
 
public class QuartzRoute extends RouteBuilder {
 
 
    @Override
    public void configure() throws Exception {
 
 
        from("quartz://myGroupName/myTimerName?cron=0/5+*+*+*+*+?")
        .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                System.out.println("I'm running every 5 sec...");
            }
        });
    }
}

In order to use the Quartz component, you need to include camel-quartz library to your project:

<dependency>
          <groupId>org.apache.camel</groupId>
          <artifactId>camel-quartz</artifactId>
          <version>${camel.version}</version>
</dependency>

0
0
0
s2smodern