Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel Redis Automatically Prepends String to Key

I'm using the Redis component in my Camel application. One issue is that it automatically prepends strings to keys. For example, let's say I run the following in my Camel app:

        from("direct://path/to/store/in/redis")
            .setHeader(RedisConstants.COMMAND, constant("SET"))
            .setHeader(RedisConstants.KEY, constant("key"))
            .setHeader(RedisConstants.VALUE, constant("value"))
            .to(spring-redis://localhost:6379);

Then, if I open my command-line Redis client and run the following to list all keys in the DB:

> keys *

it returns:

1) "\xac\xed\x00\x05t\x00\x03key"

Here you can see that it prepended \xac\xed\x00\x05t\x00\x03 to the key, and I'm not sure where exactly it does that.

This wouldn't be a problem if I was only using the GET and SET Redis commands, because for some reason it prepends the same string to the key for these commands, so there's no key mis-match. However, if I try to perform a different Redis command, like KEYS, through the Camel app, like this:

from("direct://some/other/path/to/redis")
        .setHeader(RedisConstants.COMMAND, constant("KEYS"))
        .setHeader(RedisConstants.PATTERN, constant("*"))
        .to(spring-redis://localhost:6379);

it prepends a slightly different string to the asterisk, which results in the query not returning anything because there are no matches to the pattern. That is, the

> KEYS * 

command translates to something like the following in Redis:

> KEYS "\xac\xed\x00\x05t\x00\x05t*" 

Any thoughts on this?

like image 546
Khaled Avatar asked Oct 20 '25 15:10

Khaled


1 Answers

The following two posts helped me solve this:

Redis serialization prefixed with extra string

Weird redis key with spring data Jedis

So I fixed it by setting the DefaultRedisSerializer in RedisTemplate to StringRedisSerializer.

Since I'm using Guice for dependency/bean injection, I added the following to my GuiceCamelModule:

public class GuiceCamelTestModule extends CamelModuleWithMatchingRoutes {

    ...

    @Provides
    @JndiBind("redisTemplateBean")
    Object provideRedisTemplateBean() {
        JedisConnectionFactory redisConnectionFactory = new  JedisConnectionFactory();
        redisConnectionFactory.afterPropertiesSet();

        RedisTemplate<?, ?> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        template.setDefaultSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

And my route URI looks like this:

"spring-redis://localhost:6379?redisTemplate=#redisTemplateBean"
like image 157
Khaled Avatar answered Oct 23 '25 06:10

Khaled



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!