当前位置:网站首页>Commandlinerunner may cause your application to stop down. I advise you to use mouse tail juice

Commandlinerunner may cause your application to stop down. I advise you to use mouse tail juice

2020-12-08 09:57:21 Coder little black

hello, Hello everyone , I'm Xiaohei , See you again ~~

If you go to a certain degree to search for keywords CommandLineRunner Initialize the resource , Before Xiao Hei wrote this tweet , Probably 1,030,000 results .

CommandLineRunner  Initialize the resource

Most of the articles on the Internet tell us that we can use CommandLineRunner To initialize resources , But very few articles tell us : If CommandLineRunner Improper use , It will cause some strange exceptions in the program , It's more likely to cause our application to stop running directly .

If you are reading this article, if you also use CommandLineRunner To initialize resources , So, Xiao Hei, you are advised to use mouse tail juice , Take a quick look at the following cases ~

CommandLineRunner The execution time is too long ???

@Slf4j
@SpringBootApplication
public class CommandLineRunnerDemoApp {

    private Map<String, String> map;

    public static void main(String[] args) {
        SpringApplication.run(CommandLineRunnerDemoApp.class, args);
    }
    
    @RestController
    public class controller {

        @GetMapping("/name")
        public String name() {
            return map.get("name");
        }
    }

    @Bean
    public CommandLineRunner commandLineRunner() {
        return args -> {
            //  Simulation load data is too slow 
            log.info("start do commandLineRunner...");
            TimeUnit.MINUTES.sleep(1);
            map = ImmutableMap.of("namne", "coder Little black ");
            log.info("do commandLineRunner end");
        };
    }
}

Spring After the container starts , visit http://localhost:8080/name, At this point, the background will directly report an error :

 Error log

By reporting the wrong information, we can know :

CommandLineRunner stay Spring The container gets up and starts executing , But this time Tomcat The request can be received normally . And because in this case CommandLineRunner It takes too long to run , The data has not been initialized yet , So the program starts to go wrong ......

CommandLineRunner Wrong execution report ???

Then if CommandLineRunner What happens if an error is reported in the execution process ?

The answer is :Spring The container will automatically close , The app will stop serving .

Maybe readers will refute Xiao Hei's saying that :“CommandLineRunner Run at startup time , If CommandLineRunner Operation error reporting , Then the release failed .”

In fact, there are more serious ......

When the execution time is too long, an error is reported , How is your app ???

I don't say much nonsense , Go straight to the specific case , Look at the code first :

@Slf4j
@SpringBootApplication
public class CommandLineRunnerDemoApp2 implements CommandLineRunner {

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

    @Override
    public void run(String... args) throws Exception {
        log.info("start do commandLineRunner...");
        //  The duration of the simulated task is 
        TimeUnit.MINUTES.sleep(1);

        //  There was an error during the simulation 
        int i = 1 / 0;

        log.info("do commandLineRunner end");
    }
}

The running log is as follows :

 Error log 2

You can see ,Spring The container started working properly , The system began to provide services to the outside world . In a minute ,CommandLineRunner Report an error in the course of execution , Lead to Spring Container closure , Application stops service .

Say a few more words

Although the cases above are simple , But in the actual process, Xiao hei , I met some students who used it CommandLineRunner To initialize a very time-consuming resource , And when initializing resources , I made a mistake again , So the app suddenly stopped . But fortunately , It's just that the environment test happened this time , Everything is OK online .

版权声明
本文为[Coder little black]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208095645763u.html