Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
101 lines (80 sloc) 2.06 KB

Event in ActFramework

ActFramework provides utmost expressive way to bind and dispatch events.

Simple Event Framework

Simple event framework allows developer to use any String as the event to trigger and bind event handlers. And event handler could be simple as a public method that are annotated with act.event.On annotation:

Declaring event handler

public class Foo {
    @On(value = "customer-created", async = true)
    public void sendWelcomeEmail(Contact newCustomer) {
        ...
    }
}

Trigger event

@Controller("/customer")
public class CustomerController {

    @PostAction("/")
    public void createCustomer(Customer customer, @Context EventBus eventBus) {
        customerDao.save(customer);
        eventBus.trigger("customer-created", customer);
    }
}

Typesafe Event Framework

Typesafe event framework is more classic way to bind and dispatch event.

Declaring event handler

import act.event.ActEvent;
import act.event.ActEventListenerBase;
import act.util.Async;

@Async
public class CustomerCreated extends ActEventListenerBase<ActEvent<Customer>> {
    @Override
    public void on(ActEvent<Customer> event) throws Exception {
        Customer customer = event.source();
        // send welcome email to customer
    }
}

Trigger event

@Controller("/customer")
public class CustomerController {

    @PostAction("/")
    public void createCustomer(Customer customer, @Context EventBus eventBus) {
        customerDao.save(customer);
        eventBus.trigger(new ActEvent<Customer>(customer));
    }
}

Comparison between two event framework

ProsCons
Simple event framework Simple, lightweight and very expressive to declare event handler Not type safe
reflection based method call
Typesafe event framework Typesafe; better runtime performance Slightly verbose to declare event handler

Back to index