Teudimundo
Teudimundo

Reputation: 2670

Redis: Wrong serialization using the camel-redis

I'm playing with camel and redis. I have a very short route:

from("timer://foo?period=5s")
    .to("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson")
    .split().method(SplitFeatures.class,"splitMessage")
    .to("spring-redis://localhost:6379?command=SET&serializer=#serializer");

Where splitMessage is the following:

public static List<Message> splitMessage(@Body String body) throws IOException {

    List<Message> answer = new ArrayList<Message>();
    JsonParser parser=new JsonParser();
    JsonObject jo=(JsonObject)parser.parse(body);

    // I care only for the features array
    JsonArray features=jo.get("features").getAsJsonArray();

    for (JsonElement feature: features) {
         Message msg=new DefaultMessage();
         JsonObject jof=feature.getAsJsonObject();

         // get the key
         String id=jof.get("id").getAsString().toString();

         System.out.print(id);
         msg.setHeader(RedisConstants.KEY, id);
         msg.setHeader(RedisConstants.VALUE, jof.toString());
         answer.add(msg);
    }
    return answer;
}

Everything runs smoothly, but when I check the redis db I see that the key is:

 "\xac\xed\x00\x05t\x00\nci11361338"

and the same prefix "\xac\xed\x00\x05t\x00" is in the value.

Obviously the those printed by the System.out look fine.

As you see I tried to add a serializer, a StringRedisSerializer that I defined in the Main like this:

    Main main = new Main();
main.bind("serializer", new StringRedisSerializer());

But the result is the same (also using GenericToStringSerializer).

Is there something I'm missing?

Upvotes: 1

Views: 1632

Answers (3)

Matteo Redaelli
Matteo Redaelli

Reputation: 39

In spring dsl:

  <bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

...

      <marshal ref="json" />
      <convertBodyTo type="java.lang.String" charset="UTF-8"/>
    <setHeader headerName="CamelRedis.Value">
      <simple>${body}</simple>
    </setHeader>
    <to uri="spring-redis://{{redis.host}}:{{redis.port}}?command=SET&amp;serializer=#stringRedisSerializer"/>

Upvotes: -2

Stuart Ingram
Stuart Ingram

Reputation: 101

Just ran across this today. Using Camel 2.18.0 & camel-spring-redis all you need to do is create a bean to handle the appropriate serialization and pass it in to the camel producer definition.

  @Bean
  public RedisSerializer stringSerializer() {
    return new StringRedisSerializer();
  }

The sink statement is as per your original post

 .... 
 .to("spring-redis://localhost:6379?serializer=#stringSerializer");

Upvotes: 3

Bilgin Ibryam
Bilgin Ibryam

Reputation: 3385

I had a look at the source code and it seems that the custom serializer you are specifying is set only for consumers and not for producers. So the the serializer you created is not used in your case.

In the mean time, what you can do is create a RedisTemplate in the registry and set the serializer you want to it. Then let Camel use that RedisTemplate. That should configure Camel producer with the serializer you want.

Upvotes: 1

Related Questions