Configure Component Bindings to Call External Services
1.
2. Components can use bindings to call an external
service during execution.
The bindings used with a Java component bind a
Java interface, or single interface method, to an
outbound endpoint.
The external service to be called should implement
the same interface, and the component should
encapsulate a reference to that interface, which is
initialized during the bootstrap stage by the Mule
configuration builder.
The reference will be initialized using a reflective
proxy class.
3.
4. With component bindings, you can configure
multiple interfaces or a single interface with
an endpoint bound to one or more Mule
endpoints.
Mule currently supports component bindings
for Java components (the default
components in Mule) and script components,
such as Groovy or JRuby. This page
describes how to configure each.
5. Bindings can be used by components to call out to an
external service.
The bound interface is added to the component as a field
with the usual bean getter and setter methods. In the
binding configuration for the component, you can bind this
interface along with a method in the interface to a Mule
endpoint.
When that method is called, the call parameters are sent
over the Mule endpoint to another service, which may be
local or remote. A result may be returned from the flow and
passed back to the component using the return argument
of the method. This model is very similar to traditional RPC
calls.
6.
7. In the above example, the
component InvokerComponent has a field hello,
which is of typeHelloInterface, with getter and
setter methods for the field. The invoke method
will be called on the flow and calls
the hello.sayHello() method. This call will result
in another flow.
The HelloInterface is very simple with a single
method sayHello.
8.
9. You can handle data conversion when making a
call and receiving a result using the normal
transformer configuration on the endpoint. In
the above example, assume the Web service
was expecting
an org.mule.examples.bindings.WebHelloRequ
est object and returned
an org.mule.examples.bindings.WebHelloResp
onse object. You don’t want your component to
know about these external data types, so you
can configure transformers to do the conversion
when the call is made as shown in next slide :
10.
11. If the remote flow triggers an exception of
fault, this exception will get serialized back to
the local flow call and thrown.
If your service wants to handle this
exception, you must add the exception
(or java.lang.Exception) to the bound method
signature and use a try catch block as usual.
12.
13. The implementation for the component is
contained within the <script:script>element
as shown below :
return "Total: " +
AdditionService.add(1,2)