Use AtomicInteger instead of volatile int
Change-Id: Iaa9f341b394ea4617dd6431e75ed314bd546ce96
diff --git a/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/session/type/AMQPSession.java b/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/session/type/AMQPSession.java
index 27acc6f..0fa964b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/session/type/AMQPSession.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/session/type/AMQPSession.java
@@ -34,6 +34,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
+import java.util.concurrent.atomic.AtomicInteger;
public final class AMQPSession implements Session {
@@ -86,8 +87,8 @@
private final Properties properties;
private volatile Connection connection;
private volatile Channel channel;
- private volatile int failureCount = 0;
+ private final AtomicInteger failureCount = new AtomicInteger(0);
private final ShutdownListener connectionListener = new ShutdownListenerImpl(Connection.class);
private final ShutdownListener channelListener = new ShutdownListenerImpl(Channel.class);
@@ -115,13 +116,13 @@
try {
ch = connection.createChannel();
ch.addShutdownListener(channelListener);
- failureCount = 0;
+ failureCount.set(0);
LOGGER.info(MSG("Channel #{} opened."), ch.getChannelNumber());
} catch (Exception ex) {
LOGGER.warn(MSG("Failed to open channel."));
- failureCount++;
+ failureCount.incrementAndGet();
}
- if (failureCount > properties.getSection(Monitor.class).failureCount) {
+ if (failureCount.get() > properties.getSection(Monitor.class).failureCount) {
LOGGER.warn("Connection has something wrong. So will be disconnected.");
disconnect();
}