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.


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 7: Bootstrap, Effective, Architectures, Web, React and Angular, Testing and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

Very useful!
For the 2nd benefit, the monitoring exposition, is it for the last versions of JBoss and Glassfish?
And is it by JMX?
Thanks!

Posted by Fred on September 24, 2017 at 05:16 PM CEST #

An example of a database/ configurable timer/scheduler would be greatly apreciated

Posted by litts on September 25, 2017 at 07:32 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License