com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault Java Examples

The following examples show how to use com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BettingServiceTest.java    From hystrix_lab with MIT License 4 votes vote down vote up
/**
 * Test - GetHorsesInRace - Uses Caching
 */
@Test
public void testWithCacheHits() {
	
	HystrixRequestContext context = HystrixRequestContext.initializeContext();
	
	try {
		CommandGetHorsesInRaceWithCaching commandFirst = new CommandGetHorsesInRaceWithCaching(mockService, RACE_1);
		CommandGetHorsesInRaceWithCaching commandSecond = new CommandGetHorsesInRaceWithCaching(mockService, RACE_1);

		commandFirst.execute();
		// this is the first time we've executed this command with
		// the value of "2" so it should not be from cache
		assertFalse(commandFirst.isResponseFromCache());

		verify(mockService).getHorsesInRace(RACE_1);
		verifyNoMoreInteractions(mockService);

		commandSecond.execute();
		// this is the second time we've executed this command with
		// the same value so it should return from cache
		assertTrue(commandSecond.isResponseFromCache());

	} finally {
		context.shutdown();
	}

	// start a new request context
	context = HystrixRequestContext.initializeContext();
	try {
		CommandGetHorsesInRaceWithCaching commandThree = new CommandGetHorsesInRaceWithCaching(mockService, RACE_1);
		commandThree.execute();
		// this is a new request context so this
		// should not come from cache
		assertFalse(commandThree.isResponseFromCache());

		// Flush the cache
		HystrixRequestCache.getInstance(GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear(RACE_1);

	} finally {
		context.shutdown();
	}
}
 
Example #2
Source File: HystrixStrategyMapping.java    From astrix with Apache License 2.0 4 votes vote down vote up
static HystrixStrategies getDefault() {
	return new HystrixStrategies(HystrixPropertiesStrategyDefault.getInstance(),
								 HystrixConcurrencyStrategyDefault.getInstance(),
								 HystrixEventNotifierDefault.getInstance(),
								 "default");
}
 
Example #3
Source File: HystrixPrometheusMetricsPublisher.java    From prometheus-hystrix with Apache License 2.0 4 votes vote down vote up
public void buildAndRegister() {
    HystrixPlugins plugins = HystrixPlugins.getInstance();

    // memorize the registered plugins
    HystrixCommandExecutionHook commandExecutionHook = plugins.getCommandExecutionHook();
    HystrixEventNotifier eventNotifier = plugins.getEventNotifier();
    HystrixMetricsPublisher metricsPublisher = plugins.getMetricsPublisher();
    HystrixPropertiesStrategy propertiesStrategy = plugins.getPropertiesStrategy();
    HystrixConcurrencyStrategy concurrencyStrategy = plugins.getConcurrencyStrategy();

    HystrixMetricsCollector collector = new HystrixMetricsCollector(namespace,
            new Consumer<Histogram.Builder>() {
                @Override
                public void accept(Histogram.Builder builder) {
                    if (metrics == MetricsType.EXPONENTIAL) {
                        builder.exponentialBuckets(exponentialStart, exponentialFactor, exponentialCount);
                    } else if (metrics == MetricsType.LINEAR) {
                        builder.linearBuckets(linearStart, linearWidth, linearCount);
                    } else if (metrics == MetricsType.DISTINCT) {
                        builder.buckets(distinctBuckets);
                    } else if (metrics == MetricsType.DEFAULT) {
                        // nothing to do
                    } else {
                        throw new IllegalStateException("unknown enum state " + metrics);
                    }
                }
            }).register(registry);

    // wrap the metrics publisher plugin
    HystrixPrometheusMetricsPublisher wrappedMetricsPublisher =
            new HystrixPrometheusMetricsPublisher(exportProperties,
                    exportDeprecatedMetrics, collector, metricsPublisher);

    // reset all plugins
    HystrixPlugins.reset();
    // the following statement wouldn't be necessary, but I'm paranoid that reset might
    // change the plugin instance.
    plugins = HystrixPlugins.getInstance();

    // set previous values for all plugins, but not if they would use the default implementation,
    // as this would block the slot for plugins to be registered.

    // REASON: if there was no previous setting, Hystrix would have returned the default implementation
    // and not all other plugin use the reset-and-wrap approach we do here.

    // ASSUMPTION: the default strategies/hooks can't wrap a different strategy/hook

    // CAVEAT: instead of a default implementation there is a sophisticated Archaius configuration mechanism
    // to determine a class from property settings. There is a corner case where someone would register a
    // default implementation manually overriding an Archaius configuration. Therefore this is configurable
    // using "registerDefaultPlugins".

    if (registerDefaultPlugins || concurrencyStrategy.getClass() != HystrixConcurrencyStrategyDefault.class) {
        plugins.registerConcurrencyStrategy(concurrencyStrategy);
    }
    if (registerDefaultPlugins || commandExecutionHook.getClass() != HystrixCommandExecutionHookDefault.class) {
        plugins.registerCommandExecutionHook(commandExecutionHook);
    }
    if (registerDefaultPlugins || eventNotifier.getClass() != HystrixEventNotifierDefault.class) {
        plugins.registerEventNotifier(eventNotifier);
    }
    if (registerDefaultPlugins || propertiesStrategy.getClass() != HystrixPropertiesStrategyDefault.class) {
        plugins.registerPropertiesStrategy(propertiesStrategy);
    }

    // ... except for the metrics publisher that will now be wrapped
    plugins.registerMetricsPublisher(wrappedMetricsPublisher);
}
 
Example #4
Source File: GetProductInfoCommand.java    From code with Apache License 2.0 2 votes vote down vote up
/**
 * Allow the cache to be flushed for this object.
 * @param id
 *     argument that would normally be passed to the command
 */
public static void flushCache(Long id) {
    HystrixRequestCache.getInstance(GETTER_KEY,
            HystrixConcurrencyStrategyDefault.getInstance()).clear(Long.toString(id));
}
 
Example #5
Source File: FwHystrixCommondFlushCache.java    From fw-spring-cloud with Apache License 2.0 2 votes vote down vote up
/**
 * 清理缓存
 *
 * @param name
 */
private static void flushCache(String name) {
    HystrixRequestCache.getInstance(TEST_KEY,
            HystrixConcurrencyStrategyDefault.getInstance()).clear(name);
}
 
Example #6
Source File: CommandUsingRequestCacheInvalidation.java    From tools-journey with Apache License 2.0 2 votes vote down vote up
/**
 * Allow the cache to be flushed for this object.
 * 
 * @param id
 *            argument that would normally be passed to the command
 */
public static void flushCache(int id) {
    HystrixRequestCache.getInstance(GETTER_KEY,
            HystrixConcurrencyStrategyDefault.getInstance()).clear(String.valueOf(id));
}