Home > database >  Want to kill my localhost8080 automatically after my spring boot application completed its execution
Want to kill my localhost8080 automatically after my spring boot application completed its execution

Time:10-11

I have written a spring boot application for file uploading. I want my spring boot application to shutdown the running localhost 8080 after its execution has been completed .

Is there any possible ways to kill the server after the spring boot execution is completed

Could someone help me on this

want to kill the localhost programmatically.

CodePudding user response:

You can do this in the following way by creating a bean:

@Component
class ShutdownManager {
@Autowired
private ApplicationContext appContext;
public void initiateShutdown(int returnCode){
    SpringApplication.exit(appContext, () -> returnCode);
}

}

You can autowire this bean anywhere and use the initiateShutdown function to close teh application context and server like this:

@Component
public class Controller{
@Autowired
private final ShutdownManager shutdownManager;
  @GetMapping("/")
public void Shutdown(){
   shutdownManager.initiateShutdown(0);
}
}

Or you can simply Autowire this bean on any controller and call this method any where you want with the return code.

CodePudding user response:

Method 1:

If you are using the actuator module, you can shutdown the application via JMX or HTTP if the endpoint is enabled.

add to application.properties:

endpoints.shutdown.enabled=true

Depending on how an endpoint is exposed, the sensitive parameter may be used as a security hint.

For example, sensitive endpoints will require a username/password when they are accessed over HTTP (or simply disabled if web security is not enabled).

you can check this : spring boot documentation

Method 2: Here is another option that does not require you to change the code or exposing a shut-down endpoint. Create the following scripts and use them to start and stop your app.

start.sh

#!/bin/bash
kill $(cat ./pid.file)

Stops your app using the saved process id

start_silent.sh

#!/bin/bash
nohup ./start.sh > foo.out 2> foo.err < /dev/null &

If you need to start the app using ssh from a remote machine or a CI pipeline then use this script instead to start your app. Using start.sh directly can leave the shell to hang.

After eg. re/deploying your app you can restart it using:

sshpass -p password ssh -oStrictHostKeyChecking=no [email protected] 'cd /home/user/pathToApp; ./stop.sh; ./start_silent.sh'

Method 3 here is a maven quick example for maven user to configure HTTP endpoint to shutdown a spring boot web app using spring-boot-starter-actuator so that you can copy and paste: 1.Maven pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.application.properties:

#No auth  protected 
endpoints.shutdown.sensitive=false

#Enable shutdown endpoint
endpoints.shutdown.enabled=true

All endpoints are listed here

3.Send a post method to shutdown the app: curl -X POST localhost:port/shutdown

Security Note:

if you need the shutdown method auth protected, you may also need

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

config ditails

CodePudding user response:

just put this code at end of your uploading code:

@Controller
public class Upload {

    @Autowired
    private ApplicationContext context;

    @PostMapping("/upload")
    public void uploadFile(){

    // do upload the file here.
    //after uploading shutdown the spring boot application.

    SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);

       }    

     }

I have checked it myself it is working.

  • Related