Reading Time: 4 minutes

When you're working with , you're usually concerned about responding to messages that come in and making sure you're routing them correctly from service to service. But what if you just want to trigger a service component on a set interval? What if its method doesn't require any incoming at all?

You can use the Quartz transport to trigger a service component method at scheduled intervals. In this example, the service component has a single method that doesn't take any parameters, so we don't need to pass any data to it. We'll use the EventGeneratorJob, which automatically generates a message. By not specifying a ‘payload' element on this job, there is no data to try to match to the service method, so Mule will match the method with no arguments.

The Service Component

latest report
Learn why we are the Leaders in management and

This is our service component class implementation. Note that it has a single method, which does not accept any parameters.

public class NoArgsMethodComponent
{
    public String fireMe()
    {
        return "Bullseye!";
    }
}

Configuring the Service

In the Mule configuration file, you configure the following elements for the service:

  • A Quartz inbound endpoint that specifies the job that will trigger the message and how often to run the job.
  • The service component class containing the method you want to fire.
  • A VM outbound endpoint that specifies where to send the results from the service component's method.
<mule  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mule="http://www.mulesource.org/schema/mule/core/2.2" xmlns:quartz="http://www.mulesource.org/schema/mule/quartz/2.2" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xsi:schemalocation="
       http://www.mulesource.org/schema/mule/vm/2.2 
       http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
       http://www.mulesource.org/schema/mule/quartz/2.2 
       http://www.mulesource.org/schema/mule/quartz/2.2/mule-quartz.xsd
       http://www.mulesource.org/schema/mule/core/2.2 
       http://www.mulesource.org/schema/mule/core/2.2/mule.xsd">

    <vm:connector name="resultQueueConnector" queueevents="true">

    <vm:endpoint name="resultQueue" path="results">

    <model>
        <service name="noArgService">
            <description>This example demonstrates invoking a method on a service 
                 without any arguments. The method will get called every second. 
                 We are using the event generator job and since we do not specify a 
                 payload a NullPayload object will be used and the method on the 
                 component will be matched by Mule.
            </description>
            <inbound>
                <quartz:inbound-endpoint jobname="myServiceJob" repeatinterval="1000">
                    <quartz:event-generator-job>
                </quartz:event-generator-job></quartz:inbound-endpoint>
            </inbound>
            <component class="org.mule.test.cookbook.quartz.NoArgsMethodComponent">
            <outbound>
                <pass-through-router>
                    <vm:outbound-endpoint ref="resultQueue">
                </vm:outbound-endpoint></pass-through-router>
            </outbound>
        </component></service>
    </model>
</vm:endpoint></vm:connector></mule>

Running the Example

The following test case, available in the Mule test suite, runs the above configuration and checks the results.

package org.mule.test.cookbook.quartz;

import org.mule.tck.FunctionalTestCase;
import org.mule.module.client.MuleClient;
import org.mule.api.MuleMessage;

public class TriggerNoArgsServiceMethodTestCase extends FunctionalTestCase
{
    protected String getConfigResources()
    {
        return "org/mule/test/cookbook/quartz/trigger-no-args-method-config.xml";
    }

    public void testTrigger() throws Exception
    {
        MuleClient client =  new MuleClient();

        //Our method should have fired and we can pick up the result
        MuleMessage result = client.request("resultQueue", 2000);

        //Always check method is not null.
        assertNotNull(result);

        //Check we have a hit
        assertEquals("Bullseye!", result.getPayloadAsString());
    }
}

For more information on the Quartz transport and the Event Generator job, see the Quartz Transport page in the Mule User's Guide. (Login is required, but registration is free and only takes a moment.)