001    /**
002     * Copyright (c) 2000-present 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.kernel.scheduler.messaging;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.DestinationNames;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.messaging.MessageBusUtil;
022    import com.liferay.portal.kernel.messaging.MessageListener;
023    import com.liferay.portal.kernel.messaging.MessageListenerException;
024    import com.liferay.portal.kernel.scheduler.JobState;
025    import com.liferay.portal.kernel.scheduler.SchedulerEngine;
026    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
027    import com.liferay.portal.kernel.scheduler.SchedulerEntry;
028    import com.liferay.portal.kernel.scheduler.SchedulerException;
029    import com.liferay.portal.kernel.scheduler.StorageType;
030    import com.liferay.portal.kernel.scheduler.Trigger;
031    import com.liferay.portal.kernel.scheduler.TriggerState;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    
034    import java.util.Date;
035    
036    /**
037     * @author Shuyang Zhou
038     */
039    public class SchedulerEventMessageListenerWrapper
040            implements SchedulerEventMessageListener {
041    
042            @Override
043            public SchedulerEntry getSchedulerEntry() {
044                    return _schedulerEntry;
045            }
046    
047            @Override
048            public void receive(Message message) throws MessageListenerException {
049                    String destinationName = GetterUtil.getString(
050                            message.getString(SchedulerEngine.DESTINATION_NAME));
051                    String jobName = message.getString(SchedulerEngine.JOB_NAME);
052                    String groupName = message.getString(SchedulerEngine.GROUP_NAME);
053    
054                    if (destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
055                            Trigger trigger = _schedulerEntry.getTrigger();
056    
057                            if (!jobName.equals(trigger.getJobName()) ||
058                                    !groupName.equals(trigger.getGroupName())) {
059    
060                                    return;
061                            }
062                    }
063    
064                    try {
065                            _messageListener.receive(message);
066                    }
067                    catch (Exception e) {
068                            handleException(message, e);
069    
070                            if (e instanceof MessageListenerException) {
071                                    throw (MessageListenerException)e;
072                            }
073                            else {
074                                    throw new MessageListenerException(e);
075                            }
076                    }
077                    finally {
078                            TriggerState triggerState = null;
079    
080                            if (message.getBoolean(SchedulerEngine.DISABLE)) {
081                                    triggerState = TriggerState.COMPLETE;
082    
083                                    if (destinationName.equals(
084                                                    DestinationNames.SCHEDULER_DISPATCH)) {
085    
086                                            MessageBusUtil.unregisterMessageListener(
087                                                    destinationName, this);
088                                    }
089    
090                                    StorageType storageType = (StorageType)message.get(
091                                            SchedulerEngine.STORAGE_TYPE);
092    
093                                    try {
094                                            SchedulerEngineHelperUtil.delete(
095                                                    jobName, groupName, storageType);
096                                    }
097                                    catch (SchedulerException se) {
098                                            if (_log.isInfoEnabled()) {
099                                                    _log.info(
100                                                            "Unable to delete job " + jobName + " in group " +
101                                                                    groupName,
102                                                            se);
103                                            }
104                                    }
105                            }
106                            else {
107                                    triggerState = TriggerState.NORMAL;
108                            }
109    
110                            try {
111                                    SchedulerEngineHelperUtil.auditSchedulerJobs(
112                                            message, triggerState);
113                            }
114                            catch (Exception e) {
115                                    if (_log.isInfoEnabled()) {
116                                            _log.info("Unable to send audit message", e);
117                                    }
118                            }
119                    }
120            }
121    
122            /**
123             * @deprecated As of 7.0.0
124             */
125            @Deprecated
126            public void setGroupName(String groupName) {
127                    _groupName = groupName;
128            }
129    
130            /**
131             * @deprecated As of 7.0.0
132             */
133            @Deprecated
134            public void setJobName(String jobName) {
135                    _jobName = jobName;
136            }
137    
138            public void setMessageListener(MessageListener messageListener) {
139                    _messageListener = messageListener;
140            }
141    
142            public void setSchedulerEntry(SchedulerEntry schedulerEntry) {
143                    _schedulerEntry = schedulerEntry;
144            }
145    
146            protected void handleException(Message message, Exception exception) {
147                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
148    
149                    if (jobState != null) {
150                            jobState.addException(exception, new Date());
151                    }
152            }
153    
154            private static final Log _log = LogFactoryUtil.getLog(
155                    SchedulerEventMessageListenerWrapper.class);
156    
157            /**
158             * @deprecated As of 7.0.0
159             */
160            @Deprecated
161            @SuppressWarnings("unused")
162            private String _groupName;
163    
164            /**
165             * @deprecated As of 7.0.0
166             */
167            @Deprecated
168            @SuppressWarnings("unused")
169            private String _jobName;
170    
171            private MessageListener _messageListener;
172            private SchedulerEntry _schedulerEntry;
173    
174    }