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.listener;
018
019 import org.apache.camel.Exchange;
020 import org.apache.camel.component.hazelcast.HazelcastComponentHelper;
021 import org.apache.camel.component.hazelcast.HazelcastConstants;
022 import org.apache.camel.component.hazelcast.HazelcastDefaultConsumer;
023
024 public class CamelListener {
025
026 private final String cacheName;
027 private final HazelcastDefaultConsumer consumer;
028
029 public CamelListener(HazelcastDefaultConsumer consumer, String cacheName) {
030 super();
031 this.cacheName = cacheName;
032 this.consumer = consumer;
033 }
034
035 protected void sendExchange(String operation, String key, Object value) {
036 Exchange exchange = consumer.getEndpoint().createExchange();
037
038 // set object to body
039 exchange.getOut().setBody(value);
040
041 // set headers
042 if (key != null) {
043 exchange.getOut().setHeader(HazelcastConstants.OBJECT_ID, key);
044 }
045
046 HazelcastComponentHelper.setListenerHeaders(exchange, HazelcastConstants.CACHE_LISTENER, operation, cacheName);
047
048 try {
049 consumer.getProcessor().process(exchange);
050 } catch (Exception e) {
051 if (exchange.getException() != null) {
052 consumer.getExceptionHandler().handleException(String.format("Error processing exchange for hazelcast consumer on object '%s' in cache '%s'.", key, cacheName), exchange,
053 exchange.getException());
054 }
055 }
056 }
057
058 public String getCacheName() {
059 return cacheName;
060 }
061
062 public HazelcastDefaultConsumer getConsumer() {
063 return consumer;
064 }
065
066 }