Storm framework: how to select different bolt s to send messages according to business conditions

Keywords: Java

The basic concept of the Strom framework will not be mentioned. The main topic here is the message flow of Stream custom ID. The interface method declareOutputFields needs to be implemented by default for both spout and bolt. The code is as follows:

@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
    outputFieldsDeclarer.declare(new Fields("body"));
}

In this case, messages sent will be received by all defined bolts. If we need to select different bolts according to the message types we get, we need to use Stream Grouping.

  • First of all, define and launch multiple message flow stream s through the outputfields declarer of the message source

Two kinds of stream message flows are defined as follows: email, sms

@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
    outputFieldsDeclarer.declareStream("email", new Fields("body"));
    outputFieldsDeclarer.declareStream("sms", new Fields("body"));
}
  • Then we decide to launch the specified stream type by analyzing and judging the message content
@Override
public void execute(Tuple tuple) {
    String streamType;
    String value = tuple.getStringByField("body");
    # Logical judgment stub code
    if (value.startsWith("email:")) {
        streamType = "email";
    } else {
        streamType = "sms";
    }
    
    outputCollector.emit(streamType, new Values(value));
}
  • When topology sets the message source of the bolt, it can only receive messages of the specified stream through localorshafflegrouping

FilterBolt processes the messages. When it is distributed to bolts, it will specify different streams. EmailNotifyBolt only receives stream messages of email type, and SmsNotifyBolt only receives stream messages of sms type.

TopologyBuilder topologyBuilder = new TopologyBuilder();
topologyBuilder.setSpout("RabbitmqSpout", new RabbitmqSpout());
topologyBuilder.setBolt("FilterBolt", new FilterBolt()).shuffleGrouping("RabbitmqSpout");

topologyBuilder.setBolt("EmailNotifyBolt", new EmailNotifyBolt()).localOrShuffleGrouping("FilterBolt", "email");

topologyBuilder.setBolt("SmsNotifyBolt", new SmsNotifyBolt()).localOrShuffleGrouping("FilterBolt", "sms");

Posted by philgh on Wed, 11 Dec 2019 08:19:58 -0800