1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.scheduler.job;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.messaging.Message;
20  import com.liferay.portal.kernel.messaging.MessageBusUtil;
21  import com.liferay.portal.kernel.scheduler.SchedulerEngine;
22  
23  import java.util.Date;
24  
25  import org.quartz.Job;
26  import org.quartz.JobDataMap;
27  import org.quartz.JobDetail;
28  import org.quartz.JobExecutionContext;
29  
30  /**
31   * <a href="MessageSenderJob.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Michael C. Han
34   * @author Bruno Farache
35   */
36  public class MessageSenderJob implements Job {
37  
38      public void execute(JobExecutionContext jobExecutionContext) {
39          try {
40              JobDetail jobDetail = jobExecutionContext.getJobDetail();
41  
42              JobDataMap jobDataMap = jobDetail.getJobDataMap();
43  
44              String destination = jobDataMap.getString(
45                  SchedulerEngine.DESTINATION);
46              Message message = (Message)jobDataMap.get(SchedulerEngine.MESSAGE);
47  
48              if (message == null) {
49                  message = new Message();
50              }
51  
52              Date scheduledFireTime = jobExecutionContext.getScheduledFireTime();
53  
54              message.put("scheduledFireTime", scheduledFireTime);
55  
56              if (jobExecutionContext.getNextFireTime() == null) {
57                  message.put(SchedulerEngine.DISABLE, true);
58              }
59  
60              MessageBusUtil.sendMessage(destination, message);
61          }
62          catch (Exception e) {
63              _log.error("Unable to execute job", e);
64          }
65      }
66  
67      private static Log _log = LogFactoryUtil.getLog(MessageSenderJob.class);
68  
69  }