Skip to content

Commit

Permalink
feat: Mixer lazy load
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinten10 committed Dec 7, 2021
1 parent 9bc627e commit 6b2964c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,4 @@ public ConfigurationHooks provideConfigurationHooks() {
public TelemetryHooks provideTelemetryHooks() {
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package group.rxcloud.capa.infrastructure.hook;

import group.rxcloud.capa.infrastructure.CapaClassLoader;
import io.vavr.Lazy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,15 +31,15 @@ public abstract class Mixer {

private static final Logger logger = LoggerFactory.getLogger(Mixer.class);

private static ConfigurationHooks configurationHooks;
private static TelemetryHooks telemetryHooks;
private static Lazy<ConfigurationHooks> configurationHooks;
private static Lazy<TelemetryHooks> telemetryHooks;

static {
try {
MixerProvider mixerProvider = CapaClassLoader.loadInfrastructureClassObj("mixer", MixerProvider.class);
if (mixerProvider != null) {
registerConfigurationHooks(mixerProvider.provideConfigurationHooks());
registerTelemetryHooks(mixerProvider.provideTelemetryHooks());
registerConfigurationHooks(Lazy.of(mixerProvider::provideConfigurationHooks));
registerTelemetryHooks(Lazy.of(mixerProvider::provideTelemetryHooks));
}
} catch (Exception e) {
logger.info("[CapaMixer] load empty mixer. expected error: ", e);
Expand Down Expand Up @@ -70,7 +71,7 @@ public interface MixerProvider {
*
* @param configurationHooks the configuration hooks
*/
private static void registerConfigurationHooks(ConfigurationHooks configurationHooks) {
private static void registerConfigurationHooks(Lazy<ConfigurationHooks> configurationHooks) {
Mixer.configurationHooks = configurationHooks;
}

Expand All @@ -79,7 +80,7 @@ private static void registerConfigurationHooks(ConfigurationHooks configurationH
*
* @param telemetryHooks the telemetry hooks
*/
private static void registerTelemetryHooks(TelemetryHooks telemetryHooks) {
private static void registerTelemetryHooks(Lazy<TelemetryHooks> telemetryHooks) {
Mixer.telemetryHooks = telemetryHooks;
}

Expand All @@ -90,7 +91,9 @@ private static void registerTelemetryHooks(TelemetryHooks telemetryHooks) {
*/
@Nullable
public static ConfigurationHooks configurationHooks() {
return configurationHooks;
return configurationHooks != null
? configurationHooks.get()
: null;
}

/**
Expand All @@ -99,7 +102,7 @@ public static ConfigurationHooks configurationHooks() {
* @return the configuration hooks
*/
public static Optional<ConfigurationHooks> configurationHooksNullable() {
return Optional.ofNullable(configurationHooks);
return Optional.ofNullable(configurationHooks.get());
}

/**
Expand All @@ -109,7 +112,9 @@ public static Optional<ConfigurationHooks> configurationHooksNullable() {
*/
@Nullable
public static TelemetryHooks telemetryHooks() {
return telemetryHooks;
return telemetryHooks != null
? telemetryHooks.get()
: null;
}

/**
Expand All @@ -118,6 +123,6 @@ public static TelemetryHooks telemetryHooks() {
* @return the telemetry hooks
*/
public static Optional<TelemetryHooks> telemetryHooksNullable() {
return Optional.ofNullable(telemetryHooks);
return Optional.ofNullable(telemetryHooks.get());
}
}

0 comments on commit 6b2964c

Please sign in to comment.