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