Storm framework: How to Select different bolts based on business conditions for downstream messaging

Source: Internet
Author: User

The basic concepts of the strom framework will not be mentioned.StreamThe message stream of the custom ID. By default, both spout and bolt require interface methods.declareOutputFieldsThe Code is as follows:

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

In this case, messages are received by all defined bolts. If we need to select different bolts based on the message type, we need stream grouping.

  • FirstOutputFieldsDeclarerTo define the stream for transmitting multiple message streams.

Two Stream message streams are defined as follows: email and SMS

@Overridepublic void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {    outputFieldsDeclarer.declareStream("email", new Fields("body"));    outputFieldsDeclarer.declareStream("sms", new Fields("body"));}
  • Then, the message content is analyzed and determined to transmit the specified stream type.
@ Overridepublic void execute (tuple) {string streamtype; string value = tuple. getstringbyfield ("body"); # logically judge stub code if (value. startswith ("Email:") {streamtype = "email" ;}else {streamtype = "SMS" ;}outputcollector. emit (streamtype, new values (value ));}
  • When setting the bolt message source in topology, localorshufflegrouping is used to set to only receive messages of the specified stream.

Filterbolt processes messages and specifies different streams when delivered to bolts. emailpolicybolt only receivesemailType stream message, smsnotifybolt only receivessmsType of stream messages.

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");

Storm framework: How to Select different bolts based on business conditions for downstream messaging

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.