Use events in SAP Hybris

Hybris Logo

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.

 

4.4 5 votes
Article Rating
Subscribe
Notify of
guest

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

15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
narasimha
narasimha
6 years ago

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
Ankur
6 years ago

Hi,

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

Neo
Neo
6 years ago

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

Neo
Neo
Reply to  Mouad EL Fakir
6 years ago

Thanks a lot Mouad , If you can help I have one more doubt
I am printing values in my email template using $xyz.abc whose value I am setting in email context file.So if there is no value set by default its printing the whole key i.e $xyz.abc. In this case I have to deliberately set empty string for each value How can I avoid this.
Thanks

HecGot
HecGot
6 years ago

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
Indu
5 years ago

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()

Ravi
Ravi
3 years ago

Hi mouad,

I have doubt like why should we use event and event listener, we can do same by calling spring integration bean. Can you elobrate on this?

gabriel
gabriel
1 year ago

Hi! How to trigger a Business Process with an Event?

Abhinna
Abhinna
5 months ago

We can start the process anywhere, but I don’t understand why we use event listeners in our project, what is the need of event listeners?

15
0
Would love your thoughts, please comment.x
()
x