Storm Drpc Example

Source: Internet
Author: User

1,drpc principle

The client sends the name of the method to be executed to the DRPC server, as well as the parameters of this method. The topology of this function is implemented using Drpcspout to receive the function call stream from the DRPC server. Each function call is marked with a unique ID by the DRPC server. The topology then calculates that the last bolt in topology, called Returnresults, connects to the DRPC server and sends the result of the call to the DRPC server (identified by that unique ID). The DRPC server uses that unique ID to match the waiting client, wakes up the client and sends the result to it.

2, example

Package Storm.starter;import Backtype.storm.config;import Backtype.storm.localcluster;import Backtype.storm.localdrpc;import Backtype.storm.drpc.drpcspout;import Backtype.storm.drpc.returnresults;import Backtype.storm.topology.basicoutputcollector;import Backtype.storm.topology.outputfieldsdeclarer;import Backtype.storm.topology.topologybuilder;import Backtype.storm.topology.base.basebasicbolt;import Backtype.storm.tuple.fields;import Backtype.storm.tuple.tuple;import Backtype.storm.tuple.values;public Class MANUALDRPC {public static class Exclamationbolt extends Basebasicbolt {@Override public void Declareoutputfields (    Outputfieldsdeclarer declarer) {Declarer.declare (New fields ("Result", "return-info"));       } @Override public void execute (tuple tuple, basicoutputcollector collector) {String arg = tuple.getstring (0);      Object retinfo = tuple.getvalue (1);    Collector.emit (New Values (ARG + "!!!", retinfo)); }} public static void Main (string[] args) {    Topologybuilder builder = new Topologybuilder ();    Localdrpc drpc = new Localdrpc ();    drpcspout spout = new Drpcspout ("exclamation", DRPC);    Builder.setspout ("Drpc", spout);    Builder.setbolt ("Exclaim", New Exclamationbolt (), 3). shufflegrouping ("Drpc");    Builder.setbolt ("Return", New Returnresults (), 3). shufflegrouping ("exclaim");    Localcluster cluster = new Localcluster ();    Config conf = new config ();    Cluster.submittopology ("Exclaim", Conf, Builder.createtopology ());    System.out.println (Drpc.execute ("exclamation", "AAA"));  System.out.println (Drpc.execute ("exclamation", "BBB")); }}


Note: Drpcspout's name matches the name specified by the Drpc.execute.

Storm Drpc Example

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.