Cron Job in Storm

Posted on 2018-4-16 in Code

Add a cron job in Storm is super easy, only need two steps.

1.Overwrite getComponentConfiguration() method of IComponent interface.

@Override
public Map<String, Object> getComponentConfiguration() {
    Config conf = new Config();
    // For example, work in every 60 seconds.
    conf.put(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS, 60);
    return conf;
}

2.Check whether a tuple is a tick tuple in bolt's execute() method.

@Override
public void execute(Tuple tuple) {
    // check if it is a tuple from cron job
    if (TupleUtils.isTick(tuple)) {
        // Do something
    } else {
        // Do other thing
    }
}