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