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 MUC Airport Workshop: Migrating Java Client (Swing / Java FX) to Web Standards

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

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 #

Is there any mechanism to feed the schedule data programmatically? We would like to get schedule out of java class, in order to be flexible for productive / integration environments regarding timing / disabling. Furthermore, we would like to benefit from the cluster-aware J2EE timer mechanism. Other mechanisms such as ManagedScheduledExecutorService might not be robust within clusters...

Posted by Michael Veit on June 04, 2018 at 03:06 PM CEST #

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