Database Reference
In-Depth Information
Finally, the
chooseTasks
method is implemented to rotate between
the various tasks:
public
List<Integer> chooseTasks(
int
taskId,
List<Object> values) {
LinkedList<Integer> out =
new
LinkedList<Integer>();
out.add(targetTasks.get(nextTask));
nextTask = (nextTask + 1) % numTasks;
return
out;
}
Implementing Bolts
Now that data can be passed to bolts appropriately, it is time to understand
how bolts themselves work. As shown in the previous section, bolts receive
data from other bolts or spouts within the topology. They are event driven
so they cannot be used to retrieve data; that is the role of the spout.
Although it is possible to implement bolts by implementing either the
IBasicBolt
or
IRichBolt
interfaces, the recommended method is to
extend the
BaseBasicBolt
or
BaseRichBolt
classes. These base classes
provide simple implementations for most of Storm's boilerplate methods.
Rich Bolts
Toimplement a
RichBolt
,startbyextending this
RichBaseBolt
abstract
class and implementing the three required methods. Like the custom
grouping class from the previous section, the first is a prepare method that
passes in information about the topology. For bolts, an extra parameter is
added: the
collector
object. This object manages the interaction between
the bolt and the topology, especially transmitting and acknowledging tuples.
In a rich bolt, the
collector
is usually placed into an instance variable
for use in the execute method. The
prepare
method is also a good place
to create an object that cannot be serialized because they do not implement
the
Serialization
interface. It is a good habit to declare all variables that
are initialized by the prepare method as transient. While not all variables