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.SchedulerException;
028    import com.liferay.portal.kernel.scheduler.StorageType;
029    import com.liferay.portal.kernel.scheduler.TriggerState;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
032    
033    import java.util.Date;
034    
035    /**
036     * @author Shuyang Zhou
037     */
038    public class SchedulerEventMessageListenerWrapper implements MessageListener {
039    
040            public void afterPropertiesSet() {
041                    if (_messageListenerUUID == null) {
042                            _messageListenerUUID = PortalUUIDUtil.generate();
043                    }
044            }
045    
046            public String getMessageListenerUUID() {
047                    return _messageListenerUUID;
048            }
049    
050            @Override
051            public void receive(Message message) throws MessageListenerException {
052                    String destinationName = GetterUtil.getString(
053                            message.getString(SchedulerEngine.DESTINATION_NAME));
054    
055                    if (destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
056                            String messageListenerUUID = message.getString(
057                                    SchedulerEngine.MESSAGE_LISTENER_UUID);
058    
059                            if (!_messageListenerUUID.equals(messageListenerUUID)) {
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                                    String jobName = message.getString(SchedulerEngine.JOB_NAME);
091                                    String groupName = message.getString(
092                                            SchedulerEngine.GROUP_NAME);
093                                    StorageType storageType = (StorageType)message.get(
094                                            SchedulerEngine.STORAGE_TYPE);
095    
096                                    try {
097                                            SchedulerEngineHelperUtil.delete(
098                                                    jobName, groupName, storageType);
099                                    }
100                                    catch (SchedulerException se) {
101                                            if (_log.isInfoEnabled()) {
102                                                    _log.info(
103                                                            "Unable to delete job " + jobName + " in group " +
104                                                                    groupName,
105                                                            se);
106                                            }
107                                    }
108                            }
109                            else {
110                                    triggerState = TriggerState.NORMAL;
111                            }
112    
113                            try {
114                                    SchedulerEngineHelperUtil.auditSchedulerJobs(
115                                            message, triggerState);
116                            }
117                            catch (Exception e) {
118                                    if (_log.isInfoEnabled()) {
119                                            _log.info("Unable to send audit message", e);
120                                    }
121                            }
122                    }
123            }
124    
125            /**
126             * @deprecated As of 6.2.0, replaced by {@link #setGroupName(String)}
127             */
128            @Deprecated
129            public void setClassName(String className) {
130                    _groupName = className;
131                    _jobName = className;
132            }
133    
134            /**
135             * @deprecated As of 7.0.0
136             */
137            @Deprecated
138            public void setGroupName(String groupName) {
139                    _groupName = groupName;
140            }
141    
142            /**
143             * @deprecated As of 7.0.0
144             */
145            @Deprecated
146            public void setJobName(String jobName) {
147                    _jobName = jobName;
148            }
149    
150            public void setMessageListener(MessageListener messageListener) {
151                    _messageListener = messageListener;
152            }
153    
154            public void setMessageListenerUUID(String messageListenerUUID) {
155                    _messageListenerUUID = messageListenerUUID;
156            }
157    
158            protected void handleException(Message message, Exception exception) {
159                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
160    
161                    if (jobState != null) {
162                            jobState.addException(exception, new Date());
163                    }
164            }
165    
166            private static final Log _log = LogFactoryUtil.getLog(
167                    SchedulerEventMessageListenerWrapper.class);
168    
169            /**
170             * @deprecated As of 7.0.0
171             */
172            @Deprecated
173            @SuppressWarnings("unused")
174            private String _groupName;
175    
176            /**
177             * @deprecated As of 7.0.0
178             */
179            @Deprecated
180            @SuppressWarnings("unused")
181            private String _jobName;
182    
183            private MessageListener _messageListener;
184            private String _messageListenerUUID;
185    
186    }