question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to implement a CacheRegistryStore for CircuitBreaker with MemCached

See original GitHub issue

Resilience4j version: 1.7.1

Java version: 17.0.2

Spring Boot version: 2.7.5

Hello, I’m trying to create a CacheRegistryStore for a CB with MemCached. I implemented CacheRegistryStore but when I tried to run the application it threw an exception which indicated that the CB could not be serialized.

Is there a way to serialize the circuit breaker to store it? @Romeh @RobWin The exception generated is the following:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloController': Unsatisfied dependency expressed through field 'circuitBreaker'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getCircuitBreaker' defined in class path resource [com/example/circuitbreaker/config/CircuitBreakerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.github.resilience4j.circuitbreaker.CircuitBreaker]: Factory method 'getCircuitBreaker' threw exception; nested exception is java.lang.IllegalArgumentException: Non-serializable object at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.23.jar:5.3.23] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.23.jar:5.3.23] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.5.jar:2.7.5] at com.example.circuitbreaker.CircuitBreakerApplication.main(CircuitBreakerApplication.java:10) ~[classes/:na] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getCircuitBreaker' defined in class path resource [com/example/circuitbreaker/config/CircuitBreakerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.github.resilience4j.circuitbreaker.CircuitBreaker]: Factory method 'getCircuitBreaker' threw exception; nested exception is java.lang.IllegalArgumentException: Non-serializable object at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656) ~[spring-beans-5.3.23.jar:5.3.23] ... 20 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.github.resilience4j.circuitbreaker.CircuitBreaker]: Factory method 'getCircuitBreaker' threw exception; nested exception is java.lang.IllegalArgumentException: Non-serializable object at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.23.jar:5.3.23] ... 33 common frames omitted Caused by: java.lang.IllegalArgumentException: Non-serializable object at net.spy.memcached.transcoders.BaseSerializingTranscoder.serialize(BaseSerializingTranscoder.java:110) ~[spymemcached-2.12.3.jar:2.12.3] at net.spy.memcached.transcoders.SerializingTranscoder.encode(SerializingTranscoder.java:162) ~[spymemcached-2.12.3.jar:2.12.3] at net.spy.memcached.MemcachedClient.asyncStore(MemcachedClient.java:304) ~[spymemcached-2.12.3.jar:2.12.3] at net.spy.memcached.MemcachedClient.replace(MemcachedClient.java:967) ~[spymemcached-2.12.3.jar:2.12.3] at com.example.circuitbreaker.experimentalCache.MemcachedCache.replace(MemcachedCache.java:560) ~[classes/:na] at com.example.circuitbreaker.experimentalCache.MemcachedCache.invoke(MemcachedCache.java:647) ~[classes/:na] at io.github.resilience4j.cache.CacheRegistryStore.computeIfAbsent(CacheRegistryStore.java:25) ~[resilience4j-cache-1.7.1.jar:1.7.1] at io.github.resilience4j.core.registry.AbstractRegistry.computeIfAbsent(AbstractRegistry.java:102) ~[resilience4j-core-1.7.0.jar:1.7.0] at io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry.circuitBreaker(InMemoryCircuitBreakerRegistry.java:166) ~[resilience4j-circuitbreaker-1.7.0.jar:1.7.0] at io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry.circuitBreaker(InMemoryCircuitBreakerRegistry.java:160) ~[resilience4j-circuitbreaker-1.7.0.jar:1.7.0] at io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry.circuitBreaker(InMemoryCircuitBreakerRegistry.java:147) ~[resilience4j-circuitbreaker-1.7.0.jar:1.7.0] at com.example.circuitbreaker.config.CircuitBreakerConfiguration.getCircuitBreaker(CircuitBreakerConfiguration.java:78) ~[classes/:na] at com.example.circuitbreaker.config.CircuitBreakerConfiguration$$EnhancerBySpringCGLIB$$20596377.CGLIB$getCircuitBreaker$2(<generated>) ~[classes/:na] at com.example.circuitbreaker.config.CircuitBreakerConfiguration$$EnhancerBySpringCGLIB$$20596377$$FastClassBySpringCGLIB$$9d3cadb7.invoke(<generated>) ~[classes/:na] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.23.jar:5.3.23] at com.example.circuitbreaker.config.CircuitBreakerConfiguration$$EnhancerBySpringCGLIB$$20596377.getCircuitBreaker(<generated>) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.23.jar:5.3.23] ... 34 common frames omitted Caused by: java.io.NotSerializableException: io.github.resilience4j.circuitbreaker.internal.CircuitBreakerStateMachine at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1197) ~[na:na] at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354) ~[na:na] at net.spy.memcached.transcoders.BaseSerializingTranscoder.serialize(BaseSerializingTranscoder.java:105) ~[spymemcached-2.12.3.jar:2.12.3] ... 55 common frames omitted

Notice that: Caused by: java.io.NotSerializableException: io.github.resilience4j.circuitbreaker.internal.CircuitBreakerStateMachine

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
helixBoycommented, Nov 2, 2022

Hello @marcosabelenda ,

what a coincidence, I’m trying to do exactly the same thing and I have the same exact problem. The docs is not clear at all in this particular part.

Hopefully someone provide us a solution

0reactions
maabelendacommented, Nov 4, 2022

Ok, i’ll check it out. Thank you so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

how Resilience4J cleans up circuitBreaker objects in memory
I am creating circuitBreaker instance per url and ended up ... with cache eviction either use a distributed cache such as memcached.
Read more >
javaslang - Bountysource
Hello, I'm trying to create a CacheRegistryStore for a CB with MemCached. ... Is there a way to serialize the circuit breaker to...
Read more >
Springboot Caching with Memcached and Redis - Medium
This is the configuration class I have used to load the Memcached implementation as the caching provider. This will create a CacheManager bean ......
Read more >
Cache - resilience4j
Create a CacheContext by wrapping a JCache instance. javax.cache.Cache<String, String> cacheInstance = Caching .getCache("cacheName", String.class, ...
Read more >
Guide to Resilience4j - Baeldung
In this tutorial, we'll talk about the Resilience4j library. The library helps with implementing resilient systems by managing fault ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found