JOB QUEUES

CBQ-L logo

ColdBox can work with job queues via its cbq module: install cbq. Queues allow you to push work to the background, schedule work to be done later, or even process work on many different machines. It runs on a provider-based system allowing a unified API to talk with many different queue backends.

install cbq

Create a Worker Pool

This allows our application to work the Jobs we will dispatch.

    component {
      function configure() {
        newConnection( "default" )
        .setProvider( "ColdBoxAsyncProvider@cbq" )

        newWorkerPool( "default" )
        .forConnection( "default" )
      } 
	}

Easily Create Jobs

    // models/jobs/emails/SendWelcomeEmailJob.cfc
    component extends="cbq.models.Jobs.AbstractJob" {

      property name="userId"

      function handle() {
      log.info( "Sending a Welcome email to User #getUserId()#" )

      var user = getInstance( "User" ).findOrFail( getUserId() )

      getInstance( "MailService@cbmailservices" )
        .newMail(
        from = "no-reply@example.com",
        to = user.getEmail(),
        subject = "Welcome!",
        type = "html"
        )
        .setView( "/_emails/users/welcome" )
        .setBodyTokens( {
        "firstName" : user.getFirstName(),
        "lastName" : user.getLastName()
        } )
        .send()
      }
	}
  

Dispatch Your Jobs

    getInstance( "SendWelcomeEmailJob" )
      .setUserId( prc.newUser.getId() )
      .dispatch();

Find out much more about work queues in ColdBox in the cbq documentation.