风暴没有躲过最大鲸鱼喷水消费

时间:2015-08-12 13:10:01

标签: java apache-storm

我已经创建了一个示例拓扑来测试设置最大spout支出属性。这是一个简单的toplogy,带有1个喷口和一个螺栓。在睡了一秒钟后,喷口会发出100000个元组和螺栓。我已将max spout spend属性设置为10.我认为这意味着如果该spout的非acked消息计数为10,则spout将不会发出任何元组。但是当我运行拓扑时,我可以看到spout发出2160条消息,然后等待。我的理解是正确的还是我错过了一些东西。我正在使用风暴0.9.5。 以下是代码

public static void main(String[] args) {

    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("spout", new TestSpout(), 1);
    builder.setBolt("bolt", new TestBolt(),1).shuffleGrouping("spout");
    Config conf = new Config();
    conf.setNumWorkers(1);
    conf.setMaxSpoutPending(10);
    try {
        StormSubmitter.submitTopology("test", conf, builder.createTopology());
    } catch (AlreadyAliveException e) {
        e.printStackTrace();
    } catch (InvalidTopologyException e) {
        e.printStackTrace();
    }
}


public class TestSpout extends BaseRichSpout {
private SpoutOutputCollector collector;
private int count = 1;

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

@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    this.collector = collector;
    System.out.println(context.maxTopologyMessageTimeout());
}

@Override
public void nextTuple() {

    if(count <= 100000) {
        System.out.println("Emitting : " + count);
        collector.emit(new Values(count++ + ""));
    }
}

}

public class TestBolt extends BaseRichBolt {
private OutputCollector collector;

@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
    this.collector = collector;
}

@Override
public void execute(Tuple input) {
    try {
        System.out.println(input.getString(0));
        Thread.sleep(1000);
        collector.ack(input);
    } catch (InterruptedException e) {
        e.printStackTrace();
        System.out.println("Exception");
    }
}

@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {

}

}

1 个答案:

答案 0 :(得分:3)

您需要将消息ID分配给public static int[] toint(Integer[] WrapperArray) { int[] newArray = new int[WrapperArray.length]; for (int i = 0; i < WrapperArray.length; i++) { newArray[i] = WrapperArray[i].intValue(); } return newArray; } 方法中发出的元组。否则,将忽略参数Spout.nextTuple()。例如,您可以使用max.spout.pending变量作为ID(基本上,任何东西都可以用作ID。它必须是唯一的。)

count

否则,Storm无法将输出元组链接到您的螺栓中的元组,即,Storm无法计算有多少元组待处理。只有具有ID的元组才能被Storm跟踪。