Extension with Java and Spring - Sample One
This extension is a bit more complex as it contains Java source code. The source that follows is from the event-sample-6.0.0.0-RC12-sources.jar file. You can download the file from the Solution Book.
The XML file event-sample-datahub-extension.xml does not contain any metadata information, because the extension is intended to manage data loading, composition, and publication.
<extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.hybris.com/schema/"
xsi:schemaLocation="http://www.hybris.com/schema/ http://www.hybris.com/schema/datahub-metadata-schema-1.2.0.xsd"
name="event-sample">
</extension>
The following java source code is for listening for the DataHubInitializationCompletedEvent. When the event is fired, the following source looks for a feed named SAMPLE_EVENT_FEED. If the feed named SAMPLE_EVENT_FEED does not exist, it creates it.
package com.hybris.datahub.event;
import com.hybris.datahub.api.event.DataHubEventListener;
import com.hybris.datahub.api.event.DataHubInitializationCompletedEvent;
import com.hybris.datahub.service.DataHubFeedService;
import com.hybris.datahub.validation.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
/**
* Creates a SAMPLE_EVENT_FEED after the initialization of the Data Hub.
*/
public class FeedCreationListener implements DataHubEventListener<DataHubInitializationCompletedEvent>
{
private static final Logger logger = LoggerFactory.getLogger(FeedCreationListener.class);
private static final String EVENT_FEED_NAME = "SAMPLE_EVENT_FEED";
private DataHubFeedService feedService;
@Override
public void handleEvent(final DataHubInitializationCompletedEvent event)
{
logger.info("Checking for existing of sample event driven feed");
if (feedService.findDataFeedByName(EVENT_FEED_NAME) == null)
{
try
{
logger.debug("Attempting to create sample event driven feed");
feedService.createFeed(EVENT_FEED_NAME, SampleEventDrivenPoolConstants.EVENT_POOL_NAME,
"MANUAL", "MANUAL", "NAMED_POOL", "A test feed to demonstrate " +
"event driven composition and publication");
}
catch (final ValidationException e)
{
logger.error("Error creating sample feed", e);
}
}
}
@Override
public Class<DataHubInitializationCompletedEvent> getEventClass()
{
return DataHubInitializationCompletedEvent.class;
}
@Override
public boolean executeInTransaction()
{
return true;
}
@Required
public void setFeedService(final DataHubFeedService feedService)
{
this.feedService = feedService;
}
}
The following Spring XML file, named event-sample-datahub-extension-spring.xml, configures the Spring beans needed for the listeners.
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="compositionCompletedEventListener" class="com.hybris.datahub.event.CompositionCompletedEventListener"
parent="abstractPoolActionInitiationEventListener">
<property name="eventPublicationService" ref="eventPublicationService" />
</bean>
<bean name="dataLoadedEventListener" class="com.hybris.datahub.event.DataLoadedEventListener"
parent="abstractPoolActionInitiationEventListener">
<property name="eventPublicationService" ref="eventPublicationService" />
</bean>
<bean name="feedCreationListener" class="com.hybris.datahub.event.FeedCreationListener">
<property name="feedService" ref="dataHubFeedService" />
</bean>
<bean name="publicationCompletedEventListener" class="com.hybris.datahub.event.PublicationCompletedEventListener">
<property name="eventPublicationService" ref="eventPublicationService" />
<property name="poolActionService" ref="poolActionService" />
</bean>
</beans>