弹簧靴应用启动线程

2022-09-01 04:30:37

我想在春季启动后执行一个java类(其中包含一个我要执行的java线程)。我的初始代码:

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

以下是我想在开始时执行的代码:

public class SimularProfesor implements Runnable{

    // Class atributes

    // Constructor
    public SimularProfesor() {
        //Initialization of atributes
    }

    @Override
    public void run() {
        while(true) {
            // Do something
        }
    }
}

如何调用此线程?这是我应该做的:

@SpringBootApplication
public class Application {
     public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        // Call thread (constructor must be executed too)
     }
}

答案 1

不要自己弄乱线程。Spring(以及普通的Java)对此有一个很好的抽象。

首先在配置中创建该类型的 BeanTaskExecutor

@Bean
public TaskExecutor taskExecutor() {
    return new SimpleAsyncTaskExecutor(); // Or use another one of your liking
}

然后创建一个(尽管也可以工作)来计划您的任务。CommandLineRunnerApplicationListener<ContextRefreshedEvent>

@Bean
public CommandLineRunner schedulingRunner(TaskExecutor executor) {
    return new CommandLineRunner() {
        public void run(String... args) throws Exception {
            executor.execute(new SimularProfesor());
        }
    }
}

当然,您也可以在春季管理自己的班级。

这样做的好处是,Spring还会为您清理线程,您不必自己考虑。我在这里使用了一个,因为它将在所有bean都初始化bean后执行。CommandLineRunner


答案 2
  • 主类弹簧靴

    @SpringBootApplication
    @EnableAsync
    @Controller
    public class ...
    
    
  • 示例类控制程序

    import javax.annotation.PostConstruct;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.core.task.TaskExecutor;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ExecutorBase {
    
        private static final Logger log = LoggerFactory.getLogger(ExecutorBase.class);
    
        @Autowired
        private TaskExecutor taskExecutor;
        @Autowired
        private ApplicationContext applicationContext;
    
        private Boolean debug = true;
    
        @PostConstruct
        public void atStartup() {
            ClasseTaskRunn classeTaskRunn = applicationContext.getBean(ClasseTaskRunn.class);
            taskExecutor.execute(classeTaskRunn );
            if (debug) {
                log.warn("###### Startup ok");
            }
        }
    }
    
    
  • 示例类任务可运行

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    @Component
    @Scope("application")
    public class ClasseTaskRunn implements Runnable {
    
        private static final Logger log = LoggerFactory.getLogger(ClasseTaskRunn.class);
    
        @Autowired
        ClasseDAO classeDAO;
    
        @Override
        public void run() {
            longBackgorund();
        }
    
        protected void longBackgorund() {
            while (test) {
                if (debug) {
                    log.warn("###### DEBUG: " ... );
                }
    
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    

推荐