adam bien's blog

Scheduler and Business Logic Separation 📎

Splitting a scheduler:


@Startup
@Singleton
public class Reminder {

    @Inject
    CoffeeBrewer brewer;

    @Schedule(minute = "*/30", hour = "*")
    public void remind() {
        this.brewer.brew();
    }
}    

...and the business logic:


@Stateless
public class CoffeeBrewer {

    public void brew() {
        this.boil();
        this.grind();
        this.deliver();
        this.pay();
    }

    void boil() {}
    void grind() {}
    void deliver() { }
    void pay() {}
}        

...into separate classes (dedicated boundary and control) allows the application server to inject proxies. Proxying comes with following benefits:

  1. Fine grained transaction management: business logic (CoffeeBrewer) can use declarative transactions as usual
  2. Monitoring: application server will expose monitoring statistics for both classes
  3. Interception: individual interceptors can be use on both: the scheduler as well as the business logic
  4. Less noise: dynamic/flexible Schedulers require additional configuration logic. With scheduler / business logic separation, the business logic class remains unaffected and clean.
  5. Easier unit testing: there is no scheduling code in the business logic

...and the downside? You have to write one more class...

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.