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.activemq.util;
018    
019    import org.apache.activemq.broker.ConnectionContext;
020    import org.apache.activemq.broker.ProducerBrokerExchange;
021    import org.apache.activemq.command.ActiveMQDestination;
022    import org.apache.activemq.command.Message;
023    import org.apache.activemq.command.ProducerInfo;
024    import org.apache.activemq.state.ProducerState;
025    
026    /**
027     * Utility class for re-sending messages
028     *
029     */
030    public final class BrokerSupport {
031    
032        private BrokerSupport() {        
033        }
034        
035        public static void resendNoCopy(final ConnectionContext context, Message originalMessage, ActiveMQDestination deadLetterDestination) throws Exception {
036            doResend(context, originalMessage, deadLetterDestination, false);
037        }
038        
039        /**
040         * @param context
041         * @param originalMessage 
042         * @param deadLetterDestination
043         * @throws Exception
044         */
045        public static void resend(final ConnectionContext context, Message originalMessage, ActiveMQDestination deadLetterDestination) throws Exception {
046            doResend(context, originalMessage, deadLetterDestination, true);
047        }
048        
049        public static void doResend(final ConnectionContext context, Message originalMessage, ActiveMQDestination deadLetterDestination, boolean copy) throws Exception {       
050            Message message = copy ? originalMessage.copy() : originalMessage;
051            message.setOriginalDestination(message.getDestination());
052            message.setOriginalTransactionId(message.getTransactionId());
053            message.setDestination(deadLetterDestination);
054            message.setTransactionId(null);
055            message.setMemoryUsage(null);
056            boolean originalFlowControl = context.isProducerFlowControl();
057            try {
058                context.setProducerFlowControl(false);
059                ProducerInfo info = new ProducerInfo();
060                ProducerState state = new ProducerState(info);
061                ProducerBrokerExchange producerExchange = new ProducerBrokerExchange();
062                producerExchange.setProducerState(state);
063                producerExchange.setMutable(true);
064                producerExchange.setConnectionContext(context);
065                context.getBroker().send(producerExchange, message);
066            } finally {
067                context.setProducerFlowControl(originalFlowControl);
068            }
069        }
070    
071    }