预定任务无法与WebSocket一起使用

时间:2019-05-16 13:12:35

标签: java spring-boot scheduled-tasks spring-websocket

我有一个带有websockets的spring boot应用程序:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@EnableScheduling
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

WebSocket配置:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(socketHandler(), "/connect/*")
                .setAllowedOrigins("*")
                .addInterceptors(handshakeInterceptor());
    }

    @Bean
    public WebSocketHandler socketHandler() {
        return new CustomHandler();
    }

    @Bean
    public HandshakeInterceptor handshakeInterceptor() {
        return new CustomInterceptor();
    }    
}

效果很好。 然后,我添加了@EnableSheduled并创建了计划组件:

@Component
public class ScheduledTask {

    @Scheduled(fixedRate = 1000)
    public void printHello() {
        System.out.println("hello");
    }
}

并获得一个例外:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'defaultSockJsTaskScheduler' is expected to be of type 'org.springframework.scheduling.TaskScheduler' but was actually of type 'org.springframework.beans.factory.support.NullBean'
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:392) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:224) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1116) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1083) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.resolveSchedulerBean(ScheduledAnnotationBeanPostProcessor.java:313) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:254) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:231) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:103) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:896) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:163) ~[spring-boot-2.1.5.BUILD-20190515.065035-40.jar:2.1.5.BUILD-SNAPSHOT]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.5.BUILD-20190515.065035-40.jar:2.1.5.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.5.BUILD-20190515.065035-40.jar:2.1.5.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.5.BUILD-20190515.065035-40.jar:2.1.5.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.5.BUILD-20190515.065035-40.jar:2.1.5.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.5.BUILD-20190515.065035-40.jar:2.1.5.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.5.BUILD-20190515.065035-40.jar:2.1.5.BUILD-SNAPSHOT]
    at ru.test.project.TestApplication.main(TestApplication.java:17) [classes/:na]

我试图从我的项目中删除websockets。此后,一切开始正常。 如何解决?

3 个答案:

答案 0 :(得分:1)

可以通过手动定义TaskScheduler bean来解决此问题,例如,参见此帖子:https://medium.com/@jing.xue/spring-boot-application-startup-error-with-websocket-enabled-832456bb2e

所以,用Java术语来说,应该是

@Bean
public TaskScheduler taskScheduler() {
    TaskScheduler scheduler = new ThreadPoolTaskScheduler();

    scheduler.setPoolSize(2);
    scheduler.setThreadNamePrefix("scheduled-task-");
    scheduler.setDaemon(true);

    return scheduler;
}

答案 1 :(得分:1)

除了米歇尔接受的答案:

使用Spring Boot时,与其直接创建TaskScheduler,不如直接使用TaskScheduler:

@Bean
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
    return builder.build();
}

该构建器在org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration中定义。因此,使用这种方法,您将获得与不使用@EnableWebSocket时完全相同的调度程序(即它将使用spring配置属性中的池大小等)。

答案 2 :(得分:0)

有一种更方便的方法可以解决此问题。 您可以使用 @Configurable @EnableScheduling 注释自定义调度程序类,并在其中实现 SchedulingConfigurer 接口。 然后,您应该实现 configureTasks 方法,该方法具有 ScheduledTaskRegistrar 对象作为输入参数。 使用此对象,您可以定义和执行任何类型的计划任务。 只需记住您需要在父Bean中将此内容告知Spring Framework Context。

父Bean类的构造函数:

public ParentBean() {
        new AnnotationConfigApplicationContext(MyTaskScheduler.class);
}

MyTaskScheduler类:

@Configurable
@EnableScheduling
public class MyTaskScheduler implements SchedulingConfigurer {
    @Override
    public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {
        // -- Schedule task #1  --
        taskRegistrar.addFixedDelayTask(() -> { firstTask(); }, 10000);

        // -- Schedule task #2 --
        taskRegistrar.addFixedRateTask(() -> { secondTask(); }, 1000);
    }

    private void firstTask() {
        // -- Your first task logic goes here! --
    }

    private void secondTask() {
        // -- Your second task logic goes here! --
    }
}