0

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) を実行したいと思います。でも、春はどうしたらいいのかわからない。誰でも私を助けることができますか?

4

1 に答える 1

0

最後に私は答えを得ました。私は他の人のためにそれを入れています。

<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 は、メッセージ タイプをヘッダーに明示的に配置します。

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

人々が私の質問に答えない理由がわかりません!!

于 2013-10-26T08:46:12.800 に答える