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.TriggerState;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
030    
031    import java.util.Date;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class SchedulerEventMessageListenerWrapper implements MessageListener {
037    
038            public void afterPropertiesSet() {
039                    if (_jobName.length() > SchedulerEngine.GROUP_NAME_MAX_LENGTH) {
040                            _jobName = _jobName.substring(
041                                    0, SchedulerEngine.GROUP_NAME_MAX_LENGTH);
042                    }
043    
044                    if (_groupName.length() > SchedulerEngine.JOB_NAME_MAX_LENGTH) {
045                            _groupName = _groupName.substring(
046                                    0, SchedulerEngine.JOB_NAME_MAX_LENGTH);
047                    }
048    
049                    _receiverKey = new ReceiverKey(_jobName, _groupName);
050    
051                    if (_messageListenerUUID == null) {
052                            _messageListenerUUID = PortalUUIDUtil.generate();
053                    }
054            }
055    
056            public String getMessageListenerUUID() {
057                    return _messageListenerUUID;
058            }
059    
060            @Override
061            public void receive(Message message) throws MessageListenerException {
062                    String destinationName = GetterUtil.getString(
063                            message.getString(SchedulerEngine.DESTINATION_NAME));
064    
065                    if (destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
066                            ReceiverKey receiverKey = (ReceiverKey)message.get(
067                                    SchedulerEngine.RECEIVER_KEY);
068    
069                            if (!_receiverKey.equals(receiverKey)) {
070                                    return;
071                            }
072                    }
073    
074                    try {
075                            _messageListener.receive(message);
076                    }
077                    catch (Exception e) {
078                            handleException(message, e);
079    
080                            if (e instanceof MessageListenerException) {
081                                    throw (MessageListenerException)e;
082                            }
083                            else {
084                                    throw new MessageListenerException(e);
085                            }
086                    }
087                    finally {
088                            TriggerState triggerState = null;
089    
090                            if (message.getBoolean(SchedulerEngine.DISABLE)) {
091                                    triggerState = TriggerState.COMPLETE;
092    
093                                    if (destinationName.equals(
094                                                    DestinationNames.SCHEDULER_DISPATCH)) {
095    
096                                            MessageBusUtil.unregisterMessageListener(
097                                                    destinationName, this);
098                                    }
099                            }
100                            else {
101                                    triggerState = TriggerState.NORMAL;
102                            }
103    
104                            try {
105                                    SchedulerEngineHelperUtil.auditSchedulerJobs(
106                                            message, triggerState);
107                            }
108                            catch (Exception e) {
109                                    if (_log.isInfoEnabled()) {
110                                            _log.info("Unable to send audit message", e);
111                                    }
112                            }
113                    }
114            }
115    
116            /**
117             * @deprecated As of 6.2.0, replaced by {@link #setGroupName(String)}
118             */
119            @Deprecated
120            public void setClassName(String className) {
121                    _groupName = className;
122                    _jobName = className;
123            }
124    
125            public void setGroupName(String groupName) {
126                    _groupName = groupName;
127            }
128    
129            public void setJobName(String jobName) {
130                    _jobName = jobName;
131            }
132    
133            public void setMessageListener(MessageListener messageListener) {
134                    _messageListener = messageListener;
135            }
136    
137            public void setMessageListenerUUID(String messageListenerUUID) {
138                    _messageListenerUUID = messageListenerUUID;
139            }
140    
141            protected void handleException(Message message, Exception exception) {
142                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
143    
144                    if (jobState != null) {
145                            jobState.addException(exception, new Date());
146                    }
147            }
148    
149            private static final Log _log = LogFactoryUtil.getLog(
150                    SchedulerEventMessageListenerWrapper.class);
151    
152            private String _groupName;
153            private String _jobName;
154            private MessageListener _messageListener;
155            private String _messageListenerUUID;
156            private ReceiverKey _receiverKey;
157    
158    }