Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
imyzt committed Dec 10, 2023
1 parent d923432 commit adb054f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public org.springframework.cloud.client.circuitbreaker.CircuitBreaker create(Str
public org.springframework.cloud.client.circuitbreaker.CircuitBreaker create(String id, String groupName) {
Assert.hasText(id, "A CircuitBreaker must have an id.");
Assert.hasText(groupName, "A CircuitBreaker must have a group name.");
final ExecutorService groupExecutorService = executorServices.computeIfAbsent(groupName, groupExecutorServiceFactory);
final ExecutorService groupExecutorService = executorServices.computeIfAbsent(groupName,
groupExecutorServiceFactory);
Resilience4JCircuitBreaker resilience4JCircuitBreaker = create(id, groupName, groupExecutorService);
return tryObservedCircuitBreaker(resilience4JCircuitBreaker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package org.springframework.cloud.circuitbreaker.resilience4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.github.resilience4j.bulkhead.ThreadPoolBulkheadRegistry;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
Expand All @@ -24,13 +28,10 @@
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;

import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
import org.springframework.cloud.client.circuitbreaker.NoFallbackAvailableException;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -237,8 +238,8 @@ public void runWithDisabledTimeLimiter() {
*/
@Test
public void runWithCustomGroupThreadPool() {
Resilience4JCircuitBreakerFactory factory = new Resilience4JCircuitBreakerFactory(CircuitBreakerRegistry.ofDefaults(),
TimeLimiterRegistry.ofDefaults(), null);
Resilience4JCircuitBreakerFactory factory = new Resilience4JCircuitBreakerFactory(
CircuitBreakerRegistry.ofDefaults(), TimeLimiterRegistry.ofDefaults(), null);
String groupName = "groupFoo";

// configure GroupExecutorService
Expand All @@ -253,8 +254,8 @@ public void runWithCustomGroupThreadPool() {
*/
@Test
public void runWithCustomNormalThreadPool() {
Resilience4JCircuitBreakerFactory factory = new Resilience4JCircuitBreakerFactory(CircuitBreakerRegistry.ofDefaults(),
TimeLimiterRegistry.ofDefaults(), null);
Resilience4JCircuitBreakerFactory factory = new Resilience4JCircuitBreakerFactory(
CircuitBreakerRegistry.ofDefaults(), TimeLimiterRegistry.ofDefaults(), null);
String threadPoolName = "demo-";

// configure ExecutorService
Expand All @@ -270,10 +271,11 @@ static class ContextThreadPoolExecutor extends ThreadPoolExecutor {
* example ContextThreadPoolExecutor
* @param threadPoolName fixed threadPoolName
*/
public ContextThreadPoolExecutor(String threadPoolName) {
ContextThreadPoolExecutor(String threadPoolName) {
super(2, 5, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1024));
this.setThreadFactory(new NamedThreadFactory(threadPoolName));
}

}

}

0 comments on commit adb054f

Please sign in to comment.