Tirumalesh
Tirumalesh

Reputation: 11

How to Serialize LockFreeBucket for Infinispan Cache in Bucket4j?

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 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());
}

Upvotes: 0

Views: 320

Answers (2)

Bukhtoyarov  Vladimir
Bukhtoyarov Vladimir

Reputation: 340

First of all, why do you not use the existing built-in integration of Bucket4j with Infinispan?

If you do not trust official integration, that means that you ignore built-in distributed facilities, hence it is legal to always cast bucket to LocalBucket. LocalBucket provides all neccesary functionality for serialization and desiralization from/to both binary form and JSON compatible HashMap.

Upvotes: 0

pruivo
pruivo

Reputation: 1344

LockFreeBucket does not implement java.io.Serializable. The simplest way would be with Protostream adapters, although it seems a complex class to serialize: https://infinispan.org/docs/stable/titles/encoding/encoding.html#creating-protostream-adapter_marshalling

Are you using bucket4j-infinispan?

Upvotes: 1

Related Questions