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.component.hazelcast.seda;
018
019 import java.util.concurrent.BlockingQueue;
020 import java.util.concurrent.ExecutorService;
021 import java.util.concurrent.TimeUnit;
022
023 import org.apache.camel.AsyncCallback;
024 import org.apache.camel.AsyncProcessor;
025 import org.apache.camel.Consumer;
026 import org.apache.camel.Endpoint;
027 import org.apache.camel.Exchange;
028 import org.apache.camel.Processor;
029 import org.apache.camel.impl.DefaultConsumer;
030 import org.apache.camel.impl.DefaultExchange;
031 import org.apache.camel.impl.DefaultExchangeHolder;
032 import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
033 import org.slf4j.Logger;
034 import org.slf4j.LoggerFactory;
035
036 /**
037 * Implementation of Hazelcast SEDA {@link Consumer} component.
038 */
039 public class HazelcastSedaConsumer extends DefaultConsumer implements Runnable {
040
041 private static final Logger LOG = LoggerFactory.getLogger(HazelcastSedaConsumer.class);
042 private final HazelcastSedaEndpoint endpoint;
043 private final AsyncProcessor processor;
044 private ExecutorService executor;
045
046 public HazelcastSedaConsumer(final Endpoint endpoint, final Processor processor) {
047 super(endpoint, processor);
048 this.endpoint = (HazelcastSedaEndpoint) endpoint;
049 this.processor = AsyncProcessorTypeConverter.convert(processor);
050 }
051
052 @Override
053 protected void doStart() throws Exception {
054 int concurrentConsumers = endpoint.getConfiguration().getConcurrentConsumers();
055 executor = endpoint.getCamelContext().getExecutorServiceStrategy().newFixedThreadPool(this, endpoint.getEndpointUri(), concurrentConsumers);
056 for (int i = 0; i < concurrentConsumers; i++) {
057 executor.execute(this);
058 }
059
060 super.doStart();
061 }
062
063 @Override
064 protected void doStop() throws Exception {
065 if (executor != null) {
066 endpoint.getCamelContext().getExecutorServiceStrategy().shutdownNow(executor);
067 executor = null;
068 }
069 super.doStop();
070 }
071
072 public void run() {
073 final BlockingQueue queue = endpoint.getQueue();
074
075 while (queue != null && isRunAllowed()) {
076 final Exchange exchange = new DefaultExchange(this.getEndpoint().getCamelContext());
077
078 try {
079 final Object body = queue.poll(endpoint.getConfiguration().getPollInterval(), TimeUnit.MILLISECONDS);
080
081 if (body != null) {
082 if (body instanceof DefaultExchangeHolder) {
083 DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) body);
084 } else {
085 exchange.getIn().setBody(body);
086 }
087 try {
088 // process using the asynchronous routing engine
089 processor.process(exchange, new AsyncCallback() {
090 public void done(boolean asyncDone) {
091 // noop
092 }
093 });
094
095 if (exchange.getException() != null) {
096 getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
097 }
098
099 } catch (Exception e) {
100 LOG.error("Hzlq Exception caught: " + e, e);
101 }
102 }
103 } catch (InterruptedException e) {
104 if (LOG.isDebugEnabled()) {
105 LOG.debug("Hzlq Consumer Interrupted: " + e, e);
106 }
107 continue;
108 } catch (Throwable e) {
109 getExceptionHandler().handleException("Error processing exchange", exchange, e);
110 }
111 }
112 }
113
114 }