How to handle AWS connection reaper Error

Problem

SEVERE: The web application [] appears to have started a thread named [java-sdk-http-connection-reaper] but has failed to stop it. This is very likely to create a memory leak.”

Root cause

This issue is caused by AWS Java SDK Client library which starts a thread in the background called com.amazonaws.http.IdleConnectionReaper.

Solution

Ideally, AWS should provide some cleaner to handle this error. Workaround is to call shutdown method yourslef in the code. Below example provides solution for Spring based applications using Java Config. High level, you have to do 2 things:

  1. Declare a bean.
  2. Add @Predestroy method in that bean which can invoke shutdown method explicitly.
ServiceConfig.java
  // Create a bean to register destroy method where we can shutdown the 
  //  httpconnection objects left open by AWS SDK
  // @return AmazonDynamoDBClient
  //
  @Bean
  public AwsIdleConnectionShutdownBean awsIdleConnectionShutdownBean() {
      return new AwsIdleConnectionShutdownBean();
  }
AwsIdleConnectionShutdownBean.java
@Component
public class AwsIdleConnectionShutdownBean {

  private static final Logger LOGGER = LoggerFactory
  					.getLogger(AwsIdleConnectionShutdownBean.class.getName());
  
  @PreDestroy
  private void cleanUp() {
    
      try {
          // Shutting down AWS IdleConnectionReaper thread...
          LOGGER.info("Shutting down AWS IdleConnectionReaper thread ");
          com.amazonaws.http.IdleConnectionReaper.shutdown();
      } catch (Throwable t) {
          // log error
          LOGGER.error("Shutting down AWS IdleConnectionReaper thread failed.");
      }
  }

}

Reference


# Reference
1 http://stackoverflow.com/questions/18069042/spring-mvc-webapp-schedule-java-sdk-http-connection-reaper-failed-to-stop

Version History


Date Description
2016-06-26    Initial Version