Spring – Bean执行方法两次

huangapple go评论150阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年7月21日 19:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63013207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定