Coffee Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Qualifier | |
@Retention(RUNTIME) | |
@Target({TYPE, METHOD, FIELD, PARAMETER}) | |
public @interface CoffeeOrdered { | |
} | |
public class Waiter { | |
@CoffeeOrdered | |
@Inject | |
Event<Coffee> orderCoffee; | |
public void takeOrder(Coffee coffee) { | |
orderCoffee.fire(coffee); | |
} | |
} | |
public class Barista { | |
public void prepareCoffee(@CoffeeOrdered @Observes Coffee coffee) { | |
.... | |
} | |
} |
Cache based Coffee Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Qualifier | |
@Retention(RUNTIME) | |
@Target({TYPE, METHOD, FIELD, PARAMETER}) | |
public @interface CoffeeOrdered { | |
} | |
public class Waiter { | |
@Inject | |
@CoffeeOrdered | |
Cache<String, Coffee> orderedCoffees; | |
public void takeOrder(Coffee coffee) { | |
orderedCoffees.put(UUID.randomUUID().toString(), coffee); | |
} | |
} | |
public class Barista { | |
@Inject | |
@CoffeeOrdered | |
Cache<String, Coffee> orderedCoffees; | |
public void prepareCoffee(@CoffeeOrdered @Observes CacheEntryCreatedEvent coffee) { | |
if(haveTime()) { | |
prepareDelicious(coffee); | |
} else { | |
// continue doing something else | |
// later use orderedCoffees methods, for example orderedCoffees.keySet() | |
} | |
} | |
... | |
} |
Beyond good espressos
As you can see - introducing CDI improved the service a lot. Now Waiter does not hurry Barista with the orders. This is why they serve the best espresso in the world there...
They have also a lot more time to think about other improvements (and to be honest... I think they will introduce CacheEntryModifiedEvent, CacheEntryRemovedEvent and CacheStartedEvent really shortly)... Or perhaps they'll find some other ideas in Infinispan's manual?