001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.activemq.util;
019    
020    import javax.jms.ExceptionListener;
021    import javax.jms.JMSException;
022    import javax.jms.Message;
023    import javax.jms.MessageListener;
024    
025    /**
026     * A helper base class which makes writing message listeners easier without
027     * having to worry about handling the JMSException on the onMessage() method.
028     * By default the JMS ExceptionListener will be used to handle any JMS exceptions
029     * or if none is configured then a runtime exception will be generated.
030     *
031     * @author James Strachan
032     * @version $Revision: 1.1.1.1 $
033     */
034    public abstract class MessageListenerSupport implements MessageListener {
035        private ExceptionListener exceptionListener;
036    
037        public void onMessage(Message message) {
038            try {
039                processMessage(message);
040            }
041            catch (JMSException e) {
042                onJMSException(e, message);
043            }
044            catch (Exception e) {
045                // lets wrap the exception
046                JMSException jmsEx = JMSExceptionHelper.newJMSException(e.getMessage(), e);
047                onJMSException(jmsEx, message);
048            }
049        }
050    
051        public ExceptionListener getExceptionListener() {
052            return exceptionListener;
053        }
054    
055        public void setExceptionListener(ExceptionListener exceptionListener) {
056            this.exceptionListener = exceptionListener;
057        }
058    
059        /**
060         * This method processes the incoming message possibly throwing a JMSException
061         * if the message could not be processed correctly.
062         *
063         * @param messsage
064         * @throws Exception
065         */
066        protected abstract void processMessage(Message messsage) throws Exception;
067    
068        /**
069         * Process the JMS exception either by calling an exception listener
070         * which can contian custom logic or by throwing a runtime exception
071         *
072         * @param e
073         * @param message
074         */
075        protected void onJMSException(JMSException e, Message message) {
076            if (exceptionListener != null) {
077                exceptionListener.onException(e);
078            }
079            else {
080                throw new RuntimeException("Failed to process message: " + message + " Reason: " + e, e);
081            }
082        }
083    
084    }