For every synchronous request there are asynchronous variants available. These do not return the CoAP response to the request issued. Instead handling the response is delegated to a handler. Besides that, the request is issued the same way the synchronous message-processor do. By using a handler the responses can be processed in another Mule-flow than where the request is made. Also the responses of many different requests can be processed by one and the same flow, by using the same handler. This can be useful when the processing of responses of a large number of different IoT devices needs to be centralised.
In the example below a Get request is issued, delegating the handling of the response.
Example 9. Async Get example
<flow name="async_get_example"> ... <set-payload value="#[NullPayload.getInstance()]" /> <set-property propertyName="coap.opt.accept" value="0" /> <coap-client:async-get config-ref="my_CoAP_Client" path="/query" responseHandler="my_handler"> <coap-client:query-parameters> <coap-client:query-parameter>hello=there</coap-client:query-parameter> <coap-client:query-parameter>how_are=you</coap-client:query-parameter> </coap-client:query-parameters> </coap-client:async-get> <set-payload value="we don't wait for an answer.."/> ... </flow>
In the example below a Put request is issued, delegating the handling of the response.
Example 10. Async Put example
<flow name="async_put_example"> ... <set-payload value="This is new content." /> <set-property propertyName="coap.opt.content_format" value="0" /> <coap-client:async-put config-ref="my_CoAP_Client" path="/test" responseHandler="my_handler" /> <set-payload value="we don't wait for an answer.."/> ... </flow>
In the example below a Post request is issued, delegating the handling of the response.
Example 11. Async Post example
<flow name="async_post_example"> ... <set-payload value="This is the new resources content." /> <set-property propertyName="coap.opt.content_format" value="0" /> <coap-client:async-post config-ref="my_CoAP_Client" path="/test" responseHandler="my_handler" /> <set-payload value="we don't wait for an answer.."/> ... </flow>
In the example below a Delete request is issued, delegating the handling of the response.
Example 12. Async Delete example
<flow name="async_delete_example"> ... <set-payload value="#[NullPayload.getInstance()]"/> <coap-client:async-delete config-ref="my_CoAP_Client" path="/test" responseHandler="my_handler" /> <set-payload value="we don't wait for an answer.."/> ... </flow>
The responses on asynchronous requests are processed by the Mule-flow that has
configured the <coap-client:handle-response/>
message-source, with
corresponding handlerName attribute. Also the
handler and asynchronous message-processor must reference the same coap-client
configuration. If there is not a matching handler an exception will occur during
initialisation of the Mule application.
The example below shows a handler that logs the characteristics of the originating request and checks whether the responses report success.
Example 13. Handle response example
<flow name="handle_response_example"> <coap-client:handle-response config-ref="my_CoAP_Client" handlerName="my_handler"/> <logger message="#['Response '+ message.inboundProperties['coap.response.code'] +' received on ' + message.inboundProperties['coap.request.code'] + ' request' + ' on ' + message.inboundProperties['coap.request.uri']" level="INFO"/> <choice> <when expression="#[ message.inboundProperties['coap.response.success']]"> <set-payload value="Response indicated success!" mimeType="text/plain"/> </when> <otherwise> <set-payload value="Response indicated failure or response timeout occurred!"/> </otherwise> </choice> ... </flow>