I was trying to follow the sample code at http://camel.apache.org/hazelcast-component.html#HazelcastComponent-topic to test with publish/subscribe through Hazelcast.
Below listed the publisher and subscriber routes definitions
<route>
<from uri="direct:inbound" />
<setHeader headerName="CamelHazelcastOperationType">
<simple>${type:org.apache.camel.component.hazelcast.HazelcastConstants.PUBLISH_OPERATION}</simple>
</setHeader>
<to uri="hazelcast:topic:foo" />
</route>
<route>
<from uri="hazelcast:topic:foo" />
<log message="from hazelcast topic:= ${body}" />
<bean ref="inboundProcessor" method="processHazelcastMsg" />
</route>
During my test, I sent a String like "{\\"result\\": \\"InboundProcessor.processRequest success\\"}" to publisher route through direct:inbound endpoint. The subscriber route was able to receive the message from topic and pass to processor bean. However, I was failed to get back the string properly...
Here is how I implement the bean method
public void processHazelcastMsg(Exchange inEx) throws Exception{
Map<String, Object> headers = inEx.getIn().getHeaders();
System.out.println("Exchange > In msg > Body = " + inEx.getIn().getBody());
System.out.println("Exchange > In msg > Body class = " + ObjectHelper.className(inEx.getIn().getBody()));
DataAwareMessage msg = inEx.getIn().getBody(DataAwareMessage.class);
byte[] b = serializeObject(msg.getMessageObject());
String msgStr = new String(b, Charset.forName("utf-8"));
System.out.println("Received msg string = " + msgStr);
}
private static byte[] serializeObject(Object object) throws IOException
{
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(object);
return bos.toByteArray();
}
}
and here is the log output:
[ hz._hzInstance_1_dev.event-5] route4 INFO from hazelcast topic:= com.hazelcast.topic.impl.DataAwareMessage[source=foo]
Exchange > In msg > Body = com.hazelcast.topic.impl.DataAwareMessage[source=foo]
Exchange > In msg > Body class = com.hazelcast.topic.impl.DataAwareMessage
Received msg string = ��
I did try to covert string using different encoding (like UTF-8/UTF-16... etc) but still failed. Wondering if there should be another way to get back the correct String in subscriber
With the hazelcast topic producer, the message body is of type Message . You can get the original object by calling Message.getMessageObject()
:
public void processHazelcastMsg(Exchange inEx) throws Exception {
String msgStr = inEx.getIn().getBody(Message.class).getMessageObject().toString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.