Reading Time: 4 minutes

When you’re working with Mule, 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 data 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 API management and iPaaS

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.


    

    

    
        
            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.
            
            
                
                    
                
            
            
            
                
                    
                
            
        
    

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.)