我有一个由 3 个阶段组成的责任链。
我的阶段实现如下所示。
public class InitialStage implements RecordHandler {
private RecordHandler next;
@Override
public void setNext(RecordHandler handler) {
if (this.next == null) {
this.next = handler;
} else {
this.next.setNext(handler);
}
}
@Override
public void handleRequest(String record) {
System.out.println("Processing @ Initial Stage.");
if (next != null) {
next.handleRequest(record);
}
}
}
流程管理器
public class ProcessManager {
private RecordProcessor processor = new RecordProcessor();
public ProcessManager()
{
createPipeLine();
}
private void createPipeLine() {
processor = new RecordProcessor();
processor.addHandler(new InitialStage());
processor.addHandler(new MiddleStage());
processor.addHandler(new FinalStage());
}
public void processRecord(String record){
processor.handleRequest(record);
}
}
界面
public interface RecordHandler {
public void setNext(RecordHandler handler);
public void handleRequest(String record);
}
最后是 RecordProcessor,
public class RecordProcessor {
private RecordHandler successor;
private RecordHandler first;
public RecordProcessor(){
}
public void addHandler(RecordHandler recordHandler){
if(this.first == null){
this.first = recordHandler;
}
else{
this.successor.setNext(recordHandler);
}
this.successor = recordHandler;
}
public void handleRequest(String record){
first.handleRequest(record);
}
}
现在我观察到我的中间阶段需要一些时间才能完成。所以现在我对使用线程池很感兴趣,所以记录的处理方式如下所示。
在这里,我假设了 3 个工作线程。
问题
如何正确修改现有的 CoR 以满足新要求?