I’m trying to implement a rate limiter using Bucket4j and Infinispan. Below is the code I’m using to store LockFreeBucket objects in the Infinispan cache:
public boolean isRateLimited(String key) { Bucket bucket = cacheManager.getCache("rate-limiter").get(key, Bucket.class); if (bucket == null) { bucket = bucketSupplier.get(); Cache<String, Bucket> cache = cacheManager.getCache("rate-limiter"); cache.put(key, bucket); } return !bucket.tryConsume(1); }
When it tries to put the key with cache.put(key, bucket), I get the following exception:
org.infinispan.client.hotrod.exceptions.HotRodClientException: Unable to marshall object of type [io.github.bucket4j.local.LockFreeBucket]
Caused by: java.io.NotSerializableException: io.github.bucket4j.local.LockFreeBucket
RemoteCacheManager Configuration
@Bean public RemoteCacheManager getRemoteCacheManager() { ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder .addServer() .host("somehost") .port(11222) .marshaller(new JavaSerializationMarshaller()) .addJavaSerialWhiteList("io.github.bucket4j.*") .security() .ssl() .sniHostName("infinispan") .trustStoreFileName("./cacerts") .trustStorePassword("changeit".toCharArray()) .authentication() .username("someusername") .password("somepassword") .addContextInitializer(new CreateProtoImpl()) .clientIntelligence(ClientIntelligence.BASIC); return new RemoteCacheManager(configurationBuilder.build()); }
When it tries to put the key i.e cache.put(key, bucket) I’m getting exception as org.infinispan.client.hotrod.exceptions.HotRodClientException:: Unable to marshall object of type [io.github.bucket4j.local.LockFreeBucket]] with root cause
java.io.NotSerializableException: io.github.bucket4j.local.LockFreeBucket
And below is my RemoteCacheManagerConfiugration
`@Bean
public RemoteCacheManager getRemoteCacheManager() {
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder
.addServer()
.host(“somehost”)
.port(11222)
.marshaller(new JavaSerializationMarshaller())
.addJavaSerialWhiteList(“io.github.bucket4j.*”)
.security()
.ssl()
.sniHostName(“infinispan”)
.trustStoreFileName(“./cacerts 2”)
.trustStorePassword(“changeit”.toCharArray())
.authentication()
.username(“some”)
.password(“somepassword”)
.addContextInitializer(new CreateProtoImpl())
.clientIntelligence(ClientIntelligence.BASIC);
return new RemoteCacheManager(configurationBuilder.build());
}`
Tirumalesh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.