Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jan 26, 2025
1 parent 79bef43 commit 4dc067a
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,6 @@
*/
public abstract class Resilience4jTemplate<E, C, R extends Registry<E, C>> {

/**
* The resource-path for default templates
*/
private static final String DEFAULT_TEMPLATES_RESOURCE_PATH = "META-INF/default/templates.properties";

private static final EnumMap<Resilience4jModule, Class<? extends Resilience4jTemplate>> defaultTemplates;

static {
Properties properties = new Properties();
ClassLoader classLoader = getClassLoader(Resilience4jTemplate.class);
URL url = getResource(classLoader, DEFAULT_TEMPLATES_RESOURCE_PATH);
InputStream inputStream = null;
try {
inputStream = url.openStream();
properties.load(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
close(inputStream);
}

defaultTemplates = new EnumMap<>(Resilience4jModule.class);
for (Resilience4jModule module : Resilience4jModule.values()) {
String moduleName = module.name().toLowerCase();
String templateClassName = properties.getProperty(moduleName);
Class<? extends Resilience4jTemplate> templateClass =
(Class<? extends Resilience4jTemplate>) loadClass(templateClassName, classLoader);
defaultTemplates.put(module, templateClass);
}

}

protected final Logger logger = getLogger(getClass());

protected final R registry;
Expand Down Expand Up @@ -455,17 +423,4 @@ public void destroy() {
localEntriesCache.clear();
}

/**
* @param registry
* @param <E> the type of Resilience4j's entry, e.g., {@link CircuitBreaker}
* @param <C> the type of Resilience4j's entry configuration, e.g., {@link CircuitBreakerConfig}
* @param <R> the type of Resilience4j's entry registry, e.g., {@link CircuitBreakerRegistry}
* @param <T> the sub-type of {@link Resilience4jTemplate}
* @return
*/
public static <E, C, R extends Registry<E, C>, T extends Resilience4jTemplate<E, C, R>> T createTemplate(R registry) {
Resilience4jModule module = valueOf(registry.getClass());
Class<? extends Resilience4jTemplate> templateClass = defaultTemplates.get(module);
return (T) newInstance(templateClass, registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,37 @@

import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.core.EventProcessor;
import io.github.resilience4j.core.EventPublisher;
import io.github.resilience4j.core.Registry;
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.timelimiter.TimeLimiter;
import io.microsphere.resilience4j.common.Resilience4jModule;
import io.microsphere.resilience4j.common.Resilience4jTemplate;
import io.microsphere.util.BaseUtils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static io.microsphere.io.IOUtils.close;
import static io.microsphere.reflect.MethodUtils.findMethod;
import static io.microsphere.reflect.MethodUtils.invokeMethod;
import static io.microsphere.resilience4j.common.Resilience4jModule.valueOf;
import static io.microsphere.text.FormatUtils.format;
import static io.microsphere.util.ClassLoaderUtils.getClassLoader;
import static io.microsphere.util.ClassLoaderUtils.getResource;
import static io.microsphere.util.ClassLoaderUtils.loadClass;
import static io.microsphere.util.ClassUtils.newInstance;
import static java.beans.Introspector.decapitalize;
import static java.util.Collections.unmodifiableMap;

Expand All @@ -48,8 +61,15 @@
*/
public abstract class Resilience4jUtils extends BaseUtils {

/**
* The resource-path for default templates
*/
private static final String DEFAULT_TEMPLATES_RESOURCE_PATH = "META-INF/default/templates.properties";

private static final Map<Resilience4jModule, Method> getEntryMethodsCache;

private static final Map<Resilience4jModule, Class<? extends Resilience4jTemplate>> defaultTemplates = loadDefaultTemplates();

static {
Resilience4jModule[] modules = Resilience4jModule.values();
Map<Resilience4jModule, Method> methodsCache = new HashMap<>(modules.length);
Expand Down Expand Up @@ -130,4 +150,54 @@ private static EventProcessor asEventProcessor(EventPublisher eventPublisher) {
String errorMessage = format("The eventPublisher should be an instance of EventProcessor, actual : {}", eventPublisher.getClass());
throw new UnsupportedOperationException(errorMessage);
}

/**
* Create an instance of {@link Resilience4jTemplate} from the specified {@link Registry}
*
* @param registry {@link Registry}
* @param <E> the type of Resilience4j's entry, e.g., {@link CircuitBreaker}
* @param <C> the type of Resilience4j's entry configuration, e.g., {@link CircuitBreakerConfig}
* @param <R> the type of Resilience4j's entry registry, e.g., {@link CircuitBreakerRegistry}
* @param <T> the sub-type of {@link Resilience4jTemplate}
* @return an instance of {@link Resilience4jTemplate}
*/
public static <E, C, R extends Registry<E, C>, T extends Resilience4jTemplate<E, C, R>> T createTemplate(R registry) {
Resilience4jModule module = valueOf(registry.getClass());
Class<? extends Resilience4jTemplate> templateClass = defaultTemplates.get(module);
return (T) newInstance(templateClass, registry);
}

static Map<Resilience4jModule, Class<? extends Resilience4jTemplate>> loadDefaultTemplates() {
Resilience4jModule[] modules = Resilience4jModule.values();
int size = modules.length;
Map<Resilience4jModule, Class<? extends Resilience4jTemplate>> defaultTemplates = new HashMap<>(size);

ClassLoader classLoader = getClassLoader(Resilience4jTemplate.class);
Properties properties = loadDefaultTemplatesProperties(classLoader);
new EnumMap<>(Resilience4jModule.class);
for (Resilience4jModule module : Resilience4jModule.values()) {
String moduleName = module.name().toLowerCase();
String templateClassName = properties.getProperty(moduleName);
Class<? extends Resilience4jTemplate> templateClass =
(Class<? extends Resilience4jTemplate>) loadClass(templateClassName, classLoader);
defaultTemplates.put(module, templateClass);
}

return defaultTemplates;
}

static Properties loadDefaultTemplatesProperties(ClassLoader classLoader) {
Properties properties = new Properties();
URL url = getResource(classLoader, DEFAULT_TEMPLATES_RESOURCE_PATH);
InputStream inputStream = null;
try {
inputStream = url.openStream();
properties.load(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
close(inputStream);
}
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* {@link Resilience4jUtils}
* {@link Resilience4jUtils} Test
*
* @author <a href="mailto:[email protected]">Mercy<a/>
* @see Resilience4jUtils
Expand All @@ -35,9 +35,14 @@
public class Resilience4jUtilsTest {

@Test
public void test() {
public void testGetEntry() {
BulkheadRegistry bulkheadRegistry = ofDefaults();
Bulkhead bulkhead = getEntry(bulkheadRegistry, "test");
assertNotNull(bulkhead);
}

@Test
public void testGetEventPublisher() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.bulkhead.configure.BulkheadConfigurationProperties;
import io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties;
import io.microsphere.resilience4j.spring.common.annotation.EnableResilience4jExtension;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBindings;
import io.microsphere.resilience4j.spring.common.annotation.EnableResilience4jExtension;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.AliasFor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.bulkhead.BulkheadConfig;
import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.microsphere.resilience4j.bulkhead.BulkheadTemplate;
import io.microsphere.resilience4j.common.Resilience4jTemplate;
import io.microsphere.resilience4j.spring.common.jdbc.druid.Resilience4jDruidFilter;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import io.microsphere.resilience4j.spring.common.annotation.EnableResilience4jExtension;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.AliasFor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.microsphere.resilience4j.circuitbreaker.CircuitBreakerTemplate;
import io.microsphere.resilience4j.common.Resilience4jTemplate;
import io.microsphere.resilience4j.spring.common.jdbc.druid.Resilience4jDruidFilter;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfiguration;
import io.microsphere.spring.context.annotation.BeanCapableImportCandidate;
import io.microsphere.spring.core.annotation.ResolvablePlaceholderAnnotationAttributes;
import io.microsphere.resilience4j.spring.circuitbreaker.annotation.EnableCircuitBreaker;
import io.microsphere.resilience4j.spring.common.event.Resilience4jEventApplicationEventPublisher;
import io.microsphere.resilience4j.spring.common.event.Resilience4jEventConsumerBeanRegistrar;
import io.microsphere.spring.context.annotation.BeanCapableImportCandidate;
import io.microsphere.spring.core.annotation.ResolvablePlaceholderAnnotationAttributes;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.ResolvableType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

import static com.alibaba.druid.sql.SQLUtils.parseStatements;
import static io.microsphere.logging.LoggerFactory.getLogger;
import static io.microsphere.resilience4j.common.Resilience4jTemplate.createTemplate;
import static io.microsphere.resilience4j.util.Resilience4jUtils.createTemplate;

/**
* Resilience4j x Druid {@link Filter}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

import static io.microsphere.logging.LoggerFactory.getLogger;
import static io.microsphere.reflect.MethodUtils.getSignature;
import static io.microsphere.resilience4j.common.Resilience4jTemplate.createTemplate;
import static io.microsphere.resilience4j.util.Resilience4jUtils.createTemplate;
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import io.microsphere.resilience4j.spring.common.annotation.EnableResilience4jExtension;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.AliasFor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import io.microsphere.resilience4j.common.Resilience4jTemplate;
import io.microsphere.resilience4j.ratelimiter.RateLimiterTemplate;
import io.microsphere.resilience4j.spring.common.jdbc.druid.Resilience4jDruidFilter;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import io.microsphere.resilience4j.common.Resilience4jTemplate;
import io.microsphere.resilience4j.ratelimiter.RateLimiterTemplate;
import io.microsphere.resilience4j.spring.common.web.Resilience4jHandlerMethodInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.configure.RetryConfigurationProperties;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import io.microsphere.resilience4j.spring.common.annotation.EnableResilience4jExtension;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.AliasFor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import io.github.resilience4j.retry.RetryRegistry;
import io.microsphere.resilience4j.common.Resilience4jTemplate;
import io.microsphere.resilience4j.retry.RetryTemplate;
import io.microsphere.resilience4j.spring.common.jdbc.druid.Resilience4jDruidFilter;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import io.github.resilience4j.timelimiter.TimeLimiter;
import io.github.resilience4j.timelimiter.configure.TimeLimiterConfigurationProperties;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import io.microsphere.resilience4j.spring.common.annotation.EnableResilience4jExtension;
import io.microsphere.spring.beans.factory.annotation.EnableConfigurationBeanBinding;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.AliasFor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import io.github.resilience4j.timelimiter.TimeLimiter;
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
import io.github.resilience4j.timelimiter.TimeLimiterRegistry;
import io.microsphere.resilience4j.common.Resilience4jTemplate;
import io.microsphere.resilience4j.spring.common.jdbc.druid.Resilience4jDruidFilter;
import io.microsphere.resilience4j.timelimiter.TimeLimiterTemplate;

/**
* {@link TimeLimiter} x Druid {@link Filter}
Expand Down

0 comments on commit 4dc067a

Please sign in to comment.