Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flink 无法在与其他源代码联合的Apache Flink中的自定义源代码函数中休眠_Apache Flink_Flink Streaming_Flink Batch - Fatal编程技术网

Apache flink 无法在与其他源代码联合的Apache Flink中的自定义源代码函数中休眠

Apache flink 无法在与其他源代码联合的Apache Flink中的自定义源代码函数中休眠,apache-flink,flink-streaming,flink-batch,Apache Flink,Flink Streaming,Flink Batch,我有两个源,一个是卡夫卡源,一个是自定义源,我需要做一个睡眠自定义源一个小时,但我得到以下中断 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at com.hulu.hiveIngestion.HiveAddPartitionThread.run(HiveAddPartitionThread.java:48) at org.apache.

我有两个源,一个是卡夫卡源,一个是自定义源,我需要做一个睡眠自定义源一个小时,但我得到以下中断

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)
代码:

.union()
公共类自定义_源实现SourceFunction{
公共作废运行(SourceContext ctx){
while(true)
{
睡眠(1000);
ctx.收集(“字符串”);
}
}
}

如何使睡眠自定义源,而卡夫卡源将继续其流。为什么会出现线程中断异常?

这更像是一个Java问题,而不是Flink问题。简而言之,您永远不能依赖Thread.sleep(x)来睡眠x毫秒。正确支持中断也很重要,否则您无法正常关闭作业

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;
    }
}
公共类自定义\u源实现SourceFunction{
专用静态最终持续时间睡眠时间=持续时间(1);
private volatile boolean isCanceled=false;
公共作废运行(SourceContext ctx){
而(!isCanceled){
//1小时等待时间
LocalTime end=LocalTime.now().plusHours(1);
//此循环确保随机中断不会过早关闭源
while(LocalTime.now().compareTo(end)<0){
试一试{
sleep(Duration.between(LocalTime.now(),end.toMillis());
}捕捉(中断异常e){
//吞咽中断,除非源被取消
如果(已取消){
Thread.interrupted();
返回;
}
}
}
ctx.收集(“字符串”);
}
}
@凌驾
公开作废取消(){
isCanceled=true;
}
}
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;
    }
}