private static ProtobufMessages.Timers writeTimers(
      Collection<TimerJobInstance> timers, MarshallerWriteContext outCtx) {
    if (!timers.isEmpty()) {
      List<TimerJobInstance> sortedTimers = new ArrayList<TimerJobInstance>(timers);
      Collections.sort(
          sortedTimers,
          new Comparator<TimerJobInstance>() {
            public int compare(TimerJobInstance o1, TimerJobInstance o2) {
              return (int) (o1.getJobHandle().getId() - o2.getJobHandle().getId());
            }
          });

      ProtobufMessages.Timers.Builder _timers = ProtobufMessages.Timers.newBuilder();
      for (TimerJobInstance timer : sortedTimers) {
        JobContext jctx = ((SelfRemovalJobContext) timer.getJobContext()).getJobContext();
        if (jctx instanceof ObjectTypeNode.ExpireJobContext
            && !((ObjectTypeNode.ExpireJobContext) jctx)
                .getExpireAction()
                .getFactHandle()
                .isValid()) {
          continue;
        }
        TimersOutputMarshaller writer = outCtx.writersByClass.get(jctx.getClass());
        Timer _timer = writer.serialize(jctx, outCtx);
        if (_timer != null) {
          _timers.addTimer(_timer);
        }
      }
      return _timers.build();
    }
    return null;
  }
  public void addTimerJobInstance(TimerJobInstance instance) {

    JobContext ctx = instance.getJobContext();
    if (ctx instanceof SelfRemovalJobContext) {
      ctx = ((SelfRemovalJobContext) ctx).getJobContext();
    }
    Map<Long, TimerJobInstance> instances = null;
    if (ctx instanceof ProcessJobContext) {
      int sessionId = ((ProcessJobContext) ctx).getSessionId();
      instances = timerInstances.get(sessionId);
      if (instances == null) {
        instances = new ConcurrentHashMap<Long, TimerJobInstance>();
        timerInstances.put(sessionId, instances);
      }
    } else {
      instances = singleTimerInstances;
    }
    instances.put(instance.getJobHandle().getId(), instance);
  }