Stop and close the Springboot project from within the Web service __springboot

Source: Internet
Author: User
stop and close the Springboot project from within due to business requirements

First of all, the need to encounter, in the project would rather not execute also can not carry out the wrong needs from time to time, recently in a platform, in the Save account to consider security issues, when starting the project as a must check, if the validation does not pass, blocking the project Allow.
Baidu on a variety of checks, there is no such operations, and now research out the discovery super simple. The main purpose of writing this article is to record the research process. Impatient students can look directly at the part of the end result. 1, finally got out

The spring context (which implements the Configurableapplicationcontext context) has a close method that executes context.close where it is needed. 2, how do I get out of One, the matter is not decided to ask Baidu (mainly Google can not ah ~ ~)

At first I was as usual, expecting netizens to have a mature solution. But after a few hours of searching and filtering, I didn't find what I wanted. It's not nothing. Netizens are very happy to dedicate, as long as the search Springboot start, close, terminated, can find two methods.

For this result at the beginning I was very not open Sen, because only this result, and can be found are the results, reached the end of the page to the extent of the results, and finally could not find a solution. I am angry, since no one has studied, I will contribute the time. Second, Java programmers want to upgrade or to see the source and process

Now that spring officially offers two ways to shut down services, it can be done, and if they can do it, so can I. Then according to the steps provided by the user, the following reference to the user provided by the HTTP send Shutdown method to study

1. Introduction of Actuator Dependency
        <dependency> <groupId>org.springframework.boot</groupId> in
          pom.xml
          <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
2. The endpoints.shutdown.enabled of the shutdown endpoint,springboot on the application.properties file is turned off by default.

    #启用shutdown 
    endpoints.shutdown.enabled=true 
    #禁用密码验证 
    endpoints.shutdown.sensitive=false

After a simple two-step can be implemented remote call shutdown system, but this is not what I want, I am looking for in the system directly call a method to achieve automatic shutdown.
To find this method, you must start with an official remote call, use the HTTP request remotely, and then look for the service interface of the HTTP request, no way to print the log slowly

When you find this portal, you start with debug, very painful, because you do not know which step system is closed, only in the method of shutting down the system to start the system again, execution close, so repeatedly looked up several times, finally let me find the closure of the service lair, ha ha ...
Lair Spring-boot-actuator-1.5.4release.jar Bag Shutdownendpoint This class, the following post the main code, this code is basically said to be able to shut down the system, back to say is shutting down, start the shutdown thread, call This.context.close ()。 I went, finally let me know your secret, using the context can be closed. It seems that I have a way to get to the context, I go to check (I have limited skills, just know that this thing, this is the beginning of the story behind).

Shutdownendpoint's main code

Public Shutdownendpoint () {super ("shutdown", true, false); @Override public map<string, object> invoke () {if (This.context = null) {return no_
        Context_message;
        try {return shutdown_message; finally {thread thread = new Thread (new Runnable () {@Override Publ
                    IC void Run () {try {thread.sleep (500L);
                    The catch (Interruptedexception ex) {Thread.CurrentThread (). interrupt ();
                } ShutdownEndpoint.this.context.close ();
    }
            });
            Thread.setcontextclassloader (GetClass (). getClassLoader ());
        Thread.Start ();  @Override public void Setapplicationcontext (ApplicationContext context) throws Beansexception {if (Context instanceof ConfigurableapplicaTioncontext) {This.context = (configurableapplicationcontext) context; }
    }

Spring has a way to get the context
Chinese netizen is to force, a search a lot, are the same--realize Applicationcontextaware interface
Well, according to do, realize the interface, the east copy under, the West copy, with incomparable expectations of the mood to carry out the test ... Why not close ...
I go, debug, and then a variety of checks, pain dead, the following gives the wrong code (where the wrong look), I hope netizens to take warning

public class Shutdowncontext implements Applicationcontextaware
{
    private configurableapplicationcontext context;

    public void Showdown () {
        if (null!= context) {
            context.close ()}
    }

    @Override public
    void Setapplicationcontext (ApplicationContext applicationcontext) throws beansexception{
        If (context instanceof Configurableapplicationcontext) {
            This.context =  (Configurableapplicationcontext) ApplicationContext
        }}
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.