码迷,mamicode.com
首页 > 编程语言 > 详细

spring 自定义监听器

时间:2015-05-04 23:30:07      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:

一、基于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错误");
}
}

 

spring 自定义监听器

标签:

原文地址:http://www.cnblogs.com/lxfeng/p/4272626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!