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 TimerJobInstance createTimerJobInstance(
      Job job,
      JobContext ctx,
      Trigger trigger,
      JobHandle handle,
      InternalSchedulerService scheduler) {
    Map<Long, TimerJobInstance> local = null;
    if (ctx instanceof ProcessJobContext) {
      int sessionId = ((ProcessJobContext) ctx).getSessionId();
      Map<Long, TimerJobInstance> instances = timerInstances.get(sessionId);
      if (instances == null) {
        instances = new ConcurrentHashMap<Long, TimerJobInstance>();
        timerInstances.put(sessionId, instances);
      }
      local = timerInstances.get(sessionId);
    } else {
      local = singleTimerInstances;
    }
    ctx.setJobHandle(handle);
    GlobalJpaTimerJobInstance jobInstance =
        new GlobalJpaTimerJobInstance(
            new SelfRemovalJob(job),
            new SelfRemovalJobContext(ctx, local),
            trigger,
            handle,
            scheduler);

    return jobInstance;
  }
  public TimerJobInstance createTimerJobInstance(
      Job job,
      JobContext ctx,
      Trigger trigger,
      JobHandle handle,
      InternalSchedulerService scheduler) {
    ctx.setJobHandle(handle);

    return new JpaTimerJobInstance(
        new SelfRemovalJob(job), createJobContext(ctx), trigger, handle, scheduler);
  }