MarcGiffing / bucket4j-spring-boot-starter

Spring Boot Starter for Bucket4j
Apache License 2.0
305 stars 66 forks source link

bucket4j starter, spring boot 2.7.15, Java 17 error: ClassCastException String cannot be cast to class [B #186

Closed www-viveksharma closed 1 year ago

www-viveksharma commented 1 year ago

Using bucket4j starter with spring boot version 2.7.15 and Java 17 (openJdk) along with redis (redisson-spring-boot-starter ver 3.23.2) getting an error when trying to do : bucket.tryConsumeAsMuchAsPossible(num);

Error:

javax.cache.processor.EntryProcessorException: java.lang.ClassCastException: class java.lang.String cannot be cast to class [B (java.lang.String and [B are in module java.base of loader 'bootstrap')
    at org.redisson.jcache.JCache.invoke(JCache.java:3067)
    at io.github.bucket4j.grid.jcache.JCacheProxyManager.execute(JCacheProxyManager.java:82)
    at io.github.bucket4j.distributed.proxy.AbstractProxyManager$DefaultRemoteBucketBuilder$1.execute(AbstractProxyManager.java:197)
    at io.github.bucket4j.distributed.proxy.DefaultBucketProxy.execute(DefaultBucketProxy.java:208)
    at io.github.bucket4j.distributed.proxy.DefaultBucketProxy.consumeAsMuchAsPossibleImpl(DefaultBucketProxy.java:78)
    at io.github.bucket4j.AbstractBucket.tryConsumeAsMuchAsPossible(AbstractBucket.java:307)

Caused by:

java.lang.ClassCastException: class java.lang.String cannot be cast to class [B (java.lang.String and [B are in module java.base of loader 'bootstrap')
    at io.github.bucket4j.grid.jcache.JCacheProxyManager$JCacheTransaction.getRawState(JCacheProxyManager.java:188)
    at io.github.bucket4j.distributed.remote.AbstractBinaryTransaction.get(AbstractBinaryTransaction.java:72)
    at io.github.bucket4j.distributed.remote.commands.ConsumeAsMuchAsPossibleCommand.execute(ConsumeAsMuchAsPossibleCommand.java:109)
    at io.github.bucket4j.distributed.remote.AbstractBinaryTransaction.execute(AbstractBinaryTransaction.java:53)
    at io.github.bucket4j.grid.jcache.JCacheProxyManager$BucketProcessor.process(JCacheProxyManager.java:167)
    at io.github.bucket4j.grid.jcache.JCacheProxyManager$BucketProcessor.process(JCacheProxyManager.java:155)
    at org.redisson.jcache.JCache.invoke(JCache.java:3055)
www-viveksharma commented 1 year ago

I was looking on my redis server where I am testing this and I see:

redis-cli -h <my.ip.addr>

<my.ip.addr>:6379> KEYS *
1) "my-bucket4j-cache-redis"

<my.ip.addr>:6379> hgetall my-bucket4j-cache-redis
1) "\"Tenant:1038849272730853\""
2) "\"AAAAAQAAAAEAAAABAAAAAQAAAAAAAAfQAAAAAAAAB9AAAAAN+EdYAAAAAAAAAAfQAYAAAAAAAAAAAAAAAAADAAAAAQAAAAMXhMkS0JJrAAAAAAAAAAfQAAAAAAAAAAAAAAABAAAAAAAAAAA=\""

and when I am debugging I am seeing the following: image

so looks like on redis the bytes written as string are not being deserialized properly when read back at JCacheProxyManager

www-viveksharma commented 1 year ago

#1 noted from https://github.com/MarcGiffing/bucket4j-spring-boot-starter that I should have used version 0.8.1 of bucket4j-spring-boot-starter :

<dependency>
    <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
    <artifactId>bucket4j-spring-boot-starter</artifactId>
    <!-- https://github.com/MarcGiffing/bucket4j-spring-boot-starter -->
    <version>0.8.1</version> <!-- use 0.8.* gives you Bucket4j 8.1.0 & works with Spring Boot 2.7.x -->
    <exclusions> <!-- EXCLUDE ehcache -->
        <exclusion>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </exclusion>
    </exclusions>
</dependency>

as 0.8.* gives you Bucket4j 8.1.0 & works with Spring Boot 2.7.x (which is the spring version I had)

#2 found that I had the following codec used in my redisson config yaml which was not correct for my use case:

codec: !<org.redisson.codec.JsonJacksonCodec> {}

removed this and now able to see at least the basic functionality working.

will proceed to testing the clustered redis deployment with bucket4j springboot starter 0.8.1 next.