Contact Us 1-800-596-4880

Simple Service Pattern

The goal of the Simple Service pattern is as simple as its name suggests: to provide a simple way to expose request-response services. In other terms, it is used to expose some business logic in a synchronous manner over the transport of your choice.

mcp_simple-service

Core Features

Simple Service allows the deployment of SOA-style services on Mule in a short and elegant manner. Without further ado, let’s delve into examples that will illustrate the extensive capacities of this new configuration element.

Exposing a POJO as a Simple Service

<pattern:simple-service name="maths-service"
                address="vm://maths.in"
                component-class="org.mule.tck.services.SimpleMathsComponent" />

That is all! An instance of SimpleMathsComponent is now accessible via a VM endpoint, which has been automatically configured to be synchronous (i.e. request-response). As usual, Mule will use a resolution strategy to find the best method for receiving the payload that has been sent to the service.

Of course, referencing global endpoints and components is also possible:

Using References

<pattern:simple-service name="maths-service"
                endpoint-ref="maths-service-endpoint"
                component-ref="math-component" />

By the same token, transformers can be configured on the service:

Transformers

<pattern:simple-service name="byte-array-massager"
                address="vm://bam.in"
                transformer-refs="byte-array-to-string append-bar"
                responseTransformer-refs="append-baz string-to-byte-array"
                component-class="org.mule.component.simple.EchoComponent" />

Simple Service can also work with component configuration elements from any supported schema. The following shows a very convenient way to configure server stubs during functional tests:

Functional Test Service

<pattern:simple-service name="functional-test-component"
                address="vm://ftc.in">
  <test:component />
</pattern:simple-service>

Similarly, inbound endpoint and exception strategy can both be configured as child elements:

Child Elements

<pattern:simple-service name="maths-service"
                component-class="org.mule.tck.services.SimpleMathsComponent">
  <vm:inbound-endpoint path="maths.in"
                       exchange-pattern="request-response" />
  <custom-exception-strategy class="com.acme.AcmeExceptionStrategy" />
</simple-service>

Simple Service, like all configuration pattern elements, supports inheritance the same way standard Spring beans do. Inheritance can be used for sharing common settings across configuration elements. This is illustrated hereafter:

Inheritance

<pattern:simple-service name="global-exception-strategy"
                abstract="true">
  <custom-exception-strategy class="com.acme.AcmeExceptionStrategy" />
</pattern:simple-service>

<pattern:simple-service name="inherited-exception-strategy"
                parent="global-exception-strategy"
                address="vm://maths.in"
                component-class="org.mule.tck.services.SimpleMathsComponent" />

Note: Inheritance of a nested 'component' element is not supported and as such cannot be defined on an abstract simple-service.

SOAP Services

Simple Service allows you to expose JAX-WS annotated components as SOAP services. All you need to do is add a type attribute and your component can be invoked with SOAP calls.

Here is an example:

JAX-WS Service

<pattern:simple-service name="weather-forecaster-ws"
                address="http://localhost:6099/weather-forecast"
                component-class="org.mule.test.integration.tck.WeatherForecaster"
                type="jax-ws" />

Note that if the transport used is HTTP, Simple Service will happily serve a WSDL document for requests whose paths end with "?WSDL".

RESTful Services

Simple Service is also capable of exposing JAX-RS annotated components as RESTful services.

Again, using the relevant type is the only effort needed to start serving RESTful resources, as shown here:

JAX-RS Service

<pattern:simple-service name="weather-report-rsc"
                address="http://localhost:6099/rest"
                component-class="org.mule.test.integration.tck.WeatherReportResource"
                type="jax-rs" />

If the component in the above configuration is annotated with @Path("/weather-report"), then its resources will be reachable under this URI: http://localhost:6099/rest/weather-report.

JAX-RS services work with the HTTP and Servlet transports.

JAXB Support

Simple Service can handle JAXB serialized payloads. Nothing special is needed in the XML configuration:

JAXB Serialized Service

<pattern:simple-service name="weather-jaxb-consumer"
                address="vm://weather-consumer.in"
                component-class="org.mule.test.integration.tck.WeatherReportConsumer" />

But, for this to work, it is required that @Payload, a Mule-specific annotation, is used on the component:

Annotated Service Component for JAXB Payloads

package org.mule.test.integration.tck;

import org.mule.api.annotations.param.Payload;

public class WeatherReportConsumer
{
    public String consume(@Payload WeatherReportType weatherReport)
    {
        return weatherReport.report;
    }
}

XPath Support

Finally, Simple Service can also handle XML payload with a direct extraction of values via XPath expressions. Like with JAXB, nothing special is needed in XML:

XPath Payload Service

<pattern:simple-service name="weather-xpath-consumer"
                address="vm://weather-xpath-consumer.in"
                component-class="org.mule.test.integration.tck.WeatherReportXpathConsumer" />

But again, a Mule annotation, @XPath in this case, is needed for this to work:

Annotated Service Component for XPath Payloads

package org.mule.test.integration.tck;

import org.mule.api.annotations.param.Payload;

public class WeatherReportConsumer
{
    public String consume(@Payload WeatherReportType weatherReport)
    {
        return weatherReport.report;
    }
}