Job Scheduling for Software Developers
If you are reading this document, it is assumed that you have downloaded and installed Flux onto your computer. If not, browse to
www.fluxcorp.com/download for a free trial version of Flux.
This guide will take you through the steps involved in using Flux for scheduling your Java code to fire at specific times throughout the day. We will use a pre-made Java class to demonstrate the coding aspect for Flux.
To schedule your code with Flux:
- Start your Java Integrated Development Environment (IDE), and open the "JobScheduling.java" file located within the same directory as this document, "/doc/quick_start_guides/quick_start_job_scheduling/software_developers". If you have not configured your IDE to work with Flux, you will have to arrange the IDE to point to the "flux.jar" file located within your Flux installation directory, as well as the jar files located inside the "lib" directory also underneath your Flux installation directory.
- Look over the code for the "JobScheduling" file. There are a couple notable conventions in this example for using the Flux API. If you are familiar with the Factory design pattern, you will notice it is used heavily in Flux. Several objects require either helpers or factories to create instances; these include the actual Flux engine and flowcharts at the highest level. The flowchart is also a factory - it is used to produce most of Flux's actions and triggers excluding special ones such as BPM and JMS actions. The most rudimentary progression of a program using the Flux API is "factory-> flowchart -> action -> action properties -> flows between actions".
For instructional purposes, we will walk through the creation of the timer trigger that starts the action. First a factory is created using the static "makeFactory" method of the Factory class. The factory then creates a flowchart using the "makeFlowchart" method. The flowchart is the factory used to create various actions and triggers, specifically a Timer Trigger object, timer, in this example. With the object instantiated, its properties will need to be set. A TimerTrigger object requires a time expression to schedule its executions correctly. A time expression can be conveyed using a Cron expression as is used in the example. Flux provides a Cron class to easily make proper time expressions. First a Cron object is made using an EngineHelper (The "Factory" class is able to create these), then it is configured by setting the various levels, and finally it is passed into the timer trigger using the method "setTimeExpression". The "setTimeExpression" method accepts a string with 12 parts.
Take the time to review other parts of the example code such as the creation of engines (the engine in the example is unsecured) and the how Java Actions work. Refer to the Flux javadoc entries on Factory and FlowChart for a complete list of methods and objects that are spawned from it.
The flow from actions is an important part of Flux. Notice within this example, the flows that are setup from the Timer Trigger to the Java Action. These flows are required in order for the Timer Trigger to execute the Java Action upon firing.
- Next, run this example at the command line or using a Java IDE. The line "Your Java Code Goes Here!" will be printed to the console from which you ran this example every 10 seconds. The Timer Trigger's schedule, executed within the example code, was created for simplicity and visibility; you can actually watch the Timer Trigger fire the Java Action every ten seconds as opposed to every hour. A more plausible schedule is commented out within the code below the running cron schedule in the example. To dispose the engine, press the "Enter" key.
- Flux employs robust cron time expressions to schedule events. As demonstrated in this example, Flux provides the Cron object to ease the creation of time expressions. Cron expressions can be specified from the year down to the millisecond to execute. The Cron entry in the javadocs describes the methods available to set these values.
Congratulations, you have used Flux's job scheduling capabilities to execute your own Java code at the times you required the code to execute. For more information on job scheduling, refer to the "examples/software_developers" directory, under your Flux installation directory. This directory holds many job scheduling examples that provide you with detailed, and easy to follow examples. For more details on coding with Flux, see the "Software Developers Manual" located within the "doc" directory.