我在spout类中的失败方法只适用于第一个螺栓,从第二个螺栓开始它不起作用。

时间:2017-07-17 07:35:36

标签: java apache-storm

util.promisify

注意:
Bolt1包含前三个素数(2,3,5)的列表 Bolt2包含第二组三个素数(7,11,13)的列表 在Bolt3中,它只是检查数字是否为素数 从第一个螺栓,我能够从spout类调用Fail(),但是从第二个螺栓开始,我无法从spout类调用Fail()。

拓扑类:

        ......
        TopologyBuilder builder = new TopologyBuilder();
        builder.setSpout("spout", new SpoutClass(), 1);
        builder.setBolt("bolt1", new Bolt1(), 1).shuffleGrouping("spout");
        builder.setBolt("bolt2", new Bolt2(), 1).shuffleGrouping("bolt1");
        builder.setBolt("bolt3", new Bolt3(), 1).shuffleGrouping("bolt2");  

Spout class:

SpoutClass implements IRichSpout{
    private SpoutOutputCollector collector;
    private TopologyContext context;

    public void open(@SuppressWarnings("rawtypes") Map conf, TopologyContext context, SpoutOutputCollector collector) {
        this.context = context;
        this.collector = collector;
        }

    public void nextTuple() {
        try {
            //messageQueue is blocking queue which contains data 
            String msg = messageQueue.take();
            String ackId = msg;
            this.collector.emit(new Values(msg), ackId);

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void ack(Object msgId) {

        System.out.println("Acknowledges that this tuple has been processed ........... " + msgId);

    }

    public void fail(Object msgId) {

        System.out.println("FAILED To Process Message :-" + msgId);

    }
}

Bolt1课程:

public class Bolt1 extends BaseRichBolt {
private OutputCollector collector;
ArrayList<Integer> firstthreePrime = new ArrayList<Integer>();
        firstthreePrime.add(2);
        firstthreePrime.add(3);
        firstthreePrime.add(5);
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
    }
    public void execute(Tuple tuple) {

        String message = (String) tuple.getValueByField("msg");

        System.out.println("Received " + message + " in Bolt1.");
        Integer number = Integer.valueOf(message);
        if (check this number contains bolt1 or not) {
            //if number is contains
            System.out.println(" Number is prime ............." + number + " and Throw from Bolt1");
            this.collector.fail(tuple);
        } else {
            collector.emit(new Values(message));
            collector.ack(tuple);
        }
    }
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("msg"));
    }
}

Bolt2课程:

public class Bolt2 extends BaseRichBolt {
private OutputCollector collector;
ArrayList<Integer> secondthreePrime = new ArrayList<Integer>();
        secondthreePrime.add(7);
        secondthreePrime.add(11);
        secondthreePrime.add(13);
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        this.collector = collector;

    }
    public void execute(Tuple tuple) {

        String message = (String) tuple.getValueByField("msg");

        System.out.println("Received " + message + " in Bolt2.");
        Integer number = Integer.valueOf(message);
        if (check this number contains bolt2 or not) {
            //if number is contains
            System.out.println(" Number is prime ............." + number + " and Throw from Bolt2");
            this.collector.fail(tuple);
        } else {
            collector.emit(new Values(message));
            collector.ack(tuple);
        }
    }
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("msg"));
    }
}

Bolt3课程:

public class Bolt3 extends BaseRichBolt {
private OutputCollector collector;

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

    }
    public void execute(Tuple tuple) {

        String message = (String) tuple.getValueByField("msg");

        System.out.println("Received " + message + " in Bolt3.");
        Integer number = Integer.valueOf(message);
        if (check this number is prime or not) {
            //if number is prime
            System.out.println(" Number is prime ............." + number + " and Throw from Bolt3");
            this.collector.fail(tuple);
        } else {
            collector.emit(new Values(message));
            collector.ack(tuple);
        }
    }
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
    }
}

1 个答案:

答案 0 :(得分:1)

由于您使用的是BaseRichBolt,您不想锚定传出元组吗?

_collector.emit(tuple, new Values(message));

如果你没有锚定它们,那么它们就没有链接到来自喷口的元组。查看文档:{​​{3}}