title: Thread keeps running even after application has been stopped in Websphere
body: <p>I have a long running thread which is created using <code>org.springframework.scheduling.commonj.WorkManagerTaskExecutor</code>
  with Spring and is running in Websphere Application Server 8. </p>
  
  <p>The problem is that this thread keeps running even if the application has been stopped. That thread needs to be stopped too but it is not happening. I even tried to use  <code>Thread.currentThread().isInterrupted()</code> to check if the current thread was interrupted but it always returns <code>false</code>. So there is no way to know through my code if the Thread should keep running or stop.</p>
  
  <p>This is my spring configuration for the WorkManagerTaskExecutor:</p>
  
  <pre><code>&lt;bean id="taskExecutor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor"&gt;
        &lt;property name="workManagerName" value="wm/default" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The thread is being executed this way:</p>
  
  <pre><code>Thread t = new EmailReaderThread(email);
  workManagerTaskExecutor.execute(t);
  </code></pre>
  
  <ul>
  <li>What am I missing? </li>
  <li>What can I do so that whenever the application is stopped the application's thread (threads that were generated by the application) stops too?</li>
  </ul>
  
  <p>I think this is not considered an unmanaged thread because I am registering the thread using the appropriate <a href="http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/cspr_design.html" rel="nofollow">WorkManager</a> which the container exposes as a resource by JNDI.</p>
  
  <p>Update:
  Here is the code which creates the Thread.</p>
  
  <pre><code>@Service
  @Transactional
  public class SmsServiceHypermedia implements SmsService {
  
      @Autowired
      private WorkManagerTaskExecutor workManagerTaskExecutor;
  
  
      public SmsServiceHypermedia() {
          createEmailReaderThread();
      }
  
      private void createEmailReaderThread() {
          log.debug("Generating Email Reader Threads...");
          Email email = getDefaultEmail(); //obtain the default Email object, not important for the problem.
          EmailReaderThread r = new EmailReaderThread(email);
          workManagerTaskExecutor.execute(r);     
      }
  
      private class EmailReaderThread extends Thread {
  
          private Email email;
          private Session session;
  
          public EmailReaderThread(Email email) {
              this.email = email;
          }
  
          @Override
          public void run()  {
              readEmails();
          }
  
          public void readEmails() {
              final long delay = 30 * 1000; //delay between message poll.
              log.debug("Starting to read emails for email: " + email.getAddress());
              while(!Thread.currentThread().isInterrupted()) {
                  try {
                      log.debug("Current session: " + session);
                      Store store = session.getStore();
                      log.debug("Connecting using session: " + session);
                      store.connect();
                      Folder inbox = store.getFolder("INBOX");
                      inbox.open(Folder.READ_WRITE);
  
                      javax.mail.Message[] messages = inbox.search(
                              new FlagTerm(new Flags(Flags.Flag.SEEN), false));
                      for (javax.mail.Message message : messages) {
                          //Do something with the message
                      }
                      inbox.close(true);
                      store.close();
                      block(delay);
                  } catch (Exception e) {
                      throw new RuntimeException(e);
                  }
              }
          }
  
          //I know this could be implemented by calling Thread.sleep() is just that I ran out of options so I also tried it this way.
          private void block(long millis) {
              final long endTime = System.currentTimeMillis() + millis;
              log.debug("Blocking for this amount of time: " + millis + " ms");
              while (System.currentTimeMillis() &lt; endTime) {
              }
              log.debug("End of blocking.");
          }
      }   
  }
  </code></pre>