Storm provides hooks with which you can insert custom code to run on any number of events within Storm. You create a hook by extending the BaseTaskHook class and overriding the appropriate method for the event you want to catch. There are two ways to register your hook:
Storm also provides worker-level hooks that are called during worker startup, before any bolts or spouts are prepared/opened. You can create such a hook by extending BaseWorkerHook (an implementation of IWorkerHook) and overriding the methods you want to implement. You can register your hook via TopologyBuilder.addWorkerHook
.
The IWorkerHook#start(Map, WorkerUserContext)
lifecycle method exposes WorkerUserContext which provides a way to set application-level common resources via setResource(String, Object)
method. This resource can then be retrieved by tasks, both spouts (via open(Map, TopologyContext, SpoutOutputCollector
) and bolts (via prepare(Map, TopologyContext, OutputCollector
), by calling TopologyContext#getResource(String)
.
Storm provides ways to share resources across different components via the following ways:
open
for spout and prepare
for bolt and task hook).
TopologyContext#setTaskData(String, Object)
TopologyContext#getTask(String)
open
and to bolts and task hooks via prepare
lifecycle method.
TopologyContext#setExecutorData
TopologyContext#getExecutorData(String)
WorkerUserContext#setResource(String, Object)
WorkerTopologyContext#getResouce(String)
or TopologyContext#getResource(String)