001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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 package org.apache.camel.component.jms; 018 019 import javax.jms.Message; 020 import javax.jms.MessageListener; 021 022 import org.apache.camel.Endpoint; 023 import org.apache.camel.Exchange; 024 import org.apache.camel.Processor; 025 import org.apache.camel.RuntimeCamelException; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 029 /** 030 * A JMS {@link MessageListener} which can be used to delegate processing to a 031 * Camel endpoint. 032 * 033 * @version $Revision: 563665 $ 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 075 * message 076 * 077 * @param binding the binding to use 078 */ 079 public void setBinding(JmsBinding binding) { 080 this.binding = binding; 081 } 082 }