一、基于spring监听器,在web.xml配置
二、放入collection,在需要的地方调用
1、interface:
public interface UUMSyncExceptionListener {
void uumEmpSyncExceptionOccur();
void uumOrgSyncExceptionOccur();
void uumRelationExceptionOccur(List<String> relationDatas);
}
2、实现interface类,提供add collection方法,并遍历collection,调用相应方法
@Service(value = "uumSyncExceptionDispatcher")
public class UUMSyncExceptionDispatcher implements UUMSyncExceptionListener {
private static final List<UUMSyncExceptionListener> listeners = new ArrayList<UUMSyncExceptionListener>();
public void addListener(UUMSyncExceptionListener listener) {
listeners.add(listener);
}
@Override
public void uumEmpSyncExceptionOccur() {
for (UUMSyncExceptionListener listener : listeners) {
listener.uumEmpSyncExceptionOccur();
}
}
@Override
public void uumOrgSyncExceptionOccur() {
for (UUMSyncExceptionListener listener : listeners) {
listener.uumOrgSyncExceptionOccur();
}
}
@Override
public void uumRelationExceptionOccur(List<String> relationDatas) {
for (UUMSyncExceptionListener listener : listeners) {
listener.uumRelationExceptionOccur(relationDatas);
}
}
}
3、实现interface,具体实现业务处理逻辑,并加入collection
@Service(value = "uumRelationSyncExceptionListener")
public class UUMRelationSyncExceptionListener implements UUMSyncExceptionListener {
@Autowired
SyncFailedDataWriter writer;
@Autowired
public void initialize(
@Qualifier("uumSyncExceptionDispatcher") UUMSyncExceptionDispatcher dispatcher) {
dispatcher.addListener(this);
}
@Override
public void uumEmpSyncExceptionOccur() {
}
@Override
public void uumOrgSyncExceptionOccur() {
}
@Override
public void uumRelationExceptionOccur(List<String> relationDatas) {
if (relationDatas != null && !relationDatas.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String relationData : relationDatas) {
sb.append(relationData).append("\n");
}
writer.write("relation_", sb.toString());
}
}
}
4、在需要的地方调用遍历collection相关方法,调用相应具体实现
@Autowired
UUMSyncExceptionDispatcher dispatcher;
@Override
protected void doTransToDB(String msgId, String text) throws DocumentException {
Document document = null;
try {
document = DocumentHelper.parseText(text);
List<Element> elements = document.selectNodes("//UCIMRelation");
this.elementCollection(elements);
} catch (Exception e) {
logger.error("接收部门负责人信息,解析XML错误", e);
List<String> failedDatas = new ArrayList<String>();
failedDatas.add(text);
dispatcher.uumRelationExceptionOccur(failedDatas);
throw new BaseException("解析xml错误");
}
}