Invoking a service method in Mule

motif

At TSSJS last week I had a conversation with a user that was having a problem invoking more than one method on a service component (just a POJO object). His scenario was that he had a service with multiple inbound endpoints and a service component with multiple methods, some with matching parameter types. Existing users will be aware that will match methods against the parameters received in the current message. Thus is two methods have the same parameters cannot match the method to invoke.

Well Mule can do it. By adding a ‘method’ property to the inbound endpoint or even as a message header, Mule will automatically use the name specified in the ‘method’ property and nook that method instead.

For example say I have a service component called StringMangler with a couple of methods –

And we have a service definition that has two inbound endpoints that will receive requests to be processed by the StringMangler component.

This illustrates the problem that Mule will not be able to match requests to method calls since the request endpoints will both be receiving the same data type, (String) and both of our component methods also accept a String.

The way around this is to add a ‘method’ property to the endpoints which will determine which method to call on the StringMangler component.

The ‘method’ property can also be set on the inbound message so it would be possible to have a single endpoint for both methods on the StringMangler component. Furthermore, the property on the message would override the same property on the endpoint so it would be possible to set a default ‘method’ property on the endpoint but allow individual requests to override it.


We'd love to hear your opinion on this post


3 Responses to “Invoking a service method in Mule”

  1. Hi Ross,

    Does not seem to work for me..

    First I get
    ERROR 2010-04-02 00:30:23,480 [main] org.mule.MuleServer:
    ********************************************************************************
    * A Fatal error has occurred while the server was running: *
    * cvc-complex-type.3.2.2: Attribute ‘name’ is not allowed to appear in *
    * element ‘property’. (org.xml.sax.SAXParseException) *
    * *
    * The error is fatal, the system must shutdown *
    * Server started: 1/1/70 1:00 AM *
    * Server shutdown: 4/2/10 12:30 AM *
    ********************************************************************************

    Changing the attribute ‘name’ into ‘key’ (as per xsd), I still get:
    ERROR 2010-04-02 00:31:30,227 [UppercaseUMO.2] org.mule.service.DefaultServiceExceptionStrategy:
    ********************************************************************************
    Message : [
    The required property “method” is not set on the event
    CallableEntryPointResolver:Object “org.mule.example.echo.Uppercasify@5578920a” does not implement required interface “interface org.mule.api.lifecycle.Callable”
    Found too many possible methods on object “org.mule.example.echo.Uppercasify” that accept parameters “{class java.lang.String}” using resolver “ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=true, acceptVoidMethods=false}”
    Found too many possible methods on object “org.mule.example.echo.Uppercasify” that accept parameters “{class java.lang.String}” using resolver “ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=false, acceptVoidMethods=false}”
    ]
    Type : org.mule.model.resolvers.EntryPointNotFoundException
    Code : MULE_ERROR–2
    JavaDoc : http://www.mulesource.org/docs/site/current2/apidocs/org/mule/model/resolvers/EntryPointNotFoundException.html
    ********************************************************************************

  2. Right, i have the same error.
    It works if you take off the property but you have to have only one method in your class implemented by an interface.

    The problem encountered is how to choose the method to use.

  3. How would one set this method property on a message and not on an endpoint?