001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.jms;
019    
020    import javax.jms.Message;
021    import javax.jms.MessageListener;
022    
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Processor;
026    import org.apache.camel.RuntimeCamelException;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    /**
031     * A JMS {@link MessageListener} which can be used to delegate processing to a Camel endpoint.
032     *
033     * @version $Revision: 534145 $
034     */
035    public class EndpointMessageListener<E extends Exchange> implements MessageListener {
036        private static final transient Log log = LogFactory.getLog(EndpointMessageListener.class);
037        private Endpoint<E> endpoint;
038        private Processor processor;
039        private JmsBinding binding;
040    
041        public EndpointMessageListener(Endpoint<E> endpoint, Processor processor) {
042            this.endpoint = endpoint;
043            this.processor = processor;
044        }
045    
046        public void onMessage(Message message) {
047            try {
048                            
049                    if (log.isDebugEnabled()) {
050                                log.debug(endpoint + " receiving JMS message: " + message);
051                            }
052                            JmsExchange exchange = createExchange(message);
053                            processor.process((E) exchange);
054                            
055                    } catch (Exception e) {
056                            throw new RuntimeCamelException(e);
057                    }
058        }
059    
060        public JmsExchange createExchange(Message message) {
061            return new JmsExchange(endpoint.getContext(), getBinding(), message);
062        }
063    
064        // Properties
065        //-------------------------------------------------------------------------
066        public JmsBinding getBinding() {
067            if (binding == null) {
068                binding = new JmsBinding();
069            }
070            return binding;
071        }
072    
073        /**
074         * Sets the binding used to convert from a Camel message to and from a JMS message
075         *
076         * @param binding the binding to use
077         */
078        public void setBinding(JmsBinding binding) {
079            this.binding = binding;
080        }
081    }