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