Skip to content

Commit 1484e70

Browse files
committed
Merge pull request #711 from eirslett/feature/use-archaius-for-plugin-lookup
Use Archaius for Hystrix plugin setup
2 parents 712fef3 + 82b8e6c commit 1484e70

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

hystrix-core/src/main/java/com/netflix/hystrix/strategy/HystrixPlugins.java

+39-35
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package com.netflix.hystrix.strategy;
1717

18+
import java.io.IOException;
1819
import java.util.concurrent.atomic.AtomicReference;
1920

21+
import com.netflix.config.ConfigurationManager;
22+
import com.netflix.config.DynamicPropertyFactory;
2023
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
2124
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
2225
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
@@ -33,7 +36,7 @@
3336
* Registry for plugin implementations that allows global override and handles the retrieval of correct implementation based on order of precedence:
3437
* <ol>
3538
* <li>plugin registered globally via <code>register</code> methods in this class</li>
36-
* <li>plugin registered and retrieved using {@link java.lang.System#getProperty(String)} (see get methods for property names)</li>
39+
* <li>plugin registered and retrieved using Archaius (see get methods for property names)</li>
3740
* <li>default implementation</li>
3841
* </ol>
3942
* See the Hystrix GitHub Wiki for more information: <a href="https://github.com/Netflix/Hystrix/wiki/Plugins">https://github.com/Netflix/Hystrix/wiki/Plugins</a>.
@@ -49,7 +52,12 @@ public class HystrixPlugins {
4952
/* package */ final AtomicReference<HystrixCommandExecutionHook> commandExecutionHook = new AtomicReference<>();
5053

5154
private HystrixPlugins() {
52-
55+
try {
56+
// Load configuration from hystrix-plugins.properties, if that file exists
57+
ConfigurationManager.loadCascadedPropertiesFromResources("hystrix-plugins");
58+
} catch (IOException e) {
59+
// fail silently
60+
}
5361
}
5462

5563
public static HystrixPlugins getInstance() {
@@ -71,21 +79,21 @@ public static void reset() {
7179
/**
7280
* Retrieve instance of {@link HystrixEventNotifier} to use based on order of precedence as defined in {@link HystrixPlugins} class header.
7381
* <p>
74-
* Override default by using {@link #registerEventNotifier(HystrixEventNotifier)} or setting property: <code>hystrix.plugin.HystrixEventNotifier.implementation</code> with the full classname to
82+
* Override default by using {@link #registerEventNotifier(HystrixEventNotifier)} or setting property (via Archaius): <code>hystrix.plugin.HystrixEventNotifier.implementation</code> with the full classname to
7583
* load.
7684
*
7785
* @return {@link HystrixEventNotifier} implementation to use
7886
*/
7987
public HystrixEventNotifier getEventNotifier() {
8088
if (notifier.get() == null) {
81-
// check for an implementation from System.getProperty first
82-
Object impl = getPluginImplementationViaProperty(HystrixEventNotifier.class);
89+
// check for an implementation from Archaius first
90+
Object impl = getPluginImplementationViaArchaius(HystrixEventNotifier.class);
8391
if (impl == null) {
84-
// nothing set via properties so initialize with default
92+
// nothing set via Archaius so initialize with default
8593
notifier.compareAndSet(null, HystrixEventNotifierDefault.getInstance());
8694
// we don't return from here but call get() again in case of thread-race so the winner will always get returned
8795
} else {
88-
// we received an implementation from the system property so use it
96+
// we received an implementation from Archaius so use it
8997
notifier.compareAndSet(null, (HystrixEventNotifier) impl);
9098
}
9199
}
@@ -109,21 +117,21 @@ public void registerEventNotifier(HystrixEventNotifier impl) {
109117
/**
110118
* Retrieve instance of {@link HystrixConcurrencyStrategy} to use based on order of precedence as defined in {@link HystrixPlugins} class header.
111119
* <p>
112-
* Override default by using {@link #registerConcurrencyStrategy(HystrixConcurrencyStrategy)} or setting property: <code>hystrix.plugin.HystrixConcurrencyStrategy.implementation</code> with the
120+
* Override default by using {@link #registerConcurrencyStrategy(HystrixConcurrencyStrategy)} or setting property (via Archaius): <code>hystrix.plugin.HystrixConcurrencyStrategy.implementation</code> with the
113121
* full classname to load.
114122
*
115123
* @return {@link HystrixConcurrencyStrategy} implementation to use
116124
*/
117125
public HystrixConcurrencyStrategy getConcurrencyStrategy() {
118126
if (concurrencyStrategy.get() == null) {
119-
// check for an implementation from System.getProperty first
120-
Object impl = getPluginImplementationViaProperty(HystrixConcurrencyStrategy.class);
127+
// check for an implementation from Archaius first
128+
Object impl = getPluginImplementationViaArchaius(HystrixConcurrencyStrategy.class);
121129
if (impl == null) {
122-
// nothing set via properties so initialize with default
130+
// nothing set via Archaius so initialize with default
123131
concurrencyStrategy.compareAndSet(null, HystrixConcurrencyStrategyDefault.getInstance());
124132
// we don't return from here but call get() again in case of thread-race so the winner will always get returned
125133
} else {
126-
// we received an implementation from the system property so use it
134+
// we received an implementation from Archaius so use it
127135
concurrencyStrategy.compareAndSet(null, (HystrixConcurrencyStrategy) impl);
128136
}
129137
}
@@ -147,21 +155,21 @@ public void registerConcurrencyStrategy(HystrixConcurrencyStrategy impl) {
147155
/**
148156
* Retrieve instance of {@link HystrixMetricsPublisher} to use based on order of precedence as defined in {@link HystrixPlugins} class header.
149157
* <p>
150-
* Override default by using {@link #registerMetricsPublisher(HystrixMetricsPublisher)} or setting property: <code>hystrix.plugin.HystrixMetricsPublisher.implementation</code> with the full
158+
* Override default by using {@link #registerMetricsPublisher(HystrixMetricsPublisher)} or setting property (via Archaius): <code>hystrix.plugin.HystrixMetricsPublisher.implementation</code> with the full
151159
* classname to load.
152160
*
153161
* @return {@link HystrixMetricsPublisher} implementation to use
154162
*/
155163
public HystrixMetricsPublisher getMetricsPublisher() {
156164
if (metricsPublisher.get() == null) {
157-
// check for an implementation from System.getProperty first
158-
Object impl = getPluginImplementationViaProperty(HystrixMetricsPublisher.class);
165+
// check for an implementation from Archaius first
166+
Object impl = getPluginImplementationViaArchaius(HystrixMetricsPublisher.class);
159167
if (impl == null) {
160-
// nothing set via properties so initialize with default
168+
// nothing set via Archaius so initialize with default
161169
metricsPublisher.compareAndSet(null, HystrixMetricsPublisherDefault.getInstance());
162170
// we don't return from here but call get() again in case of thread-race so the winner will always get returned
163171
} else {
164-
// we received an implementation from the system property so use it
172+
// we received an implementation from Archaius so use it
165173
metricsPublisher.compareAndSet(null, (HystrixMetricsPublisher) impl);
166174
}
167175
}
@@ -185,21 +193,21 @@ public void registerMetricsPublisher(HystrixMetricsPublisher impl) {
185193
/**
186194
* Retrieve instance of {@link HystrixPropertiesStrategy} to use based on order of precedence as defined in {@link HystrixPlugins} class header.
187195
* <p>
188-
* Override default by using {@link #registerPropertiesStrategy(HystrixPropertiesStrategy)} or setting property: <code>hystrix.plugin.HystrixPropertiesStrategy.implementation</code> with the full
196+
* Override default by using {@link #registerPropertiesStrategy(HystrixPropertiesStrategy)} or setting property (via Archaius): <code>hystrix.plugin.HystrixPropertiesStrategy.implementation</code> with the full
189197
* classname to load.
190198
*
191199
* @return {@link HystrixPropertiesStrategy} implementation to use
192200
*/
193201
public HystrixPropertiesStrategy getPropertiesStrategy() {
194202
if (propertiesFactory.get() == null) {
195-
// check for an implementation from System.getProperty first
196-
Object impl = getPluginImplementationViaProperty(HystrixPropertiesStrategy.class);
203+
// check for an implementation from Archaius first
204+
Object impl = getPluginImplementationViaArchaius(HystrixPropertiesStrategy.class);
197205
if (impl == null) {
198-
// nothing set via properties so initialize with default
206+
// nothing set via Archaius so initialize with default
199207
propertiesFactory.compareAndSet(null, HystrixPropertiesStrategyDefault.getInstance());
200208
// we don't return from here but call get() again in case of thread-race so the winner will always get returned
201209
} else {
202-
// we received an implementation from the system property so use it
210+
// we received an implementation from Archaius so use it
203211
propertiesFactory.compareAndSet(null, (HystrixPropertiesStrategy) impl);
204212
}
205213
}
@@ -223,7 +231,7 @@ public void registerPropertiesStrategy(HystrixPropertiesStrategy impl) {
223231
/**
224232
* Retrieve instance of {@link HystrixCommandExecutionHook} to use based on order of precedence as defined in {@link HystrixPlugins} class header.
225233
* <p>
226-
* Override default by using {@link #registerCommandExecutionHook(HystrixCommandExecutionHook)} or setting property: <code>hystrix.plugin.HystrixCommandExecutionHook.implementation</code> with the
234+
* Override default by using {@link #registerCommandExecutionHook(HystrixCommandExecutionHook)} or setting property (via Archaius): <code>hystrix.plugin.HystrixCommandExecutionHook.implementation</code> with the
227235
* full classname to
228236
* load.
229237
*
@@ -233,14 +241,14 @@ public void registerPropertiesStrategy(HystrixPropertiesStrategy impl) {
233241
*/
234242
public HystrixCommandExecutionHook getCommandExecutionHook() {
235243
if (commandExecutionHook.get() == null) {
236-
// check for an implementation from System.getProperty first
237-
Object impl = getPluginImplementationViaProperty(HystrixCommandExecutionHook.class);
244+
// check for an implementation from Archaius first
245+
Object impl = getPluginImplementationViaArchaius(HystrixCommandExecutionHook.class);
238246
if (impl == null) {
239-
// nothing set via properties so initialize with default
247+
// nothing set via Archaius so initialize with default
240248
commandExecutionHook.compareAndSet(null, HystrixCommandExecutionHookDefault.getInstance());
241249
// we don't return from here but call get() again in case of thread-race so the winner will always get returned
242250
} else {
243-
// we received an implementation from the system property so use it
251+
// we received an implementation from Archaius so use it
244252
commandExecutionHook.compareAndSet(null, (HystrixCommandExecutionHook) impl);
245253
}
246254
}
@@ -263,15 +271,11 @@ public void registerCommandExecutionHook(HystrixCommandExecutionHook impl) {
263271
}
264272
}
265273

266-
private static Object getPluginImplementationViaProperty(Class<?> pluginClass) {
274+
private static Object getPluginImplementationViaArchaius(Class<?> pluginClass) {
267275
String classSimpleName = pluginClass.getSimpleName();
268-
/*
269-
* Check system properties for plugin class.
270-
* <p>
271-
* This will only happen during system startup thus it's okay to use the synchronized System.getProperties
272-
* as it will never get called in normal operations.
273-
*/
274-
String implementingClass = System.getProperty("hystrix.plugin." + classSimpleName + ".implementation");
276+
// Check Archaius for plugin class.
277+
String propertyName = "hystrix.plugin." + classSimpleName + ".implementation";
278+
String implementingClass = DynamicPropertyFactory.getInstance().getStringProperty(propertyName, null).get();
275279
if (implementingClass != null) {
276280
try {
277281
Class<?> cls = Class.forName(implementingClass);

0 commit comments

Comments
 (0)