我有两个来源,一个是Kafka来源,一个是自定义来源,我需要制作一个睡眠自定义来源一个小时,但我的工作中断了。
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.hulu.hiveIngestion.HiveAddPartitionThread.run(HiveAddPartitionThread.java:48)
at org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:100)
at org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:63)
at org.apache.flink.streaming.runtime.tasks.SourceStreamTask$LegacySourceFunctionThread.run(SourceStreamTask.java:201)
代码:
<kafka_Source>.union(<custom_source>)
public class custom_source implements SourceFunction<String> {
public void run(SourceContext<String> ctx) {
while(true)
{
Thread.sleep(1000);
ctx.collect("string");
}
}
}
当Kafka源将继续其流时,如何使睡眠自定义源。为什么我收到线程中断异常?
答案 0 :(得分:1)
这比Flink问题更是Java。简而言之,您永远不能依靠Thread.sleep(x)来睡眠x ms。正确地支持中断也很重要,否则您将无法正常关闭工作。
public class custom_source implements SourceFunction<String> {
private static final Duration SLEEP_DURATION = Duration.ofHours(1);
private volatile boolean isCanceled = false;
public void run(SourceContext<String> ctx) {
while (!isCanceled) {
// 1 hour wait time
LocalTime end = LocalTime.now().plusHours(1);
// this loop ensures that random interruption is not prematurely closing the source
while (LocalTime.now().compareTo(end) < 0) {
try {
Thread.sleep(Duration.between(LocalTime.now(), end).toMillis());
} catch (InterruptedException e) {
// swallow interruption unless source is canceled
if (isCanceled) {
Thread.interrupted();
return;
}
}
}
ctx.collect("string");
}
}
@Override
public void cancel() {
isCanceled = true;
}
}