camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject [camel] 03/03: Simplify locking in GenericFileProducer
Date Wed, 07 Nov 2018 08:41:18 GMT
This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6cffd78c7c5ce4fadb1de72a20d0f0fe53f1e70f
Author: Guillaume Nodet <gnodet@gmail.com>
AuthorDate: Wed Nov 7 09:39:32 2018 +0100

    Simplify locking in GenericFileProducer
---
 .../org/apache/camel/component/file/GenericFileProducer.java | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
index 4b01f58..144fca6 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
+import java.util.Collections;
 import java.util.Map;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -41,7 +42,7 @@ public class GenericFileProducer<T> extends DefaultProducer {
     protected final GenericFileEndpoint<T> endpoint;
     protected GenericFileOperations<T> operations;
     // assume writing to 100 different files concurrently at most for the same file producer
-    private final Map<String, Lock> locks = LRUCacheFactory.newLRUCache(100);
+    private final Map<String, Lock> locks = Collections.synchronizedMap(LRUCacheFactory.newLRUCache(100));
 
     protected GenericFileProducer(GenericFileEndpoint<T> endpoint, GenericFileOperations<T>
operations) {
         super(endpoint);
@@ -66,14 +67,7 @@ public class GenericFileProducer<T> extends DefaultProducer {
 
         // use lock for same file name to avoid concurrent writes to the same file
         // for example when you concurrently append to the same file
-        Lock lock;
-        synchronized (locks) {
-            lock = locks.get(target);
-            if (lock == null) {
-                lock = new ReentrantLock();
-                locks.put(target, lock);
-            }
-        }
+        Lock lock = locks.computeIfAbsent(target, f -> new ReentrantLock());
 
         lock.lock();
         try {


Mime
View raw message