Use events in SAP Hybris

Hybris featured image

1. Overview

Events are used in Hybris to play tasks that are :

  • Low priority task.
  • Task takes long time to run.
  • Task to be run in a separated thread from the main thread.

For example : send emails, persist logging data, save audit data…

This is a amplified overview of how the events mechanism work in Hybris.

Events overview in Hybris

  • EventService : Publish event using publishEvent(event) method.
  • EventListener : Receive event and run logic defined inside onEvent(event) method.
  • Event : Hold the necessary data to be passed to the EventListener.

In this article, I will show you how to use events in Hybris (create, publish and consume).

2. Implementation

2.1. Create Event

First create a class extends AbstractEvent, that will hold all the data to be passed to the EventListener.

package com.stackextend.training.core.event;

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

public class HelloWorldEvent extends AbstractEvent {
    
    private String fullName;
    
    public HelloWorldEvent(String fullName) {
        this.fullName = fullName;
    }

    public String getFullName() {
        return fullName;
    }
}

2.2. Create EventListener

Create a HelloWorldEventListener extends AbstractEventListener, and add business logic inside the onEvent(event) method.

package com.stackextend.training.core.event.listener;

import com.stackextend.training.core.event.HelloWorldEvent;
import de.hybris.platform.servicelayer.event.impl.AbstractEventListener;

public class HelloWorldEventListener extends AbstractEventListener<HelloWorldEvent> {
    
    @Override
    protected void onEvent(HelloWorldEvent helloWorldEvent) {
        
        String fullName = helloWorldEvent.getFullName();
        
        System.out.println("Hello " + fullName);
    }
    
}

Register the HelloWorldEventListener as a Spring bean.

<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore-spring.xml -->

<bean id="helloWorldEventListener"
	  class="com.stackextend.training.core.event.listener.HelloWorldEventListener"
	  parent="abstractSiteEventListener">

	<!-- inject beans here if it's needed -->

</bean>

2.3. Publish Event

Publishing the event is simple, you need just to call the eventService.publishEvent(event) with an instance of HelloWorldEvent.

@Autowired
private EventService eventService;

// create HelloWorldEvent instance
HelloWorldEvent event = new HelloWorldEvent("Mouad EL Fakir");

// Publish the event
eventService.publishEvent(event);

Refer to this article for an example on how to publish this event using Groovy script.

If everything goes well the HelloWorldEventListener will receive the event and display the Hello Mouad EL Fakir.

 

12
Leave a Reply

avatar
5 Comment threads
7 Thread replies
5 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
InduHecGotNeoAnkurMouad EL Fakir Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
narasimha
Guest
narasimha

Hi How to get Product details from orderModel.
we are finding orderModel contain entiresModel which contains productModels is it correct?
final List orderList = new ArrayList(source.getOrders());
Where I need
final List productList = new ArrayList(xxxxx..getProducts());

I need that flow. xxxx Object is ?

Ankur
Guest
Ankur

Hi,

I have a question here, how do we make sure/ configure the EventListner to be running only on the admin node.

Neo
Guest
Neo

Is it mandatory to create event and listener for sending emails with attachment.

HecGot
Guest
HecGot

Hi Mouad. Is there a way to know all events created in hybris ? Like a table or something like that? I’m trying to find out if an event is created after an promotion rule is published.

Indu
Guest
Indu

public class LoginEventService { @Autowired EventService eventService; public EventService getEventService() { return eventService; } public void setEventService(EventService eventService) { this.eventService = eventService; } public void publish(){ LoginEvent loginEvent= new LoginEvent(“Hybris event and Listener”); eventService.publishEvent(loginEvent); } } in the above code , i wrote publish(). where i have to call the publish(), if i need to call, no need means where i can see the output ,if i use println()