环境 | 版本 |
---|---|
mule-standalone | 3.9.0 |
anypoint-studio | 6.4.0 |
MULE提供了AMQP Connector,因此可以使用该Connector来配置RabbitMQ。在Anypoint-studio中的Exchange中也可以找到AMQP的例子。
* (1)下载AMQP Connector 点击下载 , 当前Connector版本为3.7.7,下载需要注册账号,下载后的zip包可以通过Anypoint-studio的Help->Install New Software安装。
* (2)上面说到,官方提供了示例 示例地址 , 通过Anypoint-studio的Exchange打开该项目后,并且按照示例文档配置项目,配置RabbitMQ(配置一个Exchange名为salesexchange
,配置一个Queue名为salesqueue
,再将这个Queue绑定在Exchange中,以上步骤RabbitMQ控制台即可完成)。 项目启动出现报错,报错主要内容如下:
schema_reference.4: 无法读取方案文档 'http://www.mulesoft.org/schema/mule/amqp/current/mule-amqp.xsd',
原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>。
官方例子出错让人无语,错误很明显提示没有mule-amqp.xsd
这个文件,访问该路径的确是404,谷歌一番外国友人回答是maven依赖错误或者jar包错误,尝试将项目添加maven support
后添加maven依赖,仍然出现该错误。反复尝试,我认为可能是xml配置的问题,因此重新创建一个Project并且完全按照官方例子搞了一个示例。需要注意:配置AMQP的时候,需要配置一个Connector Reference,不然会提示没有指定host
,最后启动项目,完美运行,按照官方例子,往http://localhost:{your_port}/
发送json请求,最后在RabbitMQ控制台的salesqueue队列中获取到了发送的信息。至此示例完成。
{
"ITEMID": "001",
"ITEMNAME": "Shirt",
"QTY": 1,
"PRICE": 20
}
相关截图:
相关参考内容:
外国友人回答
简单的发送消息和接受消息示例
* 添加一个RabbitMQ的QUEUE,名称为test,其它默认即可。
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:amqp="http://www.mulesoft.org/schema/mule/amqp" xmlns:http="http://www.mulesoft.org/schema/mule/http"
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.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
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/amqp http://www.mulesoft.org/schema/mule/amqp/current/mule-amqp.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<amqp:connector name="AMQP_0_9_Connector" validateConnections="true" doc:name="AMQP-0-9 Connector"/>
<flow name="SendMessageToQueue">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="POST" doc:name="HTTP"/>
<amqp:outbound-endpoint queueName="test" queueDurable="true" responseTimeout="10000" connector-ref="AMQP_0_9_Connector" doc:name="AMQP-0-9"/>
<set-payload value="#['Send Message']" doc:name="Set Payload"/>
</flow>
<flow name="GetMessageFromQueue">
<amqp:inbound-endpoint queueName="test" queueDurable="true" responseTimeout="10000" exchange-pattern="request-response" connector-ref="AMQP_0_9_Connector" doc:name="AMQP-0-9"/>
<byte-array-to-object-transformer doc:name="Byte Array to Object"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>