Apache camel实现定时任务 学习一


个人学习,可能理解有误请抱歉

1.先看看别人的代码(看看就行,大致调用from里面的URI,去通知我要去执行任务调度)

   目标是从周一至周五每天15:00安排一份工作

  CamelContext   ctx = new DefaultCamelContext();

   ctx.addRoutes(new RouteBuilder() {
            public void configure() throws Exception {
               from("ftp://Sid@localhost:21/equityFeedsProcessing/?stepwise=false
&scheduler=quartz2&scheduler.cron=00+15+*+*+MON-FRI")

               .to("file:src/main/resources/?fileName=abc.csv");
            }
     });

    ctx.start();
    Thread.sleep(30000);
     ctx.stop();

 

2.找了一些官网的调用方式

    引入配置:

          <dependency>
               <groupId>org.apache.camel</groupId>
               <artifactId>camel-quartz</artifactId>
               <version>x.x.x</version>
               <!-- use the same version as your Camel core version -->
          </dependency>

       官网重要说明:

            Only consumer is supported 

            The Quartz component provides a scheduled delivery of messages using the Quartz Scheduler 2.x.

            Each endpoint represents a different timer (in Quartz terms, a Trigger and JobDetail).

        格式配置:

            quartz://timerName?options
            quartz://groupName/timerName?options
            quartz://groupName/timerName?cron=expression
            quartz://timerName?cron=expression

        官网:

               The component uses either a CronTrigger or a SimpleTrigger.

                If no cron expression is provided, the component uses a simple trigger.

                If no groupName is provided, the quartz component uses the Camel group name.

                You can append query options to the URI in the following format, ?option=value&option=value&…

        ​我理解的意思:

             没有cron表达式走默认的触发器,有的话走cron指定规则的触发器(这些是后面跟着表达式)

                          也可以自定义触发器规则

          官网提供的制定规则获取方式(字面意思,具体实现还得看你自己的代码使用):

                The Quartz component supports 13 options, which are listed below.

                   bridgeErrorHandler (consumer) :

                         Allows for bridging the consumer to the Camel routing Error Handler,

                         which mean any exceptions occurred while the consumer is trying to

                         pickup incoming messages, or the likes,will now be processed as a message

                         and handled by the routing Error Handler.                        

                         By default the consumer will use the org.apache.camel.spi.ExceptionHandler

                         to deal with exceptions,

                         that will be logged at WARN or ERROR level and ignored.             

                   enableJmx (consumer):

                         Whether to enable Quartz JMX which allows to manage the Quartz scheduler from JMX.

                         This options is default true

                   prefixInstanceName (consumer):

                        Whether to prefix the Quartz Scheduler instance name with the CamelContext name.

                         This is enabled by default, to let each CamelContext use its own Quartz scheduler instance by default.

                         You can set this option to false to reuse Quartz scheduler instances between multiple CamelContext’s.

                    prefixJobNameWithEndpointId (consumer):

                          Whether to prefix the quartz job with the endpoint id. This option is default false.

                   properties (consumer):

                          Properties to configure the Quartz scheduler.

                   propertiesFile (consumer):

                         File name of the properties to load from the classpath

                   propertiesRef (consumer):

                         References to an existing Properties or Map to lookup

                         in the registry to use for configuring quartz.

                   basicPropertyBinding (advanced):

                         Whether the component should use basic property binding (Camel 2.x)

                         or the newer property binding with additional capabilities

                   scheduler (advanced):

                         To use the custom configured Quartz scheduler,

                         instead of creating a new Scheduler.

                    schedulerFactory (advanced)

                         To use the custom SchedulerFactory which is used to create the Scheduler.

                   autoStartScheduler (scheduler):

                          Whether or not the scheduler should be auto started.

                          This options is default true

                    interruptJobsOnShutdown (scheduler):

                            Whether to interrupt jobs on shutdown which

                             forces the scheduler to shutdown quicker and

                            attempt to interrupt any running jobs. If this is enabled then

                            any running jobs can fail due to being interrupted.

                    startDelayedSeconds (scheduler):

                            Seconds to wait before starting the quartz scheduler.

               

           在控制点上配置定时任务  

            The Quartz endpoint is configured using URI syntax:

                      quartz:groupName/triggerName

              with the following path and query parameters:

 

                      groupName(定时任务组名,类型String):

                            The quartz group name to use. The combination of group name

                            and trigger name should be unique.

                       triggerName(触发器名,类型String):

                             Required The quartz trigger name to use. The combination

                             of group name and trigger name should be unique.

              query parameters (告诉定时任务需要做哪些事,比如cron表达式,定义了触发器执行的时间):

                        bridgeErrorHandler (consumer):

                                 Allows for bridging the consumer to the Camel routing Error Handler,

                                 which mean any exceptions occurred

                                while the consumer is trying to pickup incoming messages,

                                or the likes, will now be processed as a message

                                and handled by the routing Error Handler. By default the consumer 

                                will use the org.apache.camel.spi.ExceptionHandler

                                to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

                         cron (consumer):

                                 Specifies a cron expression to define when to trigger.

                          deleteJob (consumer):

                                  If set to true, then the trigger automatically delete when route stop.

                                  Else if set to false,

                                  it will remain in scheduler. When set to false, it will also mean user may

                                  reuse pre-configured trigger with camel Uri.

                                  Just ensure the names match. Notice you cannot

                                  have both deleteJob and pauseJob set to true.

                          durableJob (consumer):

                                    Whether or not the job should remain stored after

                                     it is orphaned (no triggers point to it).

                           pauseJob (consumer):

                                     If set to true, then the trigger automatically pauses when route stop.

                                     Else if set to false, it will remain in scheduler. When set to false,

                                      it will also mean user may reuse pre-configured trigger with camel Uri.

                                     Just ensure the names match. Notice you cannot have

                                     both deleteJob and pauseJob set to true.

                           recoverableJob (consumer):

                                      Instructs the scheduler whether or not the job should be re-executed

                                      if a 'recovery' or 'fail-over' situation is encountered.

                            stateful (consumer):

                                      Uses a Quartz PersistJobDataAfterExecution and

                                      DisallowConcurrentExecution instead of the default job.

                             exceptionHandler (consumer):

                                      To let the consumer use a custom ExceptionHandler.

                                      Notice if the option bridgeErrorHandler is enabled then

                                      this option is not in use.

                                      By default the consumer will deal with exceptions,

                                      that will be logged at WARN or ERROR level and ignored.

                             exchangePattern (consumer):

                                        Sets the exchange pattern when the consumer creates an exchange.

                                        The value can be one of: InOnly, InOut, InOptionalOut

                              basicPropertyBinding (advanced):

                                        Whether the endpoint should use basic property binding (Camel 2.x)

                                         or the newer property binding with additional capabilities

                                customCalendar (advanced):

                                         Specifies a custom calendar to avoid specific range of date

                                jobParameters (advanced): 

                                         To configure additional options on the job.

                                prefixJobNameWithEndpointId (advanced):

                                          Whether the job name should be prefixed with endpoint id

                                synchronous (advanced):

                                         Sets whether synchronous processing should be strictly used,

                                         or Camel is allowed to use asynchronous processing (if supported).

                               triggerParameters (advanced):

                                         To configure additional options on the trigger.

                                usingFixedCamelContextName (advanced):

                                         If it is true, JobDataMap uses the CamelContext name directly

                                        to reference the CamelContext, if it is false, JobDataMap uses

                                        use the CamelContext management name which

                                        could be changed during the deploy time.

                                autoStartScheduler (scheduler):

                                          Whether or not the scheduler should be auto started.

                                 fireNow (scheduler):

                                          If it is true will fire the trigger when the route is start when using SimpleTrigger.

                                 startDelayedSeconds (scheduler):

                                          Seconds to wait before starting the quartz scheduler.

                                  triggerStartDelay (scheduler):

                                           In case of scheduler has already started, we want the trigger

                                           start slightly after current time to ensure endpoint is fully

                                           started before the job kicks in.

                              例子:
                                   以下路由规则将触发两个计时器事件                 

                                   from("quartz://myGroup/myTimerName?trigger.repeatInterval=2&trigger.repeatCount=1")

                                   .routeId("myRoute").to("mock:result");
   

        

 

学习来源:

        https://blog.csdn.net/asd5629626/article/details/81627444

        http://ask.sov5.cn/q/TgUee79K9S   --quartz2的Apache Camel Cron作业调度

        https://blog.csdn.net/zymx14/article/details/56666188 --Quartz2.x

        https://camel.apache.org/components/latest/quartz-component.html

    


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM