001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }