我想为带有方法注释的方法设置AspectJ切入点@Scheduled。尝试了不同的方法,但没有任何效果。
1.)
@Pointcut("execution(@org.springframework.scheduling.annotation.Scheduled * * (..))")
public void scheduledJobs() {}
@Around("scheduledJobs()")
public Object profileScheduledJobs(ProceedingJoinPoint joinPoint) throws Throwable {
LOG.info("testing")
}
Run Code Online (Sandbox Code Playgroud)
2.)
@Pointcut("within(@org.springframework.scheduling.annotation.Scheduled *)")
public void scheduledJobs() {}
@Pointcut("execution(public * *(..))")
public void publicMethod() {}
@Around("scheduledJobs() && publicMethod()")
public Object profileScheduledJobs(ProceedingJoinPoint joinPoint) throws Throwable {
LOG.info("testing")
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以提出任何其他方式都around/ before上建议@Scheduled注解的方法?
我正在调查以固定速率使用@Scheduled的情况,在某些可配置的情况下,不应运行计划的作业。
该文档没有提及这一点,而是分别针对fixedDelay()和fixedDelayString()是-1和的默认值""。可以使用这些方法可靠地确保计划的方法不会触发吗?
我已经编写了一个 Spring 带注释的调度程序程序,但是当我执行它时,它会在帖子标题中给出错误消息。
SchedulerConfig.java
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import com.fwd.pmap.scheduler.SchedulerApp;
@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
@Bean
public SchedulerApp bean() {
return new SchedulerApp();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(4);
}
}
Run Code Online (Sandbox Code Playgroud)
SchedulerApp.java
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.fwd.pmap.memberInterfaceFile.CsvReader;
import com.fwd.pmap.memberInterfaceFile.CsvWriter;;
@Component
public class SchedulerApp
{
@Scheduled(cron="0 0 17 * ? *")
public void importInterfaceFile()
{
CsvReader reader …Run Code Online (Sandbox Code Playgroud) 我在 Spring 有一个预定的工作,我从我的数据库中得到它的 cron。每次执行时,都会更新下一次执行时间。因此,如果它配置为每 10 分钟运行一次,我可以将值更改到数据库中以每 15 分钟安排一次该作业。
问题是我必须等待执行才能获得更新的 cron:如果每 15 分钟安排一次作业并且我想将此值更改为每 2 分钟一次,我必须等待下一次执行(最多 15分钟)每 2 分钟完成一次这项工作。
有没有办法在我更新数据库后重新安排这项工作?
我想销毁并刷新这个 bean,但它不起作用(也许这是不可能的,或者我的实现中出了什么问题)。也许有一种方法可以触发一个事件来执行方法 configureTask。
这是我预定工作的片段。
@EnableScheduling
@Component
public class MyClass implements SchedulingConfigurer {
private static final String JOB = "My personal task";
@Autowired
JobRepository jobRepository;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
System.out.println("Hello World!");
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
JobScheduled byJobNameIgnoreCase = jobRepository.findByJobNameIgnoreCase(JOB); // read from database
String cron = …Run Code Online (Sandbox Code Playgroud) 我有一个 spring 调度程序,它有三个具有不同触发器(cron 触发器)的任务。对于这些任务中的每一个,我都有一个可以修改 cron 表达式的 UI。我想在服务收到更新请求时重新安排任务。下面是我的调度程序配置。如何在运行时更改其中一项任务的计划(当 UI 发送更新数据库中的 cron 表达式的请求时)。使用下面的方法,调度器只更新下一个调度。即在调用触发器时调用 nextExecutionTime() 方法。
@Configuration
@EnableScheduling
public class Config implements SchedulingConfigurer {
@Inject
MyDao db;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.initialize();
taskRegistrar.setTaskScheduler(taskScheduler);
List<TaskConfig> conf = db.readTaks();
conf.forEach((TaskConfig aTask) -> {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
System.out.println("Running task + " + aTask.getName());
}
}, new Trigger() {
@Override
public Date nextExecutionTime(final TriggerContext triggerContext) {
String expression = aTask.getCronExpression();
CronTrigger trigger = new …Run Code Online (Sandbox Code Playgroud) 我正在配置计划的任务以在不同的线程中运行。这是配置代码
@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)
这是使用它的代码
@Scheduled(fixedRateString = "2000" )
public void testaMethod() {
log.debug("here is the message");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我将线程固定时间为2s的线程休眠了10秒钟。所以我希望在日志中看到不同的线程,但是我只能看到一个
日志在这里
{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message"}
{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message"}
{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message"}
{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message"}
Run Code Online (Sandbox Code Playgroud) 现在我有以下声明:
@Scheduled(cron = "0 0 12 ? * MON#1")
protected synchronized void execute() {...}
Run Code Online (Sandbox Code Playgroud)
它不起作用:
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.9.RELEASE.jar:1.5.9.RELEASE]
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'execute': For input string: "2#1"
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:461) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:331) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 19 common frames omitted
Run Code Online (Sandbox Code Playgroud)
请帮助使其正常工作
我已经使用 Spring Framework(版本 5.0.5.RELEASE)在 Java 1.8 类中实现了一个异步方法:
public class ClassToBeTested {
@Autowired
private MyComponent myComponent;
@Async
public void doStuff(List<MyClass> myObjects) {
CompletableFuture<MyResponseObject>[] futureList = new CompletableFuture[myObjects.size()];
int count = 0;
for (MyClass myObject : myObjects) {
futureList[count] = myComponent.doOtherStuff(myObject);
count++;
}
// Wait until all doOtherStuff() calls have been completed
CompletableFuture.allOf(futureList).join();
... other stuff ...
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 JUnit 和 Mockito 测试该类。我已将其设置如下,目的是模拟 doStuff() 方法对组件的调用:
@MockBean
private MyComponent myComponentAsAMock;
@InjectMocks
@Autowired
private ClassToBeTested classToBeTested;
@Test
public void myTest() throws Exception { …Run Code Online (Sandbox Code Playgroud) 在 spring boot 中,我可以通过不使用@Scheduled方法的注释来安排 spring 作业吗?
我正在春季靴中从事春季工作。我想通过使用 cron 表达式来安排作业,但不使用@Scheduled(cron = " ")该方法的注释。
我知道我可以在这个方法中安排一个工作,如下所示。
@Scheduled (cron = "0 10 10 10 * ?")
public void execute() {
/ * some job code * /
}
Run Code Online (Sandbox Code Playgroud)
但我希望它是动态的,以便我可以将 cron 表达式作为用户的输入并安排它。
语境
我正在开发一个 Spring Boot 项目,该项目大量使用 @Scheduled bean。我们编写了一个非常轻量级的类似事件源的框架,该框架依赖于数据库中的数据:每次检查数据库是否有新记录,这可能会导致触发这些新记录的处理程序。
我们编写集成测试:在数据库中创建一条新记录,等待处理程序拾取它并且数据库得到更新并验证数据库的新状态。
问题
在我们编写一个会弄脏 spring 上下文的测试之前,这一切都很好。上下文可以很好地重新创建,但旧上下文中的 @Scheduled 方法继续运行,从旧上下文而不是新上下文中执行逻辑。
我在这个小例子中演示了这个问题:https ://github.com/stainii/spring-boot-test-scheduled-bean-problem 。当您运行测试时(出于演示目的,最好按字母顺序排列),您将看到每个测试本身都运行良好,但是当需要重新创建上下文时,将运行 2 个计划 bean,而不是 1 个。
有谁知道这是否是预期的行为,以及当上下文被标记为脏时我们是否可以销毁 @Scheduled bean?
spring-scheduled ×10
spring ×9
java ×7
spring-boot ×5
cron ×3
aspectj ×1
junit ×1
maven ×1
mockito ×1
scheduler ×1
spring-mvc ×1