Creating a Job

After you define your job, you need to create an instance of your job and set the properties before you dispatch it onto your Queue Connection. You can do this is a few different ways.

Creating a Job Instance

You can create a Job instance using WireBox anywhere in your ColdBox application.

component {

    function index( event, rc, prc ) {
        var job = getInstance( "SendWelcomeEmailJob" );
    }

}

Keep in mind that Job instances are transient. Do not inject a Job instance into a Component or Scope that is not transient. For instance, do not inject a Job instance as a property in a Handler.

Setting Job Properties

Once you have a Job instance, you can set the data for the particular Job by calling the setter methods.

component {

    function index( event, rc, prc ) {
        var job = getInstance( "SendWelcomeEmailJob" );
        job.setEmail( "john@example.com" );
        job.setGreeting( "Welcome!" );
    }

}

setProperties

You can also set all the properties at once using the setProperties method.

Using this method will overwrite any previously set properties.

component {

    function index( event, rc, prc ) {
        var job = getInstance( "SendWelcomeEmailJob" );
        job.setProperties( {
            "email": "john@example.com",
            "greeting": "Welcome!"
        } );
    }

}

onConnection

You can override the connection for a job by calling the onConnection method with the desired connection name.

setConnection

See onConnection.

onQueue

The queue name to use for the job. Queue names can be any string you choose, but to be worked a WorkerPool must be defined working the same connection and queue name.

setQueue

See onQueue.

setBackoff

Sets the amount of time, in seconds, to wait in-between Job attempts — including the initial attempt.

setDelay

An alias for setBackoff.

setTimeout

Sets the amount of time, in seconds, to let a Job run on a worker before marking it as a failed attempt.

setMaxAttempts

Sets the maximum number of attempts before a Job is marked as failed.

chain

Sets an array of Jobs to be executed, in order, after this Job successfully executes.

getMemento

getMemento

You can also create a Job instance using the cbq model. (This can be injected into your components using the cbq@cbq DSL.)

The cbq model is a singleton, so feel free to inject it into any component.

component {

    property name="cbq" inject="cbq@cbq";
    
    function index( event, rc, prc ) {
        var job = cbq.job(
            job = "SendWelcomeEmailJob",
            properties = {
                "email": "john@example.com",
                "greeting": "Welcome!"
            },
            chain = [],
            queue = "priority",
            connection = "db",
            backoff = 10,
            timeout = 30,
            maxAttempts = 3
        );
    }

}

Last updated