Example #1
0
  /** 提醒操作 */
  @Override
  public void execute(JobExecutionContext context) throws JobExecutionException {
    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // 获取添加任务时的参数(时间)
    JobDataMap data = context.getJobDetail().getJobDataMap();
    Long time = data.getLong("time");
    List<Remind> reminds = new RemindService().loadByTime(time); // 数据库读取time时刻提醒
    for (int i = 0; i < reminds.size(); i++) {
      // 执行post请求微信服务器
      int id = reminds.get(i).getId();
      String response =
          Util.loadJSON("http://ly.linteng.wang/xs/home/index/send?key=wanglinteng&id=" + id);
      try {
        Util.SendLog(
            " 执行时间为:"
                + dateformat.format(new Date(time * 1000))
                + " 的定时任务,提醒ID为:【"
                + id
                + "】,返回值:"
                + response);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    Quartz.removeJob(time); // 执行完立即移除自己的触发器
  }
Example #2
0
 private long getCustomerId(SchedulingContext ctxt, Trigger t) throws JobPersistenceException {
   JobDetail jd = retrieveJob(ctxt, t.getJobName(), t.getJobGroup());
   if (jd == null) {
     return -1L;
   }
   JobDataMap jdMap = jd.getJobDataMap();
   return jdMap.getLong("customerId");
 }
  /** @see org.quartz.Job#execute(org.quartz.JobExecutionContext) */
  public void execute(JobExecutionContext context) throws JobExecutionException {
    JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
    SchedulerContext schedCtxt = null;
    try {
      schedCtxt = context.getScheduler().getContext();
    } catch (SchedulerException e) {
      throw new JobExecutionException("Error obtaining scheduler context.", e, false);
    }

    String fileName = mergedJobDataMap.getString(FILE_NAME);
    String listenerName = mergedJobDataMap.getString(FILE_SCAN_LISTENER_NAME);

    if (fileName == null) {
      throw new JobExecutionException(
          "Required parameter '" + FILE_NAME + "' not found in merged JobDataMap");
    }
    if (listenerName == null) {
      throw new JobExecutionException(
          "Required parameter '" + FILE_SCAN_LISTENER_NAME + "' not found in merged JobDataMap");
    }

    FileScanListener listener = (FileScanListener) schedCtxt.get(listenerName);

    if (listener == null) {
      throw new JobExecutionException(
          "FileScanListener named '" + listenerName + "' not found in SchedulerContext");
    }

    long lastDate = -1;
    if (mergedJobDataMap.containsKey(LAST_MODIFIED_TIME)) {
      lastDate = mergedJobDataMap.getLong(LAST_MODIFIED_TIME);
    }

    long newDate = getLastModifiedDate(fileName);

    if (newDate < 0) {
      log.warn("File '" + fileName + "' does not exist.");
      return;
    }

    if (lastDate > 0 && (newDate != lastDate)) {
      // notify call back...
      log.info("File '" + fileName + "' updated, notifying listener.");
      listener.fileUpdated(fileName);
    } else if (log.isDebugEnabled()) {
      log.debug("File '" + fileName + "' unchanged.");
    }

    // It is the JobDataMap on the JobDetail which is actually stateful
    context.getJobDetail().getJobDataMap().put(LAST_MODIFIED_TIME, newDate);
  }
 /** @throws java.lang.Exception */
 @Before
 public void setUp() throws Exception {
   job = new NotificationEventCreationJob();
   job.setConfig(config);
   job.setEndPoint(END_POINT);
   job.setIdBuilder(idBuilder);
   job.setIssuer(issuer);
   job.setProducer(producer);
   exception = new RuntimeException("Nuts!");
   inOrder = inOrder(config, idBuilder, issuer, producer, context, jobDataMap);
   when(context.getMergedJobDataMap()).thenReturn(jobDataMap);
   when(jobDataMap.getString(NotificationEventCreationJob.JOB_DATA_KEY_CONTACT_INSTANCE_ID))
       .thenReturn(EVENT_INSTANCE_ID);
   when(jobDataMap.getLong(NotificationEventCreationJob.JOB_DATA_KEY_EVENT_TIME)).thenReturn(NOW);
   when(jobDataMap.getString(NotificationEventCreationJob.JOB_DATA_KEY_EVENT_TYPE))
       .thenReturn(TYPE);
   when(jobDataMap.getString(NotificationEventCreationJob.JOB_DATA_KEY_GROUND_STATION_NAME))
       .thenReturn(GS);
   when(jobDataMap.getString(NotificationEventCreationJob.JOB_DATA_KEY_SATELLITE_NAME))
       .thenReturn(SAT);
   when(config.getEventNameSpace()).thenReturn(NAME_SPACE);
   when(idBuilder.buildID(NAME_SPACE, TYPE)).thenReturn(EVENT_ID);
   when(issuer.getID()).thenReturn(ISSUER);
 }