How to schedule nightly restart of application server in Elastic Beanstalk

587 views Asked by At

I have a Java web application running on Tomcat 8.5 in AWS Elastic Beanstalk. Everyday (judging from the localhost.log, around midnight Pacific Time), AWS does something to cause a java.lang.NoClassDefFoundError: Could not initialize <my.app.MyServlet$InnerClass>. I would then need to restart the app server in the Elastic Beanstalk environment to fix the problem. How do I schedule a daily automatic restart of the Tomcat server? Or better yet: Is there a way to detect the error and then automatically restart Tomcat? Why is this happening?

1

There are 1 answers

0
Michael Bressler On

The easiest way to do this is to use the AWS SDK combined with your own scheduling system. Every day, this Java code runs in the middle of the night. Make sure you have the credential provider chain (https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html) set properly. System properties works pretty well.

Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    AWSElasticBeanstalk client = AWSElasticBeanstalkClientBuilder.standard().withRegion(Regions.US_EAST_1).build();

    RestartAppServerRequest request = new RestartAppServerRequest().withEnvironmentId(ENVIRONMENT_ID);
    RestartAppServerResult response = client.restartAppServer(request);