我有一个需要每天运行的 Spring Boot Batch 应用程序。它读取每日文件,对其数据进行一些处理,并将处理后的数据写入数据库。在此过程中,应用程序保存一些状态,例如要读取的文件(存储在 和 中FlatFileItemReader)JobParameters、运行的当前日期和时间、用于在读取项目之间进行比较的一些文件数据等。
调度的一种选择是使用 Spring,@Scheduled例如:
@Scheduled(cron = "${schedule}")
public void runJob() throws Exception {
jobRunner.runJob(); //runs the batch job by calling jobLauncher.run(job, jobParameters);
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是在运行之间保持状态。因此,我必须更新要读取的文件、运行的当前日期和时间、清除缓存的文件数据等。
另一种选择是通过 unix cron 作业运行应用程序。这显然可以满足在运行之间清除状态的需要,但我更喜欢将作业调度与应用程序而不是操作系统联系起来(并且更喜欢与操作系统无关)。@Scheduled可以在运行之间重置应用程序状态吗?
我想在“每个月的最后一天 10:15”和“每个月的第一个星期天”运行一个 spring 调度器作业 -
我在下面尝试过 - 但它在初始化 spring 上下文时出错:
org.springframework.boot.SpringApplication:应用程序启动失败 java.lang.IllegalStateException:遇到无效的@Scheduled 方法“monthEndSchedule”:对于输入字符串:“L”
@Override
@Scheduled(cron = "0 15 10 L * ?")
public void monthEndSchedule() {
//
}
Run Code Online (Sandbox Code Playgroud)
虽然下面的作品在“每天凌晨 1 点”运行
@Override
@Scheduled(cron = "0 0 1 * * ?")
public void surveyDailySchedule() {
//
}
Run Code Online (Sandbox Code Playgroud)
我使用过的 Cron 表达式参考:http : //www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html
我在当前的 Spring 项目中实现了一个带有 shedlock 的调度程序,如下所示:
@Scheduled(cron = "0 0/1 * * * *")
@SchedulerLock(name = "syncData",
lockAtMostFor = "${shedlock.myScheduler.lockAtMostFor}",
lockAtLeastFor = "${shedlock.myScheduler.lockAtLeastFor}")
public void syncSAData() {
//To assert that the lock is held
LockAssert.assertLocked();
...
}
Run Code Online (Sandbox Code Playgroud)
现在我想为此函数编写单元测试。我面临的问题是:我无法模拟第一个语句:LockAssert.assertLocked()。
我正在自学Spring,目前正在执行计划任务,以下代码不会触发计划任务.
我相信它与我设置Spring上下文的方式有关,但这只是猜测 - 我正在尝试学习Spring,所以请原谅荒谬的while循环.
Application.java:
package hello;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext rootContext =
new AnnotationConfigApplicationContext();
rootContext.register(RootContextConfiguration.class);
rootContext.refresh();
while(rootContext != null) {
try {
Thread.sleep(2500);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Loop\n");
}
rootContext.close();
}
}
Run Code Online (Sandbox Code Playgroud)
RootContextConfiguration.java:
package hello;
import java.util.concurrent.Executor;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
@EnableScheduling
@EnableAsync(
mode = …Run Code Online (Sandbox Code Playgroud) 我正在使用@Scheduled注释来运行一个cron作业.调度工作一段时间,然后停止工作.我将简化我的代码片段:
这是调度程序:
//org.springframework.scheduling.annotation.Scheduled
@Scheduled("*/30 * * * * *")
public void performTask() {
logger.info("Starting agent");
getAgentAsyncTask().execute();
logger.info("Ending agent");
}
Run Code Online (Sandbox Code Playgroud)
这是由调度程序执行的任务
//org.springframework.scheduling.annotation.Async
@Async(TASK_EXECUTOR)
@Override
public void execute() {
logger.info("Starting task");
//send some rest requests
logger.info("Ending task");
}
Run Code Online (Sandbox Code Playgroud)
两者:"起始代理"和"结束代理"被记录的次数相等.因此,每个调度都正确结束.
两者:"启动任务"和"结束任务"被记录的次数相等.所以,当然,"任务"并不是阻挡事物.
但它只是在一段时间后停止记录.可能是什么问题?
这里,TASK_EXECUTOR是以下bean:
@Bean(TASK_EXECUTOR)
public ThreadPoolTaskExecutor createDefaultTaskExecutor() {
ThreadPoolTaskExecutor te = new ThreadPoolTaskExecutor();
te.setMaxPoolSize(15);
te.setCorePoolSize(15);
te.initialize();
return te;
}
Run Code Online (Sandbox Code Playgroud)
春季版:
4.1.6.RELEASE
我需要@Scheduled使用属性文件中的值(如果存在)或默认值(如果不存在)对方法进行参数化。
我们可以通过以下方式从配置文件属性中进行参数化:
@Scheduled(cron = "${my.task.cron-exec-expr}")
public void scheduledTask() {
// do something
}
Run Code Online (Sandbox Code Playgroud)
但是如果该属性不存在,我们将有一个运行时异常。
我尝试使用@ConfigurationProperties具有默认值的bean,但没有成功:
@Component
@ConfigurationProperties(prefix = "my.task")
public class MyTaskProperties {
private String cronExecExpr = "*/5 * * * * *";
// getter and setter
}
Run Code Online (Sandbox Code Playgroud)
如何避免这种情况并传递默认值?
我有一个 Spring Boot 应用程序,其中包含使用 @Scheduled 注释的各种调度程序。一些时间表使用 fixedRate 设置,其他时间表使用 cron。fixedRate 计划运行良好,但我注意到在过去的一个月中,cron 计划触发的实际时间每天会有 1-2 小时的变化。
即具有 cron 设置的工作
@Scheduled(cron = "0 0 2 * * *")
Run Code Online (Sandbox Code Playgroud)
将在凌晨 3:00 而不是凌晨 2:00 触发。它也不是每天都在发生。它会连续几天在同一时间运行,然后发生一些事情,每次运行它都会再关闭一个小时,直到我重新启动应用程序。
服务器上的系统时间是准确的,我在这里完全没有想法。有没有人遇到过这个?
编辑 1
我输入了一些日志来确定作业是否在同一时间间隔内误触发,结果似乎是随机的。以下是三天内上述 cron 作业的开始/结束时间:
Format: Calendar.getInstance().getTime() (System.currentTimeMillis())
Start: Sat Oct 21 03:14:15 CDT 2017 (1508573655778)
End: Sat Oct 21 03:22:24 CDT 2017 (1508574144708)
Start: Sun Oct 22 02:26:58 CDT 2017 (1508657218774)
End: Sun Oct 22 02:35:12 CDT 2017 (1508657712492)
Start: Mon Oct 23 02:00:03 CDT 2017 (1508742003072)
End: Mon …Run Code Online (Sandbox Code Playgroud) 已使用注释了某些方法@Scheduled(fixedDelay=/.../),如何从数据库中获取该值?
已经具有所需的服务和存储库,只是不确定如何在此处应用该值。
我创建了一个Spring Boot应用程序,但在某些可以手动或通过@Scheduled注释触发的端点上遇到了问题。
我遇到的问题如下:
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:在SecurityContext中找不到身份验证对象
SecurityContext如果进程调用 via ,是否有办法触发@Scheduled?
我是新手Spring Security,很难理解参考指南。我发现了一些类似的问题,但仍然无法理解如何将答案应用于我的案例。
我的例子MyController:
@Secured("ROLE_ADMIN")
@RestController
@RequestMapping(value = "/api")
public class MyController {
@Scheduled(cron = "* * * * * *")
@GetMapping(path="/data")
public void getData() {
// Do some operations
}
}
Run Code Online (Sandbox Code Playgroud) 我们试图在指定的窗口时间从 Kafka 读取数据(所以我们有 Kafka 消费者),这意味着避免在其他时间读取数据。但是,我们不确定如何在时间段到期后关闭消费者。我想知道是否有任何示例可以说明如何做到这一点?非常感谢您帮助我们。
apache-kafka spring-scheduled kafka-consumer-api spring-kafka
我正在尝试集成 Shedlock 以使 Spring boot 应用程序上的预定作业在多 Pod 部署中无缝运行。
主要类如下:
@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "60m", defaultLockAtLeastFor = "15m")
@ComponentScan
public class MyService {
public static void main(String[] args) {
SconeApp.run(MyService.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
为了使用 Shedlock 配置调度程序,添加了以下类:
import com.salesforce.tm.scheduler.MyScheduler;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
@Configuration
public class LockProviderConfiguration {
@Bean
public LockProvider lockProvider(MyDbDataSource dataSource) {
return new JdbcTemplateLockProvider(
JdbcTemplateLockProvider.Configuration.builder()
.withJdbcTemplate(new JdbcTemplate(dataSource))
.build()
);
}
@Bean
public MyScheduler myScheduler(LockProvider lockProvider) {
return new MyScheduler();
}
}
Run Code Online (Sandbox Code Playgroud)
MyDbDataSource类如下:
@Component …Run Code Online (Sandbox Code Playgroud) 大家好,我有一个正在运行的春季调度程序作业,它必须在谷歌云上运行,并按预定的时间间隔运行。
它与 docker-compose 本地部署完美配合。它会毫无问题地触发。
虽然它在谷歌云运行服务中本地工作正常,并且 CPU 节流关闭,使 CPU 始终保持 100% 开启,但它在第一次运行后就无法工作。
我将粘贴 docker 文件以供任何一次参考,但我很确定它工作正常
FROM maven:3-jdk-11-slim AS build-env
# Set the working directory to /app
WORKDIR /app
COPY pom.xml ./
COPY src ./src
COPY css-common ./css-common
RUN echo $(ls -1 css-common/src/main/resources)
# Build and create the common jar
RUN cd css-common && mvn clean install
# Build and the job
RUN mvn package -DskipTests
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds …Run Code Online (Sandbox Code Playgroud) spring-batch spring-scheduled spring-boot google-cloud-platform google-cloud-run
spring-scheduled ×12
spring ×8
java ×6
spring-boot ×5
cron ×2
shedlock ×2
spring-batch ×2
apache-kafka ×1
junit ×1
mockito ×1
schedule ×1
spring-kafka ×1
spring-mvc ×1