Mule 基本流导致无效负载

Mule 基本流导致无效负载,mule,anypoint-studio,Mule,Anypoint Studio,我是Mule的新手,我正在尝试从《Mule在行动中工作》一书中获得第一个例子 我正在使用Mule 3.9和Anypoint Studio 6.4.1。在第1章中,他们描述了我创建的一个非常基本的产品注册流程,如下所示: <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.

我是Mule的新手,我正在尝试从《Mule在行动中工作》一书中获得第一个例子

我正在使用Mule 3.9和Anypoint Studio 6.4.1。在第1章中,他们描述了我创建的一个非常基本的产品注册流程,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core 
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http 
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jms 
http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8880" basePath="products" doc:name="HTTP Listener Configuration"/>
    <jms:activemq-connector name="Active_MQ" username="admin" password="admin" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
    <flow name="product_registrationFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <logger level="INFO" doc:name="Logger Before"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger level="INFO" doc:name="Logger After"/>
        <jms:outbound-endpoint doc:name="JMS" queue="products"/>
    </flow>
</mule>
    MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
    source.setProperty("Content-Type", "application/json", PropertyScope.INBOUND);
    source.setProperty("Content-Length", Integer.valueOf(productAsJson.length()), PropertyScope.INBOUND);
当我运行测试时,它在最后一次断言时失败,因为实际有效负载是:

{NullPayload}

如果我直接查看ActiveMQ,我会看到该负载。如果我使用Chrome中的Poster之类的工具手动发布到Mule,只设置标题内容类型:application/json,则有效负载是有效的json,我可以让测试通过,因为它从Poster发布的队列中获取挂起的消息,并且它创建的消息位于有效负载为{NullPayload}的队列末尾

有人能解释一下为什么从JUnit测试调用流时失败,但使用Poster之类的工具调用流时似乎有效吗

更新:在Pierre B的帮助下,我让它工作了。FunctionalTestCase中MuleMessage的初始化更新如下:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core 
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http 
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jms 
http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8880" basePath="products" doc:name="HTTP Listener Configuration"/>
    <jms:activemq-connector name="Active_MQ" username="admin" password="admin" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
    <flow name="product_registrationFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <logger level="INFO" doc:name="Logger Before"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger level="INFO" doc:name="Logger After"/>
        <jms:outbound-endpoint doc:name="JMS" queue="products"/>
    </flow>
</mule>
    MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
    source.setProperty("Content-Type", "application/json", PropertyScope.INBOUND);
    source.setProperty("Content-Length", Integer.valueOf(productAsJson.length()), PropertyScope.INBOUND);

您说过,当您使用外部工具而不是MuleClient发布消息时,它会起作用:

使用Chrome中的Poster之类的工具,只设置标题内容类型:application/json

尝试将相同的标题添加到多消息中,例如:


编辑:正如@sceaj指出的,内容类型和内容长度标题都是必需的

如果我直接在ActiveMQ中查看,我会看到有效负载,您的意思是在JUnit中运行测试后,您会在ActiveMQ中看到NullPayload,或者您会看到JSON内容?因为它正在从Poster发布的队列中获取挂起的消息,并且它创建的消息位于负载为{NullPayload}的队列末尾。您的意思是,您首先使用Poster发布了一条消息,然后运行了测试,它通过了测试,但NullPayload消息仍保留在队列中?是,如果我使用ActiveMQ管理控制台查看ActiveMQ,则使用测试用例创建的消息的有效负载实际上是{NullPayload},但是如果我运行Mule并使用Poster将相同的JSON发送到相同的HTTP入站端点,则JSON会在ActiveMQ管理控制台中正确显示。您对如何让测试通过的理解也是正确的-队列变得无序,测试发布带有{NullPayload}的消息,然后读取一条与Poster一起发布的消息,并通过-来自测试的消息保留在队列中。好的,这可能是由于您使用MuleClient生成消息的方式造成的,Mule可能无法正确识别该消息。输出前/输出后的日志是什么?如果未给出任何信息,请配置记录器,以使用message=[message]或message=[payload]输出当前消息或有效负载,谢谢@PierreB。我将对此进行尝试,并向您报告-我同意您的逻辑,并尝试过调试-我在构建后立即看到了正确的有效负载和原始有效负载,但我无法花足够的时间了解调度呼叫中发生了什么。我怀疑这与Mule不知道MuleMessage包含JSON有关?正是我的想法,我刚刚添加了一个关于此的答案。您可能需要在MuleMessage dispath调用中指定内容类型。有关如何传递头的详细信息,请参阅。我刚刚尝试了此方法,但不幸的是,它无法说明实际JMS消息内容仍然是{[NullPayload]}。今天晚些时候我将尝试日志记录并进行更新。比较日志记录的输出,除了内容类型之外,我还需要添加内容长度标题。如果你更新你的答案@PierreB,我会接受它。很高兴看到它起作用;我按照你的建议编辑,谢谢