Child pages
  • Quick Start Guide

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
package doc.quick_start_guides.quick_start_job_scheduling.software_developers;

import flux.ActionListener;
import flux.Cron;
import flux.CronColumn;
import flux.Engine;
import flux.EngineHelper;
import flux.Factory;
import flux.FlowChart;
import flux.JavaAction;
import flux.KeyFlowContext;
import flux.TimerTrigger;

/**
 * Quick Start example for scheduling Java code for execution.
 */
public class JobScheduling {

//  Create a Flux factory, from which all other Flux objects are derived.
  Factory factory = Factory.makeInstance();

//  Create a fresh cron.
  Cron cron = EngineHelper.makeCron();
  public JobScheduling() throws Exception {

//    Create a workflow.
    FlowChart flowChart = EngineHelper.makeFlowChart("Workflow");

//    Create a timer trigger.
    TimerTrigger timer = flowChart.makeTimerTrigger("Timer");
    timer.setStartAction(true);

//    Fires every 10 seconds , every minute, every hour, Monday-Friday.
    cron.add(CronColumn.MILLISECOND, 0);
    cron.addAll(CronColumn.SECOND, 10);
    System.out.println("The Cron that is produced is " + "\"" + cron.toString() + "\"");

//    The created cron must be set here.
    timer.setTimeExpression(cron.toString());

//    Work week cron shown below. Fires every 15 minutes, 8-12 am, and 1-5 pm,
//    Monday-Friday.
//    cron.add(CronColumn.MILLISECOND, 0);
//    cron.add(CronColumn.SECOND, 0);
//    cron.addAll(CronColumn.MINUTE, 15);
//    cron.add(CronColumn.HOUR, 8,12);
//    cron.add(CronColumn.HOUR, 13,15);
//    cron.add(CronColumn.DAY_OF_WEEK, Cron.MONDAY, Cron.FRIDAY);
//    Create a Java Action to implement your Java code in Flux.
    JavaAction javaAction = flowChart.makeJavaAction("Java Action");
    javaAction.setListener(JavaCode.class);

//    Create flows from each action.
    timer.addFlow(javaAction);
    javaAction.addFlow(timer);

//    Create a Flux engine.
    Engine engine = factory.makeEngine();

//    Add the workflow into the engine to execute.
    engine.put(flowChart);

//    Start the engine.
    engine.start();

//    Wait for the user to press any key before the engine is disposed.
    System.in.read();
    System.out.println("The engine has been disposed.");
    engine.dispose();
  }

/**
*     Listener for the Java Action. Put your Java Class into this listener.
*/
  public static class JavaCode implements ActionListener {

    public Object actionFired(KeyFlowContext flowContext) throws Exception {
      System.out.println("Your Java Code Goes Here!");
      return null;
    }
  }

/**
*     A simple way to run this example.
*/
  public static void main(String[] args) throws Exception {
    new JobScheduling();
  }
}