触发后在Kafka Streams上取消Punctuator

时间:2019-02-21 09:07:39

标签: apache-kafka apache-kafka-streams

我在变压器上创建了一个计划的标点符号,并计划它定期运行(使用kafka v2.1.0)。每当我接受一个特定的密钥时,都会创建一个像这样的新密钥

        scheduled = context.schedule (Duration.ofMillis(scheduleTime),
             PunctuationType.WALL_CLOCK_TIME,new CustomPunctuator(context, customStateStoreName));

我的问题是,我创建的所有这些标点符号始终运行,而我找不到消除它们的方法。我在互联网上找到了一个片段供使用

    private Cancellable scheduled;

    @Override
    public void init(PorcessorContext processContext) {
        this.context = processorContext;
        scheduled = context.schedule(TimeUnit.SECONDS.toMillis(5), PunctuationType.WALL_CLOCK_TIME,
                                     this::punctuateCancel);
    }

    private void punctuateCancel(long timestamp) {
        scheduled.cancel();
    }

但是不幸的是,这似乎只取消了最新创建的Punctuator。

我正在编辑我的帖子,以进一步了解我的方法以及这与wardzinia的评论有何关系。所以我的方法非常相似,只是使用Map,因为每个事件键只需要激活一个标点符号,因此在我的Transformer类中启动

   private Map<String,Cancellable> scheduled  = new HashMap<>();

在我的transform方法上,我确实执行以下代码

{
 final Cancellable cancelSched = scheduled.get(recordKey);

 // Every time I get a new event I cancel my previous Punctuator
 // and schedule a new one ( context.schedule a few lines later)

 if(cancelSched != null)
    cancelSched.cancel();

 // This is supposed to work like a closure by capturing the currentCancellable which in the next statement
 // is moved to the scheduled map. Scheduled map at any point will have the only active Punctuator for a
 // specific String as it is constantly renewed
 // Note: Previous registered punctuators have already been cancelled as it can be seen by the previous
 // statement (cancelSched.cancel();)

 Cancellable currentCancellable = context.schedule(Duration.ofMillis(scheduleTime), PunctuationType.WALL_CLOCK_TIME,
                new CustomPunctuator(context, recordKey ,()-> scheduled ));

 // Update Active Punctuators for a specific key.

 scheduled.put(recordKey,currentCancellable);   
}

然后我在Punctuator标点方法上使用该注册的回调来取消最后一个活动的Punctuator 在它开始之后。它似乎可以工作(虽然不确定),但是感觉很“ hacky”,不是那种解决方案 当然是可取的。

所以在触发后如何取消标点符号。有办法解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

认为您可以做的一件事是:

class CustomPunctuator implements Punctuator {
  final Cancellable schedule;

  public void punctuate(final long timestamp) {
    // business logic
    if (/* do cancel */) {
      schedule.cancel()
    }
  }
}

// registering a punctuation
{
  final CustomPunctuator punctuation = new CustomPunctuator();
  final Cancellable currentCancellable = context.schedule(
    Duration.ofMillis(scheduleTime),
    PunctuationType.WALL_CLOCK_TIME,
    punctuation);

  punctuation.schedule = currentCancellable;
}

这样,您无需维护HashMap并为每个CustomPunctuator实例提供一种取消自身的方法。

答案 1 :(得分:0)

我也有同样的情况,只是对于对scala感兴趣的人,我以

处理
val punctuation = new myPunctuation()
val scheduled:Cancellable=context.schedule(Duration.ofSeconds(5), PunctuationType.WALL_CLOCK_TIME, punctuation)
punctuation.schedule=scheduled

班级

class myPunctuation() extends Punctuator{
      var schedule: Cancellable = _
      override def punctuate(timestamp: Long): Unit = {
      println("hello")
      schedule.cancel()
      }

    }

像魅力一样工作