|
| 1 | +package com.baeldung.hystrix; |
| 2 | + |
| 3 | +import com.netflix.hystrix.*; |
| 4 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 5 | +import org.aspectj.lang.annotation.Around; |
| 6 | +import org.aspectj.lang.annotation.Aspect; |
| 7 | +import org.springframework.beans.factory.annotation.Value; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | + |
| 10 | +import javax.annotation.PostConstruct; |
| 11 | + |
| 12 | +@Component |
| 13 | +@Aspect |
| 14 | +public class HystrixAspect { |
| 15 | + |
| 16 | + private HystrixCommand.Setter config; |
| 17 | + private HystrixCommandProperties.Setter commandProperties; |
| 18 | + private HystrixThreadPoolProperties.Setter threadPoolProperties; |
| 19 | + |
| 20 | + @Around("@annotation(com.baeldung.hystrix.HystrixCircuitBreaker)") |
| 21 | + public Object circuitBreakerAround(final ProceedingJoinPoint aJoinPoint) { |
| 22 | + return new RemoteServiceCommand(config, aJoinPoint).execute(); |
| 23 | + } |
| 24 | + |
| 25 | + @PostConstruct |
| 26 | + private void setup() { |
| 27 | + this.config = HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)); |
| 28 | + this.config = config.andCommandKey(HystrixCommandKey.Factory.asKey(key)); |
| 29 | + |
| 30 | + this.commandProperties = HystrixCommandProperties.Setter(); |
| 31 | + this.commandProperties.withExecutionTimeoutInMilliseconds(executionTimeout); |
| 32 | + this.commandProperties.withCircuitBreakerSleepWindowInMilliseconds(sleepWindow); |
| 33 | + |
| 34 | + this.threadPoolProperties= HystrixThreadPoolProperties.Setter(); |
| 35 | + this.threadPoolProperties.withMaxQueueSize(maxThreadCount).withCoreSize(coreThreadCount).withMaxQueueSize(queueCount); |
| 36 | + |
| 37 | + this.config.andCommandPropertiesDefaults(commandProperties); |
| 38 | + this.config.andThreadPoolPropertiesDefaults(threadPoolProperties); |
| 39 | + } |
| 40 | + |
| 41 | + private static class RemoteServiceCommand extends HystrixCommand<String> { |
| 42 | + |
| 43 | + private final ProceedingJoinPoint joinPoint; |
| 44 | + |
| 45 | + RemoteServiceCommand(final Setter config, final ProceedingJoinPoint joinPoint) { |
| 46 | + super(config); |
| 47 | + this.joinPoint = joinPoint; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected String run() throws Exception { |
| 52 | + try { |
| 53 | + return (String) joinPoint.proceed(); |
| 54 | + } catch (final Throwable th) { |
| 55 | + throw new Exception(th); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Value("${remoteservice.command.execution.timeout}") |
| 62 | + private int executionTimeout; |
| 63 | + |
| 64 | + @Value("${remoteservice.command.sleepwindow}") |
| 65 | + private int sleepWindow; |
| 66 | + |
| 67 | + @Value("${remoteservice.command.threadpool.maxsize}") |
| 68 | + private int maxThreadCount; |
| 69 | + |
| 70 | + @Value("${remoteservice.command.threadpool.coresize}") |
| 71 | + private int coreThreadCount; |
| 72 | + |
| 73 | + @Value("${remoteservice.command.task.queue.size}") |
| 74 | + private int queueCount; |
| 75 | + |
| 76 | + @Value("${remoteservice.command.group.key}") |
| 77 | + private String groupKey; |
| 78 | + |
| 79 | + @Value("${remoteservice.command.key}") |
| 80 | + private String key; |
| 81 | +} |
0 commit comments