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.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.sender.SingleDestinationMessageSender;
020    
021    /**
022     * @author Michael C. Han
023     * @author Brian Wing Shun Chan
024     */
025    public abstract class BaseMessageStatusMessageListener
026            implements MessageListener {
027    
028            @Override
029            public void receive(Message message) {
030                    MessageStatus messageStatus = new MessageStatus();
031    
032                    messageStatus.startTimer();
033    
034                    try {
035                            doReceive(message, messageStatus);
036                    }
037                    catch (Exception e) {
038                            _log.error(
039                                    "Unable to process request " + message.getDestinationName(), e);
040    
041                            messageStatus.setException(e);
042                    }
043                    finally {
044                            messageStatus.stopTimer();
045    
046                            _statusSender.send(messageStatus);
047                    }
048            }
049    
050            public void setStatusSender(SingleDestinationMessageSender statusSender) {
051                    _statusSender = statusSender;
052            }
053    
054            protected abstract void doReceive(
055                            Message message, MessageStatus messageStatus)
056                    throws Exception;
057    
058            private static final Log _log = LogFactoryUtil.getLog(
059                    BaseMessageStatusMessageListener.class);
060    
061            private SingleDestinationMessageSender _statusSender;
062    
063    }