Over a million developers have joined DZone.

ColdBox Event Chaining

DZone's Guide to

ColdBox Event Chaining

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

This is probably common knowledge to seasoned ColdBox developers, but I wasn't sure how event chaining worked with runEvent, so I did a simple test.

First off you may wonder why I'd want to run multiple events from one http call, well, I have several things that need to happen overnight, such as updating exchange rates, checking product stock, deleting empty baskets etc. Each of these actions is in a seperate event (so they can be called individually). Being lazy, I didn't want to set up three seperate scheduled tasks.

Here's my test handler:

* I am a ColdBox handler
  function chaining(event)
    var rc = arguments.event.getCollection();
    runEvent( "legacy.foo" );
    runEvent( "legacy.bar" );
    runEvent( "legacy.foobar" );
  function foo(event)
    var prc = arguments.event.getCollection( private=true );
    prc.foo = true;
  function bar(event)
    var prc = arguments.event.getCollection( private=true );
    prc.bar = true;
  function foobar(event)
    var prc = arguments.event.getCollection( private=true );
    prc.foobar = "pre";
    runEvent( "legacy.nested" );
    prc.foobar = "post";
  function nested(event)
    var prc = arguments.event.getCollection( private=true );
    prc.nested = prc.foobar;

Nothing out of the ordinary going on here. As you can see I've got a chaining event which executes the other events. I've also added a runevent call inside the foobar method to see what happens. Finally I set up a chaining.cfm view file which simply dumps out the prc struct.

When I run http://localhost/myapp/index.cfm/test/chaining I get the following output.

BAR   true
FOO   true
FOOBAR   post
NESTED   pre 

So ColdBox uses the original events name to find the view template and the application flow returns to the calling method once the runEvent has executed.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}