我已经配置了两个不同的线程池,一个用于@Scheduled和另一个用于@Async.但是,我注意到@Async没有使用线程池.
这是Scheduler配置
@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
private final int POOL_SIZE = 10;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
threadPoolTaskScheduler.setThreadNamePrefix("my-sched-pool-");
threadPoolTaskScheduler.initialize();
scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
}
Run Code Online (Sandbox Code Playgroud)
这是Async的配置
@Configuration
@EnableAsync
public class AppConfig {
@Bean(name = "asyncTaskExecutor")
public TaskExecutor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(15);
executor.setMaxPoolSize(15);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("my-async-pool-");
executor.initialize();
return executor;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我如何调用它们
@Scheduled(fixedRateString = "2000" )
public void schedule() {
log.debug("here is the message …Run Code Online (Sandbox Code Playgroud) threadpoolexecutor spring-scheduled spring-boot taskscheduler spring-async
我似乎想到了一个问题。
我有课
@Component
@Scope("prototype")
public class MyClass extends BaseClass {
....
...
@Async
public void doSomething() {
....
}
....
}
Run Code Online (Sandbox Code Playgroud)
和一个 Spring 配置,其中包含
<context:annotation-config />
<context:component-scan base-package="com.company.project" />
<task:annotation-driven executor="taskExecutor"/>
<task:executor id="taskExecutor" pool-size="10" queue-capacity="10" />
Run Code Online (Sandbox Code Playgroud)
在我的代码的某些部分
BaseClass bean = springBeans.getBean(MyClass.class);
Run Code Online (Sandbox Code Playgroud)
但我得到了这个例外
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myClass' must be of type [com.company.project.MyClass], but was actually of type [$Proxy19]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361)
Run Code Online (Sandbox Code Playgroud)
我可以理解它是一个代理类,但不知道为什么Spring不允许转换代理类。
我在类路径上有 cglib 2.2 no dep,以及 Spring 3.2 核心库。
任何人都可以指出解决此问题的任何线索吗?
简而言之,我希望一个方法在调用时是异步的。
@Scheduled(cron = "0 0 0 * * *")
Run Code Online (Sandbox Code Playgroud)
这会在午夜运行春季计划的作业。我如何明确添加该作业应运行的年份?(我只想禁用今年测试环境中的作业,所以我想设置2016年)。
我使用 @Scheduled 注释每两分钟运行一次代码。然而,我们的工作往往会持续更长的时间。据我了解,@Scheduled 注释将新作业排队并在第一个作业完成后立即运行它们。我不希望这种情况发生。我希望只有 1 个正在运行的作业实例,并且没有排队的实例。我怎样才能做到这一点?
@Scheduled(cron = "0 */2 * * * ?")
public void twoMinMethod() {
// code here
}
Run Code Online (Sandbox Code Playgroud) 在 Spring 中,我可以使用以下方法获取 nextExecution 时间:
final CronSequenceGenerator generator = new CronSequenceGenerator(cronExpression);
final Date nextExecutionDate = generator.next(new Date());
Run Code Online (Sandbox Code Playgroud)
但是如何从 cron 表达式中获取最后一次执行时间呢?
我有一个使用 Spring 作业调度的 Spring Boot 应用程序。我遵循了这个样本。
现在的问题是如何获取当前正在运行的调度程序/作业的列表?目前我正在使用 Spring Boot 1.5.2 RELEASE。
谢谢
我正在使用春季时间表。我配置了以下 Cron 表达式以在每周二晚上 9 点运行我的任务,
"0 0 21 * * TUE"
Run Code Online (Sandbox Code Playgroud)
但是,在启动应用程序时出现以下异常
遇到无效的 @Scheduled 方法“runSchduler”:Cron 表达式必须包含 6 个字段
我的 Spring Cron 表达式错误吗?
我有一个问题,我想在运行时创建一个计划任务。计划任务应以固定速率触发。但是现在我遇到了手动设置计划没有以异步方式触发的问题。
主要问题是,如果我们可以启动调度程序,我们没有任何修复点。它应该在我读取特定值 (1) 时创建,并在值变回 (0) 时销毁。否则,我们可以使用下面测试 1 中描述的注释配置。
到目前为止我尝试过的:
1. 安排@Scheduled(fixedRate = 500L)和@Async
代码
@Async
@Scheduled(fixedRate = 500L)
public void annotationTest() {
UUID id = UUID.randomUUID();
log.warn("Hello from Thread {} going to sleep", id);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.warn("Finished Thread {}", id);
}
Run Code Online (Sandbox Code Playgroud)
还具有类级别的@EnableAsync和@EnableScheduling注释。
结果
09:56:24.855 [task-5] : Hello from Thread 3b5514b2-3b80-4641-bf12-2cd320c4b6e5 going to sleep
09:56:25.355 [task-6] : Hello from Thread e98514a7-e193-422b-9569-f7635deb33f8 going to sleep
09:56:25.356 [task-4] …Run Code Online (Sandbox Code Playgroud) 我有 springBoot 应用程序在多个实例中运行 Quarts (2.3.0),集群模式为 true 。
我已经配置了作业并在每次运行之间提供了 2 秒的延迟。
@Configuration
public class SchedulerConfig {
@Bean
public JobDetail jobDetail() {
return JobBuilder.newJob()
.ofType(BatchTriggerJob.class)
.storeDurably()
.withIdentity("SCHEDULER")
.withDescription("event")
.build();
}
@Bean
public Trigger trigger() {
return TriggerBuilder
.newTrigger()
.forJob(jobDetail())
.withIdentity("BATCH")
.withDescription("SCHEDULER")
.withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatForever().withIntervalInSeconds(2)
.withMisfireHandlingInstructionIgnoreMisfires())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
我已启用 DisallowConcurrentExecution
@DisallowConcurrentExecution
public class BatchTriggerJob extends QuartzJobBean {
@Autowired
private SchedulerService schedulerService;
@Override
protected void executeInternal(JobExecutionContext context) {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
schedulerService.processBatch(); //business logic and may take more than 2 sec
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序.yml
quartz:
job-store-type: …Run Code Online (Sandbox Code Playgroud) 示例场景:我有两个 API(帐户 API 和向用户公开的 API),其中帐户 API 将为我提供帐户列表(最小大小大约为 500,并且当添加更多帐户时可能会增加)。我想根据过滤器获取帐户并将其他 API 中的数据公开给用户。
我正在使用 Spring @scheduled 来安排每 30 分钟获取一次帐户 API 的作业。
在单个 Pod 中,调度程序将轻松执行作业。
当应用程序复制到 Kubernetes 中的三个 Pod 时,所有调度程序将同时唤醒,这是调度程序作业的重复。
预期行为:
我希望调度程序以同步方式工作,如果一个帐户正在由一个调度程序处理,则另一个调度程序应该处理下一个帐户。基本上,我希望调度程序像多线程程序一样工作。
像调度程序 1 处理 200 个帐户,调度程序 2 处理 200 个帐户,调度程序 3 处理 100 个帐户。
我是 Spring boot 应用程序开发的新手,想知道是否可以完成上述操作。
我读到了 Shedlock,但它一次只能运行一个调度程序。但我喜欢使用所有调度程序并更快地处理帐户
spring synchronization spring-scheduled spring-boot shedlock
spring-scheduled ×10
spring ×7
java ×6
spring-boot ×5
cglib ×1
cron ×1
shedlock ×1
spring-async ×1
spring-mvc ×1