lburgazzoli commented on a change in pull request #3993: URL: https://github.com/apache/camel/pull/3993#discussion_r453236039
########## File path: components/camel-redis/src/main/java/org/apache/camel/component/redis/processor/aggregate/RedisAggregationRepository.java ########## @@ -0,0 +1,338 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.component.redis.processor.aggregate; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.spi.OptimisticLockingAggregationRepository; +import org.apache.camel.spi.RecoverableAggregationRepository; +import org.apache.camel.spi.OptimisticLockingAggregationRepository.OptimisticLockingException; +import org.apache.camel.support.DefaultExchange; +import org.apache.camel.support.DefaultExchangeHolder; +import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.util.StringHelper; +import org.redisson.Redisson; +import org.redisson.api.RLock; +import org.redisson.api.RMap; +import org.redisson.api.RTransaction; +import org.redisson.api.RedissonClient; +import org.redisson.api.TransactionOptions; +import org.redisson.config.Config; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RedisAggregationRepository extends ServiceSupport + implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository { + private static final Logger LOG = LoggerFactory.getLogger(RedisAggregationRepository.class.getName()); + private static final String COMPLETED_SUFFIX = "-completed"; + + private boolean optimistic; + private boolean useRecovery = true; + private Map<String, DefaultExchangeHolder> cache; + private Map<String, DefaultExchangeHolder> persistedCache; + private String endpoint; + private String mapName; + private String persistenceMapName; + private RedissonClient redisson; + private String deadLetterChannel; + private long recoveryInterval = 5000; + private int maximumRedeliveries = 3; + private boolean allowSerializedHeaders; + + public RedisAggregationRepository(final String mapName, final String endpoint) { + this.mapName = mapName; + this.persistenceMapName = String.format("%s%s", mapName, COMPLETED_SUFFIX); + this.optimistic = false; + this.endpoint = endpoint; + } + + public RedisAggregationRepository(final String mapName, final String persistenceMapName, + final String endpoint) { + this.mapName = mapName; + this.persistenceMapName = persistenceMapName; + this.optimistic = false; + this.endpoint = endpoint; + } + + public RedisAggregationRepository(final String mapName, final String endpoint, boolean optimistic) { + this(mapName, endpoint); + this.optimistic = optimistic; + } + + public RedisAggregationRepository(final String mapName, final String persistenceMapName, + final String endpoint, boolean optimistic) { + this(mapName, persistenceMapName, endpoint); + this.optimistic = optimistic; + } + + @Override + public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange) throws OptimisticLockingException { + if (!optimistic) { + throw new UnsupportedOperationException(); + } + LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(), key); + if (oldExchange == null) { + DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders); + final DefaultExchangeHolder misbehaviorHolder = cache.putIfAbsent(key, holder); + if (misbehaviorHolder != null) { + Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder); + LOG.error("Optimistic locking failed for exchange with key {}: IMap#putIfAbsend returned Exchange with ID {}, while it's expected no exchanges to be returned", + key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>"); + throw new OptimisticLockingException(); + } + } else { + DefaultExchangeHolder oldHolder = DefaultExchangeHolder.marshal(oldExchange, true, allowSerializedHeaders); + DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders); + if (!cache.replace(key, oldHolder, newHolder)) { + LOG.error("Optimistic locking failed for exchange with key {}: IMap#replace returned no Exchanges, while it's expected to replace one", + key); + throw new OptimisticLockingException(); + } + } + LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key); + return oldExchange; + } + + @Override + public Exchange add(CamelContext camelContext, String key, Exchange exchange) { + if (optimistic) { + throw new UnsupportedOperationException(); + } + LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key); + Config config = new Config(); + config.useSingleServer().setAddress(String.format("redis://%s",endpoint)); + RedissonClient redisson = Redisson.create(config); Review comment: The global `redisson` instance is created by `onStart` so I guess this code can be replaced by using the shared instance ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
