001    /**
002     * Copyright (c) 2000-2012 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.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.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
027    
028    import java.util.Date;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class SchedulerEventMessageListenerWrapper implements MessageListener {
034    
035            public void afterPropertiesSet() {
036                    String jobName = _className;
037    
038                    if (_className.length() > SchedulerEngine.JOB_NAME_MAX_LENGTH) {
039                            jobName = _className.substring(
040                                    0, SchedulerEngine.JOB_NAME_MAX_LENGTH);
041                    }
042    
043                    String groupName = _className;
044    
045                    if (_className.length() > SchedulerEngine.GROUP_NAME_MAX_LENGTH) {
046                            groupName = _className.substring(
047                                    0, SchedulerEngine.GROUP_NAME_MAX_LENGTH);
048                    }
049    
050                    _key = jobName.concat(StringPool.PERIOD).concat(groupName);
051    
052                    if (_messageListenerUUID == null) {
053                            _messageListenerUUID = PortalUUIDUtil.generate();
054                    }
055            }
056    
057            public String getMessageListenerUUID() {
058                    return _messageListenerUUID;
059            }
060    
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                            String receiverKey = GetterUtil.getString(
067                                    message.getString(SchedulerEngine.RECEIVER_KEY));
068    
069                            if (!receiverKey.equals(_key)) {
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                            if (message.getBoolean(SchedulerEngine.DISABLE) &&
089                                    destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
090    
091                                    MessageBusUtil.unregisterMessageListener(destinationName, this);
092                            }
093                    }
094            }
095    
096            public void setClassName(String className) {
097                    _className = className;
098            }
099    
100            public void setMessageListener(MessageListener messageListener) {
101                    _messageListener = messageListener;
102            }
103    
104            public void setMessageListenerUUID(String messageListenerUUID) {
105                    _messageListenerUUID = messageListenerUUID;
106            }
107    
108            protected void handleException(Message message, Exception exception) {
109                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
110    
111                    if (jobState != null) {
112                            jobState.addException(exception, new Date());
113                    }
114            }
115    
116            private String _className;
117            private String _key;
118            private MessageListener _messageListener;
119            private String _messageListenerUUID;
120    
121    }