Storm确保消息被消费

1.spout中nextTuple方法中发送的tuple需要携带msgId

   collector.emit(new Values(line),index);   //index为Integer类型

2.bolt中需要对tuple进行确认(ack() | fail())
public void execute(Tuple tuple) {
String line = tuple.getString(0);
System.out.println(this + " : " + line);
if(new Random().nextBoolean()){

//确认,向上家(也就是Spout)回执成功

collector.ack(tuple);
}
else{

//失败,向上家(也就是Spout)回执失败

collector.fail(tuple);
}
}


3.实现spout的ack()和fail()方法

                //下家(Bolt)接收成功处理

public void ack(Object msgId) {
System.out.println(this + " : ack() : " + msgId);
}
                //下家(Bolt)接收失败处理
public void fail(Object msgId) {
System.out.println(this + " : fail() : " + msgId);

}




猜你喜欢

转载自blog.csdn.net/scgh_fx/article/details/80623223