Camel之Quartz2

代码示例如下:

import java.util.List;

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

public class Quartz2Demo {
    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.start();
        camelContext.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                // TODO Auto-generated method stub
                from("quartz2:quartz2Demo?cron=1 * * * * ?").process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        // TODO Auto-generated method stub

                        System.out.println("Quartz2 执行....");
                    }
                });
            }
        });

        List routes = camelContext.getRoutes();
        for (Route route : routes) {
            System.out.println(route.getId() + " , " + route.getProperties());
        }

        synchronized (Quartz2Demo.class) {
            Quartz2Demo.class.wait();
        }
    }
}

注意:
quartz2 只能用于消费者,不能用于生产者,且作为消费者时类似定时器,在满足条件时自动执行,其中cron代表的是执行条件的一个表达式。示例中:1 * * * * ? 表示 每分钟执行一次,关于quartz2表达式的写法,请自行百度啦。


需要导包:

<dependency>
    <groupId>org.apache.camelgroupId>
    <artifactId>camel-quartz2artifactId>
    <version>2.16.2version>
    
dependency>

你可能感兴趣的:(Camel)