Skip to content

Martini Invoking Services via a Scheduler Trigger

Overview

The Scheduler trigger enables developers to configure services to execute at specified schedules or fixed intervals. This functionality is crucial for automating tasks that need to run periodically, such as data synchronization, routine maintenance, or batch processing.

Prerequisites

  • Understanding of Martini services and triggers.
  • Knowledge of cron expressions for cron scheduling.

Configuration

General Configuration

Refer to the general trigger configuration documentation for initial setup steps and parameters applicable to all triggers.

Scheduler-specific Configuration

The Scheduler trigger can be configured to execute tasks based on a simple repeating interval or a cron expression.

Properties

Property Default Description
Type Simple Repeating The frequency of execution, configurable as either a cron expression or a simple fixed interval.
Stateful false If true, preserves the JobDataMap for subsequent executions and prevents concurrent execution.

Schedule Types

  • Simple Repeating: Executes the service at a fixed interval, specified in seconds.
  • Cron: Utilizes cron expressions for precise scheduling, akin to the Unix cron utility.

Examples of Scheduler Configuration

Simple Repeating

To configure a service to execute every 300 seconds:

1
2
Type: Simple Repeating
Interval: 300 (5 minutes)

Cron

To schedule a service to run at 3 AM on weekdays:

1
2
Type: Cron
Cron Expression: 0 0 3 ? * MON-FRI

Service Parameters

Scheduler-specific Parameters

Services invoked by the Scheduler trigger can access specific contextual information through parameters.

Name Type Description
context org.quartz.JobExecutionContext Provides the execution context from Quartz.
jobDataMap org.quartz.JobDataMap A map for passing data between executions of the job.
job org.quartz.Job The Quartz job instance being executed.

Troubleshooting and FAQs

Troubleshooting

  • Problem: Service not executing as scheduled.
  • Solution: Check the schedule configuration for accuracy. Ensure the Martini server's time zone aligns with the expected execution times.

Example

Groovy Script as Service

Consider a Groovy script that prints available variables in the context when the Scheduler trigger is activated. This script showcases how to access execution context and other parameters provided by the Scheduler trigger.

1
2
3
['properties', 'parameters', 'martiniPackage', 'properties', 'context', 'jobDataMap', 'job', '$trackerId', '$tracker'].each {
    println "$it\t : " + this[it]
}

Upon activation, the console will display logs similar to the following, illustrating the contextual information available to the script:

1
2
3
4
5
6
7
8
9
properties   : [ <omitted> ]
parameters   : [ <omitted> ]
martiniPackage   : martiniPackage [name=examples]
properties   : [ <omitted> ]
context   : JobExecutionContext: <omitted>
jobDataMap   : [ <omitted> ]
job   : io.toro.martini.core.endpoint.impl.vfs.VFSClientJobWrapper@5f10fee7
$trackerId   : null
$tracker   : null

Groovy Function as Service

A function within a Groovy class can also be designated as a service for the Scheduler trigger. The following example replicates functionality similar to sending scheduled emails, demonstrating practical use of the Scheduler trigger for automated tasks:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class SchedulerExamples {
    void sendScheduledEmail() {
        String email = 'login'.getPackageProperty()
        String password = 'password'.getPackageProperty()
        String port = 'port'.getPackageProperty()
        String subject = 'subject'.getPackageProperty()
        String[] to = 'email.bulk.to'.getPackageProperty().split(',')
        String protocol = 'protocol'.getPackageProperty()
        String server = 'server'.getPackageProperty()

        to.each {
            String body = '''<html>
            <head></head>
            <body>
                Hello $name!,<br/>
                This is an email from Martini!<br/>
                <br/>
                Thanks,<br/>
                TORO
            </body>
            </html>
            '''.parse([name: it])
            "${protocol}://${URLEncoder.encode(email, 'UTF-8')}:${URLEncoder.encode(password, 'UTF-8')}@${server}:${port}/${subject}"
                    .send([to: it], body)
        }
    }
}
These examples demonstrate the flexibility and utility of the Scheduler trigger in automating various tasks within Martini, using Groovy scripts and functions.