title: run multiple threads (each with own application context) and shutdown gracefully
body: <p>I have a multithreaded app which I'm redesigning with SpringBoot 1.5. Please look at the following example:</p>
  
  <pre><code>@Service
  @Lazy
  class MyService {
      private static final Logger logger = LoggerFactory.getLogger(MyService.class);
  
      private String account;
  
      private boolean stopped = false;
      private boolean processing;
  
      public MyService(String account) {
          logger.debug("MyService constructor");
          this.account = account;
      }
  
      public void run() {
          logger.debug("starting thread " + account);        
          while(!stopped) {
              try {
                  processing = false;
                  Thread.sleep(5000); // awaiting some service response
                  processing = true;
                  Thread.sleep(3000); // processing service response
              } catch (InterruptedException e) {
                  logger.error(null,e);
              }
          }
          logger.debug("finished gracefully");
      }
  
      public void stop() {
          stopped = true;
      }
  }
  
  @SpringBootApplication
  public class App {
  
      private static final String[] accounts = { "user1", "user2", "user3" };
  
      public static void main(String[] args) {
          for(String account : accounts) {
              new Thread(() -&gt; {
                  ConfigurableApplicationContext context = SpringApplication.run(App.class, account);
                  BeanFactory factory = context.getBeanFactory();
                  MyService service = factory.getBean(MyService.class, account);
  
                  context.addApplicationListener(event -&gt; {
                      if(event instanceof ContextClosedEvent) {
                          service.stop();
                          // context.registerShutdownHook();
                          // context.close();
                      }
                  });
                  service.run();
              }).start();
          }
      }
  }
  </code></pre>
  
  <p>application.properties</p>
  
  <pre><code>logging.level.com.example = DEBUG
  </code></pre>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;multicontext-app&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.3.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>I came up with multi-context configuration because I want to autowire "singleton" scoped beans which have thread-specific data.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li>Is it a correct way to create application context per thread?</li>
  <li>Why can I see duplicated log messages (thread number squared times)? For example, "MyService constructor" message is printed 9 times when 3 threads are running, instead of 3 times (one instance per context).</li>
  <li>How to shutdown each service thread gracefully, taking into account that no need to wait if the service is awaiting response and not processing it? Currently, I can't see "finished gracefully" message when the app stopped.</li>
  <li>Do I need to call <code>context.close()</code> or <code>context.registerShutdownHook()</code> or both? when I should do that and what will happen wouldn't I do that?</li>
  </ol>