| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.component.seda; |
| 18 |
|
|
| 19 |
|
import java.util.concurrent.TimeUnit; |
| 20 |
|
|
| 21 |
|
import org.apache.camel.AlreadyStoppedException; |
| 22 |
|
import org.apache.camel.Consumer; |
| 23 |
|
import org.apache.camel.Exchange; |
| 24 |
|
import org.apache.camel.Processor; |
| 25 |
|
import org.apache.camel.impl.ServiceSupport; |
| 26 |
|
import org.apache.commons.logging.Log; |
| 27 |
|
import org.apache.commons.logging.LogFactory; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public class SedaConsumer<E extends Exchange> extends ServiceSupport implements Consumer<E>, Runnable { |
| 33 |
3 |
private static final Log LOG = LogFactory.getLog(SedaConsumer.class); |
| 34 |
|
|
| 35 |
|
private SedaEndpoint<E> endpoint; |
| 36 |
|
private Processor processor; |
| 37 |
|
private Thread thread; |
| 38 |
|
|
| 39 |
24 |
public SedaConsumer(SedaEndpoint<E> endpoint, Processor processor) { |
| 40 |
24 |
this.endpoint = endpoint; |
| 41 |
24 |
this.processor = processor; |
| 42 |
24 |
} |
| 43 |
|
|
| 44 |
|
@Override |
| 45 |
|
public String toString() { |
| 46 |
0 |
return "QueueConsumer: " + endpoint.getEndpointUri(); |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
public void run() { |
| 50 |
78 |
while (!isStopping()) { |
| 51 |
|
E exchange; |
| 52 |
|
try { |
| 53 |
54 |
exchange = endpoint.getQueue().poll(1000, TimeUnit.MILLISECONDS); |
| 54 |
0 |
} catch (InterruptedException e) { |
| 55 |
0 |
break; |
| 56 |
54 |
} |
| 57 |
54 |
if (exchange != null && !isStopping()) { |
| 58 |
|
try { |
| 59 |
30 |
processor.process(exchange); |
| 60 |
0 |
} catch (AlreadyStoppedException e) { |
| 61 |
0 |
LOG.debug("Ignoring failed message due to shutdown: " + e, e); |
| 62 |
0 |
break; |
| 63 |
0 |
} catch (Throwable e) { |
| 64 |
0 |
LOG.error(e); |
| 65 |
30 |
} |
| 66 |
|
} |
| 67 |
54 |
} |
| 68 |
24 |
} |
| 69 |
|
|
| 70 |
|
protected void doStart() throws Exception { |
| 71 |
24 |
thread = new Thread(this, getThreadName(endpoint.getEndpointUri())); |
| 72 |
24 |
thread.setDaemon(true); |
| 73 |
24 |
thread.start(); |
| 74 |
24 |
} |
| 75 |
|
|
| 76 |
|
protected void doStop() throws Exception { |
| 77 |
24 |
thread.join(); |
| 78 |
24 |
} |
| 79 |
|
|
| 80 |
|
} |