【问题标题】:Getting data from GSON parsor to Camel Spring从 GSON 解析器获取数据到 Camel Spring
【发布时间】:2013-10-24 04:12:26
【问题描述】:

我正在使用 Spring DSL 将 JSON 格式的数据获取到 Camel。 我是这样写代码的,

<bean id="mqtt" class="org.apache.camel.component.mqtt.MQTTComponent"/>
    <bean id="gson" class="org.apache.camel.component.gson.GsonDataFormat"/>
    <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
        <camel:route>
            <camel:from uri="mqtt:bar?host=tcp://10.30.11.0:1883&amp;subscribeTopicName=apss/messages" />
            <unmarshal ref="gson" />
            <camel:choice>
                <camel:when>
                    <!-- I dont knwo what to write here -->
                    <camel:to uri="stream:out" />
                </camel:when>
                <camel:otherwise>
                    <camel:to uri="stream:out" />
                </camel:otherwise>
            </camel:choice>
        </camel:route>
    </camel:camelContext>

我想将第一个字段与字符串进行比较,然后决定解析后做什么。 我知道 GSON 会将 JSON 字符串解析为哈希图。所以我想用哈希图做一个 get(0) 。 但我不知道如何在春天做到这一点。有人可以帮帮我吗?

【问题讨论】:

    标签: java spring gson apache-camel


    【解决方案1】:

    我终于得到了答案。 我是给别人放的。

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="mqtt:apsssub?host=tcp://10.30.11.0:1883&amp;subscribeTopicName=apss/messages" />
                <unmarshal ref="gson" />
                <bean beanType="com.hk.MessageHeaderSetter" method="putMessageTypeInHeader" />
                <choice>
                    <when>
                        <simple>${headers.msg_type} == 'location_update'</simple>
                        <log message="Message type 1." />
                        <to uri="jms:LOCATION_UPDATE_QUEUE" />method="updateLocation" />
                    </when>
                    <otherwise>
                        <log message="Other message" />
                        <log message="Headers ${headers}" />
                        <to uri="stream:out" />
                    </otherwise>
                </choice>
        </route>
    </camelContext>
    

    这里MessageHeaderSetter中的putMessageTypeInHeader会显式的将消息类型放入header中。

    public void putMessageTypeInHeader(Exchange exchange) {
            exchange.getIn().setHeader("msg_type", ((HashMap)exchange.getIn().getBody()).get("msg_type"));
    }
    

    我不知道为什么人们不回答我的问题!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多