简体   繁体   English

Apache Camel — Hazelcast主题发布/订阅—如何在订户中序列化String消息

[英]Apache Camel — Hazelcast Topic Publish/Subscribe — how to serialize String message in Subscriber

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. 我试图按照http://camel.apache.org/hazelcast-component.html#HazelcastComponent-topic上的示例代码通过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. 在测试期间,我通过direct:inbound端点向发布者路由发送了一个字符串,例如“ {\\“ result \\”:\\“ InboundProcessor.processRequest success \\”}“。 The subscriber route was able to receive the message from topic and pass to processor bean. 订户路由能够从主题接收消息并传递给处理器Bean。 However, I was failed to get back the string properly... 但是,我无法正确取回字符串...

Here is how I implement the bean method 这是我实现bean方法的方法

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. 我确实尝试使用不同的编码(例如UTF-8 / UTF-16 ...等)来隐蔽字符串,但仍然失败。 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 . 使用hazelcast主题生产者,消息正文为Message类型。 You can get the original object by calling Message.getMessageObject() : 您可以通过调用Message.getMessageObject()获得原始对象:

public void processHazelcastMsg(Exchange inEx) throws Exception {
  String msgStr = inEx.getIn().getBody(Message.class).getMessageObject().toString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM