title: JavaEE: Two objects for the same Timer, Timer in @Timeout and TimerService differ
body: <p>In our JavaEE application (run on JBoss 6.1) we use <code>Timer</code> objects. These objects are created with the <code>timerService.createIntervalTimer()</code> method. The info-object for these Timers is a class which has a <code>String</code> (as an ID) and a boolean field.</p>
  
  <p>Imagine there is only one <code>Timer</code> created for the <code>timerService</code>. Now a method, annotated with <code>@Timeout</code>, is responsible for handling the behaviour when a <code>Timer</code> timeouts. This <code>Timer</code> object is not the same object (different hashcode) as the one accesible through <code>timerService.getTimers()</code>. The IDs of the info objects of the <code>Timers</code> is the same <code>String</code>, but the objects are not the same.</p>
  
  <p>My problem with this is the following:
  I want the <code>@Timeout</code> method to only do something with the <code>Timer</code> when its boolean field is set to true and then set it to false. Later, outside of the @Timeout method, I want to set this boolean field to true again. But as the objects are different, this has no effect.</p>
  
  <p>Is this a bug in JBoss or is this intended behaviour? Does anyone know how to circumvent this?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is a code example:</p>
  
  <pre><code>public class TimerManager {
     @Resource
     TimerService timerService;
  
     public void createTimer(String Id) {
        TimerConfig timerConfig = new TimerConfig();
        timerConfig.setInfo(new TimerInfo(Id));
        this.timerService.createIntervalTimer(new Date(), 120000, timerConfig);
     }
  
     @Timeout
     public void timeOut(Timer timer) {
        TimerInfo timerInfo = (TimerInfo) timer.getInfo();
        if (timerInfo.isFlagSet()) {
           //do something
           System.out.println("Timeout for timer with ID: " + info.getID() + " and hashcode " + timer.hashCode());
           timerInfo.setFlag(false);
        }
     }
  
     //will be called from outside this class and after the timeout happened once
     public void searchTimerByIdAndChangeFlag(String ID) {
        for (Timer timer : this.timerService.getTimers()) {
           if (timer.getInfo() instanceof TimerInfo) {
              TimerInfo info = (TimerInfo) timer.getInfo();
              if (info.getID().equals(ID)) {
                 System.out.println("Searching for timer with ID: " + info.getID() + " and hashcode " + timer.hashCode());
                 info.setFlag(true); // has no effect because the flag is still set to true
              }
           }
        }
     }
  
  }
  
  public class TimerInfo {
     private String id;
     private boolean flag;
  
     public TimerInfo(String id) {
        this.id = id;
        this.flag = true;
     }
  
     //getter and setter for id and flag
  
  }
  </code></pre>
  
  <p>After the first execution of timeOut, the timerInfo.flag of the timer will be false and it can not be changed by searchTimerByIdAndChangeFlag because the timer object stored in timerService.getTimers() with the same timerInfo.id is not the same object as the one in the timeOut method.</p>
  
  <p>I hope this clarifies the situation.</p>