System Event Tutorial
SystemEvent
s represent an event that you want to execute on another Node in a Broadleaf Cluster.
In this tutorial, we will create the EchoSystemEventConsumer
which sends a message from a Site instance to an Admin instance and logs the event details.
We'll use a custom Spring-MVC controller to create the event and send it to the consumer.
Prerequisites
You should be running in a Broadleaf instance that already has the ScheduledJobsAndEvents module configured.
Step 1 - Create the Event Consumer
The event consumer represents the code you want to run when the event is received. This tutorial uses the same event consumer as the Scheduled Job Tutorial. Make sure to complete step 1 from that tutorial before continuing.
Step 2 - Create a Controller to Call the Event
In this step, we'll create a controller that we can use to create and send the event.
package com.mycompany.controller.tutorial.event;
@Controller("blEventTestController")
@RequestMapping("/events")
public class EventTestController {
@Resource(name = "blSystemEventSender")
protected SystemEventSender systemEventSender;
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String testEvent(HttpServletRequest request) {
SystemEvent event = createSystemEvent(request);
systemEventSender.sendEvents(Arrays.asList(event));
// Just go back to the index view.
return "/";
}
protected SystemEvent createSystemEvent(HttpServletRequest request) {
SystemEvent event = new SystemEventImpl();
// TODO: Set event properties
event.setType("MY_EVENT_TYPE");
event.setScopeType(EventScopeType.VM);
event.setWorkerType(EventWorkerType.SITE);
event.setEnabled(true);
addRequestParametersAsEventDetails(event, request);
return event;
}
protected void addRequestParametersAsEventDetails(SystemEvent event, HttpServletRequest request) {
// TODO: Loop through request params and add as event details
}
}
Step 3 - Run the Test
- Build and Start Admin
- Build and Start Site
- Hit the site URL /events/test?demo=success
Within 30 seconds, you should see the echo message in your Admin console.