我创建了一个带有 Spout 的 Storm 拓扑,它发出许多用于基准测试的元组。一旦从 spout 发出所有元组或拓扑中不再有任何元组流动,我想停止/终止我的拓扑。
这是我的拓扑结构。
LocalCluster cluster = new LocalCluster();
TopologyBuilder builder = new TopologyBuilder();
Config conf = new Config();
//Disabled ACK'ing for higher throughput
conf.put(Config.TOPOLOGY_ACKER_EXECUTORS, 0);
LoadGeneratorSource loadGenerator = new LoadGeneratorSource(runtime,numberOfTuplesToBeEmitted);
builder.setSpout("loadGenerator", loadGenerator);
//Some Bolts Here
while (loadGenerator.isRunning()){
//Active Waiting
}
//DO SOME STUFF WITH JAVA
cluster.killTopology("StormBenchmarkTopology");
问题是我在这个范围内引用的 loadGenerator 实例与在 spout 线程中运行的实例不同。因此,isRuning() 总是返回 true,即使在 spout 线程中,当没有更多的元组要发出时,它的值为 false。
这是 LoadGeneratorSource 类的一部分。
public class LoadGeneratorSource extends BaseRichSpout {
private final int throughput;
private boolean running;
private final long runtime;
public LoadGeneratorSource(long runtime,int throughput) {
this.throughput = throughput;
this.runtime = runtime;
}
@Override
public void nextTuple() {
ThroughputStatistics.getInstance().pause(false);
long endTime = System.currentTimeMillis() + runtime;
while (running) {
long startTs = System.currentTimeMillis();
for (int i = 0; i < throughput; i++) {
try {
emitValue(readNextTuple());
} catch (Exception e) {
e.printStackTrace();
}
}
while (System.currentTimeMillis() < startTs + 1000) {
// active waiting
}
if (endTime <= System.currentTimeMillis())
setRunning(false);
}
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
//MORE STUFF
}
一旦不再有从 spout 发出或在拓扑中流动的元组,有人能告诉我一种停止我的拓扑的方法吗?提前感谢您的帮助。