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.processor.resequencer; 018 019 import java.lang.reflect.InvocationHandler; 020 import java.lang.reflect.Method; 021 import java.lang.reflect.Proxy; 022 import java.util.concurrent.BlockingQueue; 023 024 import org.apache.camel.Exchange; 025 import org.apache.camel.Processor; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 029 /** 030 * A thread that takes re-ordered {@link Exchange}s from a blocking queue and 031 * send them to the linked processor. 032 * 033 * @author Martin Krasser 034 * 035 * @version $Revision 036 */ 037 public class SequenceSender extends Thread { 038 039 private static final Log LOG = LogFactory.getLog(SequenceSender.class); 040 private static final Exchange STOP = createStopSignal(); 041 042 private BlockingQueue<Exchange> queue; 043 private Processor processor; 044 045 /** 046 * Creates a new {@link SequenceSender} thread. 047 * 048 * @param processor 049 * the processor to send re-ordered {@link Exchange}s. 050 */ 051 public SequenceSender(Processor processor) { 052 this.processor = processor; 053 } 054 055 /** 056 * Sets the {@link BlockingQueue} to take messages from. 057 * 058 * @param queue 059 * the {@link BlockingQueue} to take messages from. 060 */ 061 public void setQueue(BlockingQueue<Exchange> queue) { 062 this.queue = queue; 063 } 064 065 public void run() { 066 while (true) { 067 try { 068 Exchange exchange = queue.take(); 069 if (exchange == STOP) { 070 LOG.info("exit processing loop after cancellation"); 071 return; 072 } 073 processor.process(exchange); 074 } catch (InterruptedException e) { 075 LOG.info("exit processing loop after interrupt"); 076 return; 077 } catch (Exception e) { 078 LOG.warn("exception during exchange processing"); 079 } 080 } 081 } 082 083 /** 084 * Cancels this thread. 085 */ 086 public void cancel() throws InterruptedException { 087 queue.put(STOP); 088 } 089 090 private static Exchange createStopSignal() { 091 return (Exchange)Proxy.newProxyInstance(SequenceSender.class.getClassLoader(), 092 new Class[] {Exchange.class}, createStopHandler()); 093 } 094 095 private static InvocationHandler createStopHandler() { 096 return new InvocationHandler() { 097 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 098 throw new RuntimeException("illegal method invocation on stop signal"); 099 } 100 }; 101 } 102 103 }