We humans are known to celebrate certain milestones in our life journey. We do celebrate birth, tying knots etc. Similarly, in the buying journey of a customer, there are few milestones which are worth celebrating or react to. Order placement, registration of a customer is few of them. The reaction could be about sending a welcome email, or sending the order data to a third party system for fulfillment.
Further, let’s say, a customer registered on a web site, and wants to start browsing the cool products. But the lousy code of sending a fancy welcome email with a promotional voucher in it, took around one minute. He will regret his decision to register, and will walk away.
Spring based events, provides the exact same infrastructure. So now we know, whenever we have a situation where some lousy code is to be executed after some thing happens (an event), we will rely on events.
First we need to create an Event class, that will hold the necessary data to pass to the listener.
protected AbstractCommerceUserEvent initializeEvent(final AbstractCommerceUserEvent event, final CustomerModel customerModel)
Spring provides a way to publish an event.
getEventService().publishEvent(initializeEvent(new RegisterEvent(), customerModel));
There are dedicated listeners lying around, who listens to these wishes, and reacts the way, they are programmed.
Listeners can be bonded to publishing services via common event object.
// Do whatever you want. send email/voucher or whatever
Please note that since, listener code starts in a new thread, it will not hamper customer journey on your site. The listener code will execute as a back end process.