Package org.apache.storm.task
Class TopologyContext
java.lang.Object
org.apache.storm.task.GeneralTopologyContext
org.apache.storm.task.WorkerTopologyContext
org.apache.storm.task.TopologyContext
- All Implemented Interfaces:
- org.apache.storm.shade.net.minidev.json.JSONAware,- IMetricsContext
A `TopologyContext` is given to bolts and spouts in their `prepare()` and `open()` methods, respectively. This object provides
 information about the component's place within the topology, such as task ids, inputs and outputs, etc.
 
The `TopologyContext` is also used to declare `ISubscribedState` objects to synchronize state with StateSpouts this object is subscribed to.
- 
Field SummaryFields inherited from class org.apache.storm.task.WorkerTopologyContextSHARED_EXECUTORFields inherited from class org.apache.storm.task.GeneralTopologyContextdoSanityCheck, topoConf
- 
Constructor SummaryConstructorsConstructorDescriptionTopologyContext(StormTopology topology, Map<String, Object> topoConf, Map<Integer, String> taskToComponent, Map<String, List<Integer>> componentToSortedTasks, Map<String, Map<String, Fields>> componentToStreamToFields, Map<String, Long> blobToLastKnownVersionShared, String stormId, String codeDir, String pidDir, Integer taskId, Integer workerPort, List<Integer> workerTasks, Map<String, Object> defaultResources, Map<String, Object> userResources, Map<String, Object> executorData, Map<Integer, Map<Integer, Map<String, IMetric>>> registeredMetrics, AtomicBoolean openOrPrepareWasCalled, StormMetricRegistry metricRegistry) 
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddTaskHook(ITaskHook hook) getExecutorData(String name) Fetches the executor-level data for the given name.getHooks()Deprecated.getTaskData(String name) Fetches the task-level data for the given name.Get component id.Gets the declared input fields for this component.getThisOutputFields(String streamId) Gets the declared output fields for the specified stream id for the component this task is a part of.Gets the declared output fields for all streams for the component this task is a part of.Gets the declared inputs to this component.Gets the set of streams declared for the component of this task.Gets information about who is consuming the outputs of this component, and how.intGets the task id of this task.intGets the index of this task id in getComponentTasks(getThisComponentId()).com.codahale.metrics.CounterregisterCounter(String name) <T> com.codahale.metrics.Gauge<T>registerGauge(String name, com.codahale.metrics.Gauge<T> gauge) com.codahale.metrics.HistogramregisterHistogram(String name) com.codahale.metrics.MeterregisterMeter(String name) registerMetric(String name, ICombiner combiner, int timeBucketSizeInSecs) Deprecated.registerMetric(String name, IReducer reducer, int timeBucketSizeInSecs) Deprecated.<T extends IMetric>
 TregisterMetric(String name, T metric, int timeBucketSizeInSecs) Deprecated.voidregisterMetricSet(String prefix, com.codahale.metrics.MetricSet set) com.codahale.metrics.TimerregisterTimer(String name) <T extends ISubscribedState>
 TsetAllSubscribedState(T obj) All state from all subscribed state spouts streams will be synced with the provided object.voidsetExecutorData(String name, Object data) Sets the executor-level data for the given name.<T extends ISubscribedState>
 TsetSubscribedState(String componentId, String streamId, T obj) Synchronizes the specified stream from the specified state spout component id with the provided ISubscribedState object.<T extends ISubscribedState>
 TsetSubscribedState(String componentId, T obj) Synchronizes the default stream from the specified state spout component id with the provided ISubscribedState object.voidsetTaskData(String name, Object data) Sets the task-level data for the given name.Methods inherited from class org.apache.storm.task.WorkerTopologyContextgetAssignmentId, getCodeDir, getNodeToHost, getPIDDir, getResource, getSharedExecutor, getTaskToNodePort, getThisWorkerPort, getThisWorkerTasksMethods inherited from class org.apache.storm.task.GeneralTopologyContextdoSanityCheck, getComponentCommon, getComponentId, getComponentIds, getComponentOutputFields, getComponentOutputFields, getComponentStreams, getComponentTasks, getConf, getRawTopology, getSources, getStormId, getTargets, getTaskToComponent, maxTopologyMessageTimeout
- 
Constructor Details- 
TopologyContextpublic TopologyContext(StormTopology topology, Map<String, Object> topoConf, Map<Integer, String> taskToComponent, Map<String, List<Integer>> componentToSortedTasks, Map<String, Map<String, Fields>> componentToStreamToFields, Map<String, Long> blobToLastKnownVersionShared, String stormId, String codeDir, String pidDir, Integer taskId, Integer workerPort, List<Integer> workerTasks, Map<String, Object> defaultResources, Map<String, Object> userResources, Map<String, Object> executorData, Map<Integer, Map<Integer, Map<String, IMetric>>> registeredMetrics, AtomicBoolean openOrPrepareWasCalled, StormMetricRegistry metricRegistry) 
 
- 
- 
Method Details- 
setAllSubscribedStateAll state from all subscribed state spouts streams will be synced with the provided object.It is recommended that your ISubscribedState object is kept as an instance variable of this object. The recommended usage of this method is as follows: ```java _myState = context.setAllSubscribedState(new MyState()); ``` - Parameters:
- obj- Provided ISubscribedState implementation
- Returns:
- Returns the ISubscribedState object provided
 
- 
setSubscribedStateSynchronizes the default stream from the specified state spout component id with the provided ISubscribedState object.The recommended usage of this method is as follows: ```java _myState = context.setSubscribedState(componentId, new MyState()); ``` - Parameters:
- componentId- the id of the StateSpout component to subscribe to
- obj- Provided ISubscribedState implementation
- Returns:
- Returns the ISubscribedState object provided
 
- 
setSubscribedStatepublic <T extends ISubscribedState> T setSubscribedState(String componentId, String streamId, T obj) Synchronizes the specified stream from the specified state spout component id with the provided ISubscribedState object.The recommended usage of this method is as follows: ```java _myState = context.setSubscribedState(componentId, streamId, new MyState()); ``` - Parameters:
- componentId- the id of the StateSpout component to subscribe to
- streamId- the stream to subscribe to
- obj- Provided ISubscribedState implementation
- Returns:
- Returns the ISubscribedState object provided
 
- 
getBlobToLastKnownVersion
- 
getThisTaskIdpublic int getThisTaskId()Gets the task id of this task.- Returns:
- the task id
 
- 
getThisComponentIdGet component id.- Returns:
- the component id for this task. The component id maps to a component id specified for a Spout or Bolt in the topology definition.
 
- 
getThisOutputFieldsGets the declared output fields for the specified stream id for the component this task is a part of.
- 
getThisOutputFieldsForStreamsGets the declared output fields for all streams for the component this task is a part of.
- 
getThisStreamsGets the set of streams declared for the component of this task.
- 
getThisTaskIndexpublic int getThisTaskIndex()Gets the index of this task id in getComponentTasks(getThisComponentId()). An example use case for this method is determining which task accesses which resource in a distributed resource to ensure an even distribution.
- 
getThisInputFieldsGets the declared input fields for this component.- Returns:
- A map from sources to streams to fields.
 
- 
getThisSourcesGets the declared inputs to this component.- Returns:
- A map from subscribed component/stream to the grouping subscribed with.
 
- 
getThisTargetsGets information about who is consuming the outputs of this component, and how.- Returns:
- Map from stream id to component id to the Grouping used.
 
- 
setTaskDataSets the task-level data for the given name. This data is shared amongst the task and its corresponding task hooks.- Parameters:
- name- name of the task-level data to be set
- data- task-level data
 
- 
getTaskDataFetches the task-level data for the given name. This data is shared amongst the task and its corresponding task hooks.- Parameters:
- name- name of the task-level data to be fetched
- Returns:
- Associated task-level data
 
- 
setExecutorDataSets the executor-level data for the given name. This data is shared amongst tasks and corresponding task hooks managed by the given executor.- Parameters:
- name- name of the executor-level data to be set
- data- executor-level data
 
- 
getExecutorDataFetches the executor-level data for the given name. This data is shared across tasks and corresponding task hook managed by the given executor.- Parameters:
- name- name of the executor-level data to be fetched
- Returns:
- Associated executor-level data
 
- 
addTaskHook
- 
getHooks
- 
toJSONString- Specified by:
- toJSONStringin interface- org.apache.storm.shade.net.minidev.json.JSONAware
- Overrides:
- toJSONStringin class- GeneralTopologyContext
 
- 
registerMetric@Deprecated public <T extends IMetric> T registerMetric(String name, T metric, int timeBucketSizeInSecs) Deprecated.Description copied from interface:IMetricsContextRegister metric.- Specified by:
- registerMetricin interface- IMetricsContext
 
- 
registerMetric@Deprecated public ReducedMetric registerMetric(String name, IReducer reducer, int timeBucketSizeInSecs) Deprecated.Description copied from interface:IMetricsContextRegister metric.- Specified by:
- registerMetricin interface- IMetricsContext
 
- 
registerMetric@Deprecated public CombinedMetric registerMetric(String name, ICombiner combiner, int timeBucketSizeInSecs) Deprecated.Description copied from interface:IMetricsContextRegister metric.- Specified by:
- registerMetricin interface- IMetricsContext
 
- 
getRegisteredMetricByNameDeprecated.Get component's metric from registered metrics by name. Notice: Normally, one component can only register one metric name once. But now registerMetric has a bug(https://issues.apache.org/jira/browse/STORM-254) cause the same metric name can register twice. So we just return the first metric we meet.
- 
registerTimer- Specified by:
- registerTimerin interface- IMetricsContext
 
- 
registerHistogram- Specified by:
- registerHistogramin interface- IMetricsContext
 
- 
registerMeter- Specified by:
- registerMeterin interface- IMetricsContext
 
- 
registerCounter- Specified by:
- registerCounterin interface- IMetricsContext
 
- 
registerGaugepublic <T> com.codahale.metrics.Gauge<T> registerGauge(String name, com.codahale.metrics.Gauge<T> gauge) - Specified by:
- registerGaugein interface- IMetricsContext
 
- 
registerMetricSet- Specified by:
- registerMetricSetin interface- IMetricsContext
 
 
-