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 */ 017package org.apache.activemq.camel.component.broker; 018 019import java.util.Map; 020 021import org.apache.activemq.broker.ProducerBrokerExchange; 022import org.apache.activemq.command.ActiveMQMessage; 023import org.apache.camel.AsyncCallback; 024import org.apache.camel.Exchange; 025import org.apache.camel.Message; 026import org.apache.camel.component.jms.JmsMessage; 027import org.apache.camel.converter.ObjectConverter; 028import org.apache.camel.impl.DefaultAsyncProducer; 029 030public class BrokerProducer extends DefaultAsyncProducer { 031 private final BrokerEndpoint brokerEndpoint; 032 033 public BrokerProducer(BrokerEndpoint endpoint) { 034 super(endpoint); 035 brokerEndpoint = endpoint; 036 } 037 038 @Override 039 public boolean process(Exchange exchange, AsyncCallback callback) { 040 try { 041 //In the middle of the broker - InOut doesn't make any sense 042 //so we do in only 043 return processInOnly(exchange, callback); 044 } catch (Throwable e) { 045 // must catch exception to ensure callback is invoked as expected 046 // to let Camel error handling deal with this 047 exchange.setException(e); 048 callback.done(true); 049 return true; 050 } 051 } 052 053 protected boolean processInOnly(final Exchange exchange, final AsyncCallback callback) { 054 try { 055 ActiveMQMessage message = getMessage(exchange); 056 if (message != null) { 057 message.setDestination(brokerEndpoint.getDestination()); 058 //if the ProducerBrokerExchange is null the broker will create it 059 ProducerBrokerExchange producerBrokerExchange = (ProducerBrokerExchange) exchange.getProperty(BrokerEndpoint.PRODUCER_BROKER_EXCHANGE); 060 061 brokerEndpoint.inject(producerBrokerExchange, message); 062 } 063 } catch (Exception e) { 064 exchange.setException(e); 065 } 066 callback.done(true); 067 return true; 068 } 069 070 private ActiveMQMessage getMessage(Exchange exchange) throws Exception { 071 ActiveMQMessage result; 072 Message camelMessage; 073 if (exchange.hasOut()) { 074 camelMessage = exchange.getOut(); 075 } else { 076 camelMessage = exchange.getIn(); 077 } 078 079 Map<String, Object> headers = camelMessage.getHeaders(); 080 081 /** 082 * We purposely don't want to support injecting messages half-way through 083 * broker processing - use the activemq camel component for that - but 084 * we will support changing message headers and destinations 085 */ 086 if (camelMessage instanceof JmsMessage) { 087 JmsMessage jmsMessage = (JmsMessage) camelMessage; 088 if (jmsMessage.getJmsMessage() instanceof ActiveMQMessage) { 089 result = (ActiveMQMessage) jmsMessage.getJmsMessage(); 090 //lets apply any new message headers 091 setJmsHeaders(result, headers); 092 } else { 093 throw new IllegalStateException("Not the original message from the broker " + jmsMessage.getJmsMessage()); 094 } 095 } else { 096 throw new IllegalStateException("Not the original message from the broker " + camelMessage); 097 } 098 099 return result; 100 } 101 102 private void setJmsHeaders(ActiveMQMessage message, Map<String, Object> headers) { 103 message.setReadOnlyProperties(false); 104 for (Map.Entry<String, Object> entry : headers.entrySet()) { 105 if (entry.getKey().equalsIgnoreCase("JMSDeliveryMode")) { 106 Object value = entry.getValue(); 107 if (value instanceof Number) { 108 Number number = (Number) value; 109 message.setJMSDeliveryMode(number.intValue()); 110 } 111 } 112 if (entry.getKey().equalsIgnoreCase("JmsPriority")) { 113 Integer value = ObjectConverter.toInteger(entry.getValue()); 114 if (value != null) { 115 message.setJMSPriority(value.intValue()); 116 } 117 } 118 if (entry.getKey().equalsIgnoreCase("JMSTimestamp")) { 119 Long value = ObjectConverter.toLong(entry.getValue()); 120 if (value != null) { 121 message.setJMSTimestamp(value.longValue()); 122 } 123 } 124 if (entry.getKey().equalsIgnoreCase("JMSExpiration")) { 125 Long value = ObjectConverter.toLong(entry.getValue()); 126 if (value != null) { 127 message.setJMSExpiration(value.longValue()); 128 } 129 } 130 if (entry.getKey().equalsIgnoreCase("JMSRedelivered")) { 131 message.setJMSRedelivered(ObjectConverter.toBool(entry.getValue())); 132 } 133 if (entry.getKey().equalsIgnoreCase("JMSType")) { 134 Object value = entry.getValue(); 135 if (value != null) { 136 message.setJMSType(value.toString()); 137 } 138 } 139 } 140 } 141 142}