001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.scheduler.job;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.Message;
020    import com.liferay.portal.kernel.messaging.MessageBusUtil;
021    import com.liferay.portal.kernel.scheduler.SchedulerEngine;
022    
023    import java.util.Date;
024    
025    import org.quartz.Job;
026    import org.quartz.JobDataMap;
027    import org.quartz.JobDetail;
028    import org.quartz.JobExecutionContext;
029    
030    /**
031     * @author Michael C. Han
032     * @author Bruno Farache
033     */
034    public class MessageSenderJob implements Job {
035    
036            public void execute(JobExecutionContext jobExecutionContext) {
037                    try {
038                            JobDetail jobDetail = jobExecutionContext.getJobDetail();
039    
040                            JobDataMap jobDataMap = jobDetail.getJobDataMap();
041    
042                            String destination = jobDataMap.getString(
043                                    SchedulerEngine.DESTINATION);
044                            Message message = (Message)jobDataMap.get(SchedulerEngine.MESSAGE);
045    
046                            if (message == null) {
047                                    message = new Message();
048                            }
049    
050                            Date scheduledFireTime = jobExecutionContext.getScheduledFireTime();
051    
052                            message.put("scheduledFireTime", scheduledFireTime);
053    
054                            if (jobExecutionContext.getNextFireTime() == null) {
055                                    message.put(SchedulerEngine.DISABLE, true);
056                            }
057    
058                            MessageBusUtil.sendMessage(destination, message);
059                    }
060                    catch (Exception e) {
061                            _log.error("Unable to execute job", e);
062                    }
063            }
064    
065            private static Log _log = LogFactoryUtil.getLog(MessageSenderJob.class);
066    
067    }