Events

Events are meant for exchanging information between loosely coupled components. Hybris event system is based on the spring event framework. Hybris events allow us to publish and listen to specific events for perform business logic depending on the event that occured. Events can be published either locally or across cluster nodes.

Events are good to use when we want to pass on the processing to another thread. we use event for less priority task or long time running task or something which can be done in parallel by separated thread from the main thread. For example triggering of email, sending report to third party server, adding logs etc.

Events Mechanism

Let’s see how we can create, publish custom events and how to create event listener.

Create Custom Event

Event is hold the necessary data to be passed to the EventListener. It is define by a class which extends AbstractEvent. This class will hold all the data which will be passed to the EventListener.

package com.experts.hybris.core.event;

import de.hybris.platform.servicelayer.event.events.AbstractEvent;


public class ExpertHybrisEvent extends AbstractEvent {

private String name;

public ExpertHybrisEvent(String name) {

this.name = name;

}

public String getName(){

return name;

}

}

Create EventListener

EventListener will listen to trigger event and run business logic which is define inside onEvent(event) method. The easiest way to create an event listener is to extend the AbstractEventListener class and override its onEvent() method.

package com.experts.hybris.core.listener;


import com.experts.hybris.core.event.ExpertHybrisEvent;

import de.hybris.platform.servicelayer.event.impl.AbstractEventListener;

public class ExpertHybrisEventListener extends AbstractEventListener<ExpertHybrisEvent> {

@Override

protected void onEvent(ExpertHybrisEvent expertHybrisEvent) {

// write business logic here.

System.out.println("Hi " + expertHybrisEvent.getName());

}

}

Register EventListener

Register event listener as spring bean in <extension>core-spring.xml(like trainingcore-spring.xml) file.

<bean id="expertHybrisEventListener" class="com.experts.hybris.core.listener.ExpertHybrisEventListener" parent="abstractSiteEventListener">

</bean>

Publish Event

Publishing the event is easy. we just need to call publishEvent(event) method of EventService class with instance of event. we will call the eventService.publishEvent(event) with an instance of ExpertHybrisEvent.


//inject eventService in your class

@Resource

private EventService eventService;


// create ExpertHybris instance

ExpertHybrisEvent event = new ExpertHybrisEvent ("Experts Hybris Website");


// Publish the event

eventService.publishEvent(event);

When you will execute this program, ExpertHybrisEventListener will receive the ExpertHybrisEvent event and display the name .

Output :"Hi Experts Hybris Website".

After Save Event

Interceptors can interrupt the lifecycle of a model and execute some logic. Interceptor work before save in database. But sometimes, we have requirement to perform some business operation after save of any model. After each database operation (whether caused by a committed transaction or not), Hybris is created one event. This event contains information about the item that was added or updated. we can collect those events and perform our business logic.

AfterSaveEvent class that holds the PK of a modified item and Type of a database operation. It could be CREATE, UPDATE, or REMOVE. we can collect those events and perform our logic with help to implement our listener using the AfterSaveListener interface.

Listener can be notified about newly created events in two modes: synchronous and asynchronous. The asynchronous mode is enabled by default. we can enable synchronous mode by using below properties. We will add this properties in local.properties.

core.aftersave.async=false

Creating Custom Listener for After Save (AfterSaveListener )

All listeners are notified with the same collection of events. We need to create a listener to collect specific events and handle business logic. For creating custom listener, We need to implement the AfterSaveListener interface and override afterSave method. There is no default implementation available.

public class CustomAfterSaveListener implements AfterSaveListener

{

@Resource

private ModelService modelService;


@Override

public void afterSave(final Collection<AfterSaveEvent> events)

{

for (final AfterSaveEvent event : events)

{

final int type = event.getType();

//As per requirement, we can match AfterSaveEvent. it could be CREATE , UPDATE or REMOVE

if (AfterSaveEvent.UPDATE == type || AfterSaveEvent.CREATE == type)

{

final PK pk = event.getPk();

//we can find type code from *-item.xml which is assign in deployment code and match from code.

//type code value will be unique for each item. CategoryModel type code is 142

if (142 == pk.getTypeCode())

{

final CategoryModel category = (CategoryModel)modelService.get(pk);

//we will add our custom business code here

}


//Same add other item in same listener or create new listener for item. ProductModel type code is 1

if (1 == pk.getTypeCode())

{

final ProductModel product = (ProductModel)modelService.get(pk);

//we will add our custom business code here

}

}

}

}

}

We need to register event listener as spring bean in <extension>core-spring.xml(like trainingcore-spring.xml) file.

<bean id="customAfterSaveListener" class="com.experts.hybris.listener.CustomAfterSaveListener" >

</bean>

After write this code , We can perform any logic on after save event.