Skip to content

Commit

Permalink
use holder singleton instantiation pattern to avoid possible multiple…
Browse files Browse the repository at this point in the history
… with double check locking

Signed-off-by: olivier lamy <olamy@apache.org>
  • Loading branch information
olamy committed Sep 21, 2018
1 parent dc8a512 commit 1eb2a6e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -17,6 +17,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.level>8</java.level>
</properties>

<scm>
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/org/jenkinsci/plugins/pubsub/PubsubBus.java
Expand Up @@ -46,7 +46,6 @@ public abstract class PubsubBus implements ExtensionPoint {

private static final Logger LOGGER = Logger.getLogger(PubsubBus.class.getName());

private static PubsubBus pubsubBus;
private static List<AbstractChannelSubscriber> autoSubscribers = new CopyOnWriteArrayList<>();

static {
Expand All @@ -56,25 +55,22 @@ public void run() {
try {
SyncQueueListener.shutdown();
} finally {
if (pubsubBus != null) {
if (Holder.pubsubBus != null) {
try {
unregisterAutoChannelSubscribers(pubsubBus);
unregisterAutoChannelSubscribers(Holder.pubsubBus);
} finally {
pubsubBus.shutdown();
Holder.pubsubBus.shutdown();
}
}
}
}
});
}

/**
* Get the installed {@link PubsubBus} implementation.
* @return The installed {@link PubsubBus} implementation, or default
* implementation if none are found.
*/
public synchronized static @Nonnull PubsubBus getBus() {
if (pubsubBus == null) {

private static final class Holder {
static final PubsubBus pubsubBus;

static {
ExtensionList<PubsubBus> installedBusImpls = ExtensionList.lookup(PubsubBus.class);
if (!installedBusImpls.isEmpty()) {
pubsubBus = installedBusImpls.get(0);
Expand All @@ -92,7 +88,16 @@ public void onChange() {
}
});
}
return pubsubBus;
}


/**
* Get the installed {@link PubsubBus} implementation.
* @return The installed {@link PubsubBus} implementation, or default
* implementation if none are found.
*/
public synchronized static @Nonnull PubsubBus getBus() {
return Holder.pubsubBus;
}

/**
Expand Down

0 comments on commit 1eb2a6e

Please sign in to comment.