英文:
Spring - Bean executes method twice
问题
以下是要翻译的内容:
SchedulingTasks
@Component
@EnableScheduling
public class SchedulingTasks {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Autowired
public TestClass testclass;
@Scheduled(fixedDelay = 1 * 60 * 60 * 1000)
public void refreshDatasets() {
testclass.simpleTest();
System.out.println("The time is now " + dateFormat.format(new Date()));
}
}
TestClass
@Component
public class TestClass {
public void simpleTest() {
System.out.println("FINISHED");
}
}
Configuration
@Configuration
@ComponentScan(basePackages = "com.some.analytics.scripts",
excludeFilters = {@ComponentScan.Filter (
type= FilterType.ANNOTATION,
value=Configuration.class)})
public class ScriptsConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
p.setIgnoreResourceNotFound(true);
return p;
}
}
Main
public class Scripts {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ScriptsConfig.class);
SchedulingTasks schedulingTasks = ctx.getBean(SchedulingTasks.class);
schedulingTasks.refreshDatasets();
}
}
当运行主方法时,我得到以下输出:
FINISHED The time is now 11:08:39
FINISHED The time is now 11:08:39
英文:
The below method refreshDatasets is executed twice when I run the app. Any idea what I messed up with the configuration of Spring (annotation based)?
SchedulingTasks
@Component
@EnableScheduling
public class SchedulingTasks {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Autowired
public TestClass testclass;
@Scheduled(fixedDelay = 1 * 60 * 60 * 1000)
public void refreshDatasets() {
testclass.simpleTest();
System.out.println("The time is now " + dateFormat.format(new Date()));
}
}
TestClass
@Component
public class TestClass {
public void simpleTest() {
System.out.println("FINISHED");
}
}
Configuration
@Configuration
@ComponentScan(basePackages = "com.some.analytics.scripts",
excludeFilters = {@ComponentScan.Filter (
type= FilterType.ANNOTATION,
value=Configuration.class)})
public class ScriptsConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
p.setIgnoreResourceNotFound(true);
return p;
}
}
Main
public class Scripts {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ScriptsConfig.class);
SchedulingTasks schedulingTasks = ctx.getBean(SchedulingTasks.class);
schedulingTasks.refreshDatasets();
}
}
When running the main method, I get the following output:
FINISHED The time is now 11:08:39
FINISHED The time is now 11:08:39
答案1
得分: 4
在使用@EnableScheduling
注解时,Spring会在后台创建一个TaskExecutor
。这将调度所有的@Scheduled
方法。对于使用fixedDelay
的方法,它们将立即触发执行(除非设置了initialDelay
)。
您还以编程方式执行任务,因此有两个执行:
- 由Spring执行的一个
- 在主方法中执行的一个
您应该删除手动调用,一切都应按预期工作。
您可以在以下链接中找到更多信息:https://spring.io/guides/gs/scheduling-tasks/ 和 https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-task-scheduler
英文:
When using the @EnableScheduling
annotation, Spring creates a TaskExecutor
in the background. This will schedule all the @Scheduled
methods. In the case of methods with fixedDelay
, they will be fired instantly (unless initialDelay
is set).
You are also programatically executing the task, so you have two executions:
- The one executed by Spring
- The one executed in the main method.
You should remove the manual invocation, and everything should work as expected.
You can find more information in https://spring.io/guides/gs/scheduling-tasks/ and https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-task-scheduler
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论