Pausing or Suspending Currently Running Processes
There are two features you can use to control Data Hub processing, @BlockWhileProcessing and @BlockOnShutdown.
@BlockWhileProcessing Functionality
There are instances where you may not want an extension running if another process is active. To make it simple to implement, use the @BlockWhileProcessing feature. This feature monitors com.hybris.datahub.api.event.ProcessStartedEvent for active processes and listens to the com.hybris.datahub.api.event.ProcessCompletedEvent for when processes complete. If processes are active and an extension covered by @BlockWhileProcessing tries to start, the @BlockWhileProcessing feature throws a com.hybris.datahub.process.DataHubProcessingException. Otherwise, the @BlockWhileProcessing is not activated, and the process goes ahead.
Here is an example of how the @BlockWhileProcessing feature might be used. Let's assume there is a MyService in Data Hub extension.
public class DefaultMyExtensionService implements MyExtensionService
{
@BlockWhileProcessing
public void refreshConfiguration(final MyExtensionServiceConfig cfg)
{
…
}
}
In this example the refreshConfiguration() method can be called only if no data loading, compositions, or publications are running. Additionally, you could use the @BlockWhileProcessing feature to prevent processing while another extension was active.
Blocking Feature
Extension developers may want to prevent access to their components (REST resources, facades, services, and so on) once a DataHubShutdownEvent is issued. To prevent access, they have to place the @BlockOnShutdown annotation to the method they want to protect:
Sample service
import com.hybris.datahub.shutdown.BlockOnShutdown;
...
public class MyService
{
@BlockOnShutdown
public void startLongAndImportantProcess()
{
...
}
}
The component also has to be registered as a Spring bean for the annotation to work.
If your service must know when shut down happens and has to react to it, listen for the Data Hub shutdown event. This event fires upon receiving a shutdown signal:
Sample of handling shutdown event
import com.hybris.datahub.api.event.DataHubEventListener;
import com.hybris.datahub.api.event.DataHubShutdownEvent;
...
public class MyService implements DataHubEventListenter
{
@Override
public void handleEvent(final DataHubEvent event)
{
releaseResources();
}
@Override
public Class<DataHubShutdownEvent> getEventClass()
{
return DataHubShutdownEvent.class;
}
@Override
public boolean executeInTransaction()
{
return false;
}
private void releaseResources()
{
...
}
}
Return true or false from the executeInTransaction() method. The value depends on whether you are changing data in the database and the releaseResources() method has to be transactional or not.