Mulesoft 简明教程

MuleSoft - Endpoints

端点基本上包括那些在 Mule 应用程序的工作流中触发或启动处理的组件。它们在 Anypoint Studio 中称为 Source ,在 Mule 设计中心中称为 Triggers 。Mule 4 中的一个重要端点是 Scheduler component

Endpoints basically include those components that trigger or initiate the processing in a working flow of Mule application. They are called Source in Anypoint Studio and Triggers in the Design Center of Mule. One important endpoint in Mule 4 is Scheduler component.

Scheduler Endpoint

此组件基于时间条件工作,这意味着它使我们能够在基于时间条件满足时触发流。例如,调度程序可以触发事件以每隔 10 秒启动 Mule 工作流。我们还可以使用灵活的 Cron 表达式来触发调度程序端点。

This component works on time-based conditions, which means, it enables us to trigger a flow whenever a time-based condition is met. For example, a scheduler can trigger an event to start a Mule working flow every, say 10 seconds. We can also use flexible Cron expression to trigger a Scheduler Endpoint.

Important points about Scheduler

在使用调度程序事件时,我们需要考虑以下一些重要事项:

While using Scheduler event, we need to take care of some important points as given below −

  1. Scheduler Endpoint follows the time-zone of the machine where Mule runtime is running.

  2. Suppose if a Mule application is running in CloudHub, the Scheduler will follow the time-zone of the region in which the CloudHub worker is running.

  3. At any given time, only one flow triggered by the Scheduler Endpoint can be active.

  4. In Mule runtime cluster, the Scheduler Endpoint runs or triggers only on primary node.

Ways to configure a Scheduler

如上所述,我们可以配置调度程序端点以在固定时间间隔触发,或者我们还可以提供 Cron 表达式。

As discussed above, we can configure a scheduler endpoint to be triggered at a fixed interval or we can also give a Cron expression.

Parameters to configure a Scheduler (For Fixed Interval)

以下是要在常规时间间隔触发流的调度程序的参数:

Following are the parameters to set a scheduler to trigger a flow at regular intervals −

Frequency - 它基本上描述了调度程序端点将触发 Mule 流的频率。可以从时间单位字段中为其选择时间单位。如果您不为此提供任何值,它将使用 1000 的默认值。另一方面,如果您提供 0 或负值,它也将使用默认值。

Frequency − It basically describes at which frequency the Scheduler Endpoint will trigger the Mule flow. Unit of time for this can be selected from the Time Unit field. In case you do not provide any values for this, it will use the default value which is 1000. On the other side, if you provide 0 or a negative value, then also it uses the default value.

Start Delay - 这是应用程序启动后第一次触发 Mule 流之前我们必须等待的时间量。启动延迟的值以与频率相同的时间单位表示。它的默认值为 0。

Start Delay − It is the amount of time we must wait before triggering the Mule flow for the first time once the application is started. The value of Start delay is expressed in the same unit of time as the frequency. Its default value is 0.

Time Unit - 它描述了频率和启动延迟的时间单位。时间单位的可能值是毫秒、秒、分钟、小时、天。默认值为毫秒。

Time Unit − It describes the time unit for both Frequency and Start Delay. The possible values of time unit are Milliseconds, Seconds, Minute, Hours, Days. The default value is Milliseconds.

Parameters to configure a Scheduler (For Cron Expression)

实际上,Cron 是用于描述时间和日期信息的标准。如果您使用灵活的 Cron 表达式来使调度触发器触发,则调度程序端点将跟踪每秒,并在 Quartz Cron 表达式与时间日期设置相匹配时创建一个 Mule 事件。使用 Cron 表达式,可以一次触发事件或在常规时间间隔触发。

Actually, Cron is a standard used for describing time and date information. In case you use the flexible Cron expression to make Scheduler trigger, the Scheduler Endpoint keeps track of every second and creates a Mule event whenever the Quartz Cron expression matches the time-date setting. With Cron expression, the event can be triggered just once or at regular intervals.

下表给出了六个必填设置的日期时间表达式:

Following table gives the date-time expression of six required settings −

Attribute

Value

Seconds

0-59

Minutes

0-59

Hours

0-23

Day of month

1-31

Month

1-12 or JAN-DEC

Day of the week

1-7 or SUN-SAT

以下是一些由调度程序端点支持的 Quartz Cron 表达式的示例:

Some examples of Quartz Cron expressions supported by the Scheduler Endpoint are given below −

  1. ½ * * * * ? − means that the scheduler runs every 2 seconds of the day, every day.

  2. 0 0/5 16 * ?* − means that the scheduler runs every 5 minutes starting at 4 pm and ending at 4:55 pm, every day.

  3. 1 1 1 1, 5 * ? − means that the scheduler runs the first day of January and the first day of April, every year.

Example

以下代码每秒钟记录一次消息“hi”−

The following code logs the message “hi” every second −

<flow name = "cronFlow" doc:id = "ae257a5d-6b4f-4006-80c8-e7c76d2f67a0">
   <doc:name = "Scheduler" doc:id = "e7b6scheduler8ccb-c6d8-4567-87af-aa7904a50359">
      <scheduling-strategy>
         <cron expression = "* * * * * ?" timeZone = "America/Los_Angeles"/>
      </scheduling-strategy>
   </scheduler>
   <logger level = "INFO" doc:name = "Logger"
      doc:id = "e2626dbb-54a9-4791-8ffa-b7c9a23e88a1" message = '"hi"'/>
</flow>