-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yongjingchuan
authored and
yongjingchuan
committed
Aug 20, 2020
1 parent
e9d7a45
commit 74afebf
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
@Scope("prototype") | ||
@Component("baseProcessor") | ||
public class BaseProcessor<T> implements IProcessor, Iterable { | ||
|
||
private final static Logger LOGGER = LoggerFactory.getLogger(BaseProcessor.class); | ||
/** | ||
* 线程变量 | ||
*/ | ||
@Resource | ||
protected ContextHolder contextHolder; | ||
@Resource | ||
protected ApplicationContext springContext; | ||
|
||
private final static String TERMINATE = "terminate"; | ||
|
||
/** | ||
* 当前处理器 | ||
*/ | ||
protected IProcessor processor; | ||
|
||
/** | ||
* 后继处理器 | ||
*/ | ||
protected IProcessor successor; | ||
|
||
/** | ||
* 持有的执行器集合 | ||
*/ | ||
protected LinkedList<T> handlers = Lists.newLinkedList(); | ||
|
||
public IProcessor getProcessor() { | ||
LOGGER.debug("[BaseProcessor] processor is {}.", processor.getClass().getName()); | ||
return processor; | ||
} | ||
|
||
public void setProcessor(IProcessor processor) { | ||
this.processor = processor; | ||
} | ||
|
||
public IProcessor getSuccessor() { | ||
LOGGER.debug("[BaseProcessor] successor is {}.", successor.getClass().getName()); | ||
return successor; | ||
} | ||
|
||
public void setSuccessor(IProcessor successor) { | ||
this.successor = successor; | ||
} | ||
|
||
public boolean hasSuccessor() { | ||
return null != successor; | ||
} | ||
|
||
public void processSuccessor() { | ||
if (isTerminated()) { | ||
LOGGER.debug("[BaseProcessor] terminate is stop status , it will stop all processors."); | ||
return; | ||
} | ||
if (!hasSuccessor()) return; | ||
getSuccessor().process(); | ||
} | ||
|
||
public void terminate() { | ||
LOGGER.debug("[BaseProcessor] terminate works , it will stop all processors."); | ||
contextHolder.bindLocal(TERMINATE, Boolean.TRUE); | ||
} | ||
|
||
public T getHandler() { | ||
Assert.notEmpty(handlers, "handlers cant not be null."); | ||
LOGGER.debug("[BaseProcessor] handler is {}.", handlers.get(0).getClass().getName()); | ||
return handlers.get(0); | ||
} | ||
|
||
public LinkedList<T> getHandlers() { | ||
return handlers; | ||
} | ||
|
||
public void setHandlers(LinkedList<T> handlers) { | ||
this.handlers = handlers; | ||
} | ||
|
||
@Override | ||
public void configurate() { | ||
} | ||
|
||
@Override | ||
public void process() { | ||
|
||
} | ||
|
||
protected void attach(T handler) { | ||
handlers.add(handler); | ||
} | ||
|
||
protected void append(T handler) { | ||
handlers.addLast(handler); | ||
} | ||
|
||
@Override | ||
public Iterator iterator() { | ||
return new BaseProcessorIterator(); | ||
} | ||
|
||
private class BaseProcessorIterator implements Iterator { | ||
|
||
int index; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (index < handlers.size()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (hasNext()) { | ||
LOGGER.debug("[BaseProcessor] handler is {}.", handlers.get(index).getClass().getName()); | ||
return handlers.get(index++); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
private boolean isTerminated() { | ||
if (null == contextHolder.getLocal(TERMINATE)) return false; | ||
return (boolean) contextHolder.getLocal(TERMINATE); | ||
} | ||
} |