如何在 Apache Storm 拓扑中运行周期性任务?



我有一个Apache Storm拓扑,想每隔一段时间执行一次特定的操作。我不知道如何以一种自然和优雅的方式解决这个问题。

应该是使用ScheduledExecutorServiceBoltSpout,还是其他东西?

Tick 元组是一个不错的选择 https://kitmenke.com/blog/2014/08/04/tick-tuples-within-storm/

编辑:这是螺栓的基本代码

@Override
public Map<String, Object> getComponentConfiguration() {
// configure how often a tick tuple will be sent to our bolt
Config conf = new Config();
conf.put(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS, 300);
return conf;
}

然后,您可以使用TupleUtils.isTick(tuple)inexecute来检查收到的元组是否为刻度元组。

我不知道这是否是一种正确的方法,但它似乎工作正常: 在Boltprepare方法的末尾,我添加了对intiScheduler()的调用,其中包含以下代码:

Calendar calendar = Calendar.getInstance();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new PeriodicAction() [class implementing Runnable], millisToFullHour(calendar) [wanna start at the top of the hour], 60*60*1000 [run every hour], TimeUnit.MILLISECONDS);

不过,这需要谨慎使用,因为螺栓可以有多个实例,具体取决于您的设置。

最新更新