Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » camel » converter » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    * http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing, software
   13    * distributed under the License is distributed on an "AS IS" BASIS,
   14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    * See the License for the specific language governing permissions and
   16    * limitations under the License.
   17    */
   18   package org.apache.activemq.camel.converter;
   19   
   20   import java.io.Serializable;
   21   
   22   import javax.jms.JMSException;
   23   import javax.jms.MessageListener;
   24   import javax.jms.Message;
   25   
   26   import org.apache.activemq.command.ActiveMQMessage;
   27   import org.apache.activemq.command.ActiveMQObjectMessage;
   28   import org.apache.activemq.command.ActiveMQTextMessage;
   29   import org.apache.camel.Converter;
   30   import org.apache.camel.Exchange;
   31   import org.apache.camel.Processor;
   32   import org.apache.camel.component.jms.JmsBinding;
   33   import org.apache.camel.component.jms.JmsEndpoint;
   34   
   35   /**
   36    * @version $Revision: 758789 $
   37    */
   38   @Converter
   39   public class ActiveMQMessageConverter {
   40       private JmsBinding binding = new JmsBinding();
   41   
   42       /**
   43        * Converts the inbound message exchange to an ActiveMQ JMS message
   44        *
   45        * @return the ActiveMQ message
   46        */
   47       @Converter
   48       public ActiveMQMessage toMessage(Exchange exchange) throws JMSException {
   49           ActiveMQMessage message = createActiveMQMessage(exchange);
   50           getBinding().appendJmsProperties(message, exchange);
   51           return message;
   52       }
   53   
   54       /**
   55        * Allows a JMS {@link MessageListener} to be converted to a Camel {@link Processor}
   56        * so that we can provide better
   57        * <a href="">Bean Integration</a> so that we can use any JMS MessageListener in
   58        * in Camel as a bean
   59        * @param listener the JMS message listener
   60        * @return a newly created Camel Processor which when invoked will invoke
   61        * {@link MessageListener#onMessage(Message)}
   62        */
   63       @Converter
   64       public Processor toProcessor(final MessageListener listener) {
   65           return new Processor() {
   66               public void process(Exchange exchange) throws Exception {
   67                   Message message = toMessage(exchange);
   68                   listener.onMessage(message);
   69               }
   70   
   71               @Override
   72               public String toString() {
   73                   return "Processor of MessageListener: " + listener;
   74               }
   75           };
   76       }
   77   
   78       private static ActiveMQMessage createActiveMQMessage(Exchange exchange) throws JMSException {
   79           Object body = exchange.getIn().getBody();
   80           if (body instanceof String) {
   81               ActiveMQTextMessage answer = new ActiveMQTextMessage();
   82               answer.setText((String) body);
   83               return answer;
   84           } else if (body instanceof Serializable) {
   85               ActiveMQObjectMessage answer = new ActiveMQObjectMessage();
   86               answer.setObject((Serializable) body);
   87               return answer;
   88           } else {
   89               return new ActiveMQMessage();
   90           }
   91   
   92       }
   93   
   94       // Properties
   95       //-------------------------------------------------------------------------
   96       public JmsBinding getBinding() {
   97           return binding;
   98       }
   99   
  100       public void setBinding(JmsBinding binding) {
  101           this.binding = binding;
  102       }
  103   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » camel » converter » [javadoc | source]