Skip to content

Commit

Permalink
Merge pull request #2494 from microsoft/hanli/fixes
Browse files Browse the repository at this point in the history
Fix logic for load messager and remove duplicate codes
  • Loading branch information
wangmingliang-ms authored Oct 23, 2024
2 parents c312cbd + f632885 commit 47513bf
Showing 1 changed file with 2 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static final class Holder {
private static IAzureMessager defaultMessager = null;

@Nonnull
private static IAzureMessager loadMessager() {
public static IAzureMessager loadMessager() {
final ClassLoader current = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(AzureMessager.class.getClassLoader());
Expand All @@ -44,28 +44,11 @@ public static void setDefaultMessager(@Nonnull final IAzureMessager defaultMessa
@Nonnull
public static synchronized IAzureMessager getDefaultMessager() {
if (Holder.defaultMessager == null) {
Holder.defaultMessager = loadMessager();
Holder.defaultMessager = Holder.loadMessager();
}
return Holder.defaultMessager;
}

@Nullable
private static IAzureMessager loadMessager() {
final ClassLoader current = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(AzureMessager.class.getClassLoader());
// don't use "IAzureMessager" as SPI interface to be compatible with IntelliJ's "Service" mechanism.
final ServiceLoader<AzureMessagerProvider> loader = ServiceLoader.load(AzureMessagerProvider.class, AzureMessager.class.getClassLoader());
final Iterator<AzureMessagerProvider> iterator = loader.iterator();
if (iterator.hasNext()) {
return iterator.next().getMessager();
}
return null;
} finally {
Thread.currentThread().setContextClassLoader(current);
}
}

@Nonnull
public static IAzureMessager getMessager() {
return OperationContext.current().getMessager();
Expand Down

0 comments on commit 47513bf

Please sign in to comment.