gpt4 book ai didi

java - Spring Boot @Scheduled 暂停

转载 作者:行者123 更新时间:2023-11-28 23:15:39 25 4
gpt4 key购买 nike

我编写了一个 springboot 应用程序,每 15 分钟执行一次从数据源到另一个数据湖的 etl。我已经使用 @Scheduled 注释为函数安排了执行。

我已经创建了 jar 并直接通过 java -jar ingest.jar 执行。它可以正常工作几天(3-4 天)。并且毫无异常(exception)地停顿一下。要恢复,我必须去按任意键使其再次激活。

@Scheduled(initialDelayString = "${ingest.initialdelay.in.seconds}000", fixedRateString = "${ingest.rate.in.seconds}000")
public void ingestData(){
// Ingestion Logic
}

因为问题依旧,我创建了war并部署到tomcat服务器上。但问题依然存在。

有人可以指出我在这里缺少什么吗?如果我部署到 cloudfoundry,相同的应用程序工作正常。

IO 流 - FileInputStreamFileOutputStream

IO 辅助函数

public static void saveLastSuccessfulDate(String filepath, String propertyName, Date dateTime) {
Properties prop = new Properties();
OutputStream output = null;
try {
String lastDate = getDateInFormat(dateTime);
log.info("Saving: " + lastDate);

output = new FileOutputStream(filepath);

prop.setProperty(propertyName, lastDate);

prop.store(output, null);

} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


//Helper to Write to properties file
public static String checkLastSuccessfulDateAsString(String filepath, String propName) {

Properties prop = new Properties();
InputStream input = null;

try {

input = new FileInputStream(filepath);

// load a properties file
prop.load(input);

String lastSuccesfulDate = prop.getProperty(propName);

log.info("Last Successful Date: "+lastSuccesfulDate);
return lastSuccesfulDate;

} catch (FileNotFoundException f) {
log.error("checkLastSuccessfulDateAsString: File Not Found: " + f.getMessage());

} catch (IOException ex) {
log.error(ex.getMessage());
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return null;

}

问候

最佳答案

默认情况下,spring @Scheduled 使用单线程。因此,如果一个任务被阻塞,则下一个任务将不会运行。

您可以让您的任务类实现 SchedulingConfigurer。它将使用多线程来运行任务并避免阻塞。像这样的代码:

@Component
public class TaskService implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(taskExecutor());
}

@Bean(destroyMethod = "shutdown")
public ScheduledExecutorService taskExecutor() {
return Executors.newScheduledThreadPool(100);
}

// your code
@Scheduled(initialDelayString = "${ingest.initialdelay.in.seconds}000", fixedRateString = "${ingest.rate.in.seconds}000")
public void ingestData(){
// Ingestion Logic
}
}

可能对你有帮助。

关于java - Spring Boot @Scheduled 暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50006548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com