Skip to content

Commit

Permalink
CAMEL-20187: Replace synchronized by reentrant locks
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo committed Dec 4, 2023
1 parent aa52577 commit 853934d
Show file tree
Hide file tree
Showing 30 changed files with 1,202 additions and 714 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.Closeable;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;

import org.apache.camel.CamelContext;
Expand All @@ -34,6 +35,8 @@ public class CamelContextTracker implements Closeable {

private static final List<CamelContextTracker> TRACKERS = new CopyOnWriteArrayList<>();

private static final ReentrantLock LOCK = new ReentrantLock();

@FunctionalInterface
public interface Filter extends Predicate<CamelContext> {

Expand Down Expand Up @@ -91,27 +94,37 @@ public final void close() {
TRACKERS.remove(this);
}

public static synchronized void notifyContextCreated(CamelContext camelContext) {
for (CamelContextTracker tracker : TRACKERS) {
try {
if (tracker.accept(camelContext)) {
tracker.contextCreated(camelContext);
public static void notifyContextCreated(CamelContext camelContext) {
LOCK.lock();
try {
for (CamelContextTracker tracker : TRACKERS) {
try {
if (tracker.accept(camelContext)) {
tracker.contextCreated(camelContext);
}
} catch (Exception e) {
LOG.warn("Error calling CamelContext tracker. This exception is ignored.", e);
}
} catch (Exception e) {
LOG.warn("Error calling CamelContext tracker. This exception is ignored.", e);
}
} finally {
LOCK.unlock();
}
}

public static synchronized void notifyContextDestroyed(CamelContext camelContext) {
for (CamelContextTracker tracker : TRACKERS) {
try {
if (tracker.accept(camelContext)) {
tracker.contextDestroyed(camelContext);
public static void notifyContextDestroyed(CamelContext camelContext) {
LOCK.lock();
try {
for (CamelContextTracker tracker : TRACKERS) {
try {
if (tracker.accept(camelContext)) {
tracker.contextDestroyed(camelContext);
}
} catch (Exception e) {
LOG.warn("Error calling CamelContext tracker. This exception is ignored.", e);
}
} catch (Exception e) {
LOG.warn("Error calling CamelContext tracker. This exception is ignored.", e);
}
} finally {
LOCK.unlock();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.support.service;

import java.util.concurrent.locks.ReentrantLock;

import org.apache.camel.RuntimeCamelException;
import org.apache.camel.ServiceStatus;
import org.slf4j.Logger;
Expand Down Expand Up @@ -49,12 +51,13 @@ public abstract class BaseService {
protected static final byte SHUTDOWN = 11;
protected static final byte FAILED = 12;

protected final Object lock = new Object();
protected final ReentrantLock lock = new ReentrantLock();
protected volatile byte status = NEW;

public void build() {
if (status == NEW) {
synchronized (lock) {
lock.lock();
try {
if (status == NEW) {
try (AutoCloseable ignored = doLifecycleChange()) {
doBuild();
Expand All @@ -63,14 +66,17 @@ public void build() {
}
status = BUILT;
}
} finally {
lock.unlock();
}
}
}

public void init() {
// allow to initialize again if stopped or failed
if (status <= BUILT || status >= STOPPED) {
synchronized (lock) {
lock.lock();
try {
if (status <= BUILT || status >= STOPPED) {
build();
try (AutoCloseable ignored = doLifecycleChange()) {
Expand All @@ -82,6 +88,8 @@ public void init() {
fail(e);
}
}
} finally {
lock.unlock();
}
}
}
Expand All @@ -93,7 +101,8 @@ public void init() {
* invoke the operation in a safe manner.
*/
public void start() {
synchronized (lock) {
lock.lock();
try {
if (status == STARTED) {
logger().trace("Service: {} already started", this);
return;
Expand Down Expand Up @@ -125,6 +134,8 @@ public void start() {
logger().trace("Error while starting service: {}", this, e);
fail(e);
}
} finally {
lock.unlock();
}
}

Expand All @@ -135,7 +146,8 @@ public void start() {
* invoke the operation in a safe manner.
*/
public void stop() {
synchronized (lock) {
lock.lock();
try {
if (status == FAILED) {
logger().trace("Service: {} failed and regarded as already stopped", this);
return;
Expand All @@ -158,6 +170,8 @@ public void stop() {
logger().trace("Error while stopping service: {}", this, e);
fail(e);
}
} finally {
lock.unlock();
}
}

Expand All @@ -168,7 +182,8 @@ public void stop() {
* invoke the operation in a safe manner.
*/
public void suspend() {
synchronized (lock) {
lock.lock();
try {
if (status == SUSPENDED) {
logger().trace("Service: {} already suspended", this);
return;
Expand All @@ -187,6 +202,8 @@ public void suspend() {
logger().trace("Error while suspending service: {}", this, e);
fail(e);
}
} finally {
lock.unlock();
}
}

Expand All @@ -197,7 +214,8 @@ public void suspend() {
* invoke the operation in a safe manner.
*/
public void resume() {
synchronized (lock) {
lock.lock();
try {
if (status != SUSPENDED) {
logger().trace("Service is not suspended: {}", this);
return;
Expand All @@ -212,6 +230,8 @@ public void resume() {
logger().trace("Error while resuming service: {}", this, e);
fail(e);
}
} finally {
lock.unlock();
}
}

Expand All @@ -222,7 +242,8 @@ public void resume() {
* invoke the operation in a safe manner.
*/
public void shutdown() {
synchronized (lock) {
lock.lock();
try {
if (status == SHUTDOWN) {
logger().trace("Service: {} already shutdown", this);
return;
Expand All @@ -242,6 +263,8 @@ public void shutdown() {
logger().trace("Error shutting down service: {}", this, e);
fail(e);
}
} finally {
lock.unlock();
}
}

Expand Down

0 comments on commit 853934d

Please sign in to comment.