我正在玩骆驼和redis。我的路线非常短:
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");
splitMessage的位置如下:
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;
}
一切都运行顺利,但当我检查redis db时,我看到关键是:
"\xac\xed\x00\x05t\x00\nci11361338"
和值相同的前缀"\xac\xed\x00\x05t\x00"
。
显然,System.out打印的那些看起来很好。
如你所见,我试图添加一个序列化器,一个我在Main中定义的StringRedisSerializer,如下所示:
Main main = new Main();
main.bind("serializer", new StringRedisSerializer());
但结果是一样的(也使用GenericToStringSerializer)。
有什么我想念的吗?
今天就碰到了这个。使用Camel 2.18.0和camel-spring-redis你需要做的就是创建一个bean来处理相应的序列化并将其传递给camel生成器定义。
@Bean
public RedisSerializer stringSerializer() {
return new StringRedisSerializer();
}
接收器声明与原始帖子一致
....
.to("spring-redis://localhost:6379?serializer=#stringSerializer");
我看了一下源代码,看来你指定的自定义序列化程序只针对消费者而不是生产者。因此,您创建的序列化程序不会用于您的情况。
与此同时,您可以在注册表中创建RedisTemplate并设置您想要的序列化程序。然后让Camel使用RedisTemplate。这应该配置Camel生产者你想要的序列化器。
在春季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&serializer=#stringRedisSerializer"/>