需求是对于@JmsListener的消息的处理,监听到的记录处理如下
1.更新记录状态
2.发送通知给客户
3.其他处理
刚好可以使用链式的处理
首先定义一个Response处理的接口
public interface ResponseHandler {
void process(JSONObject jsonObj) throws Exception;
//使用java8的default默认实现
default void process(JSONObject jsonObj,
ListenerHandlerChainExecutor chain) throws Exception{
this.process(jsonObj);
if(chain!=null)
chain.process(jsonObj);//链式调用
}
}
定义chain来一个个的处理ResponseHandler的实例,这里借鉴了RestTemplate的interceptor的处理,
用了Iterator来处理
public class ListenerHandlerChainExecutor implements ResponseHandler{
private Iterator<ResponseHandler> it= null ;
private List<ResponseHandler> handlers = null;
public ListenerHandlerChainExecutor(List<ResponseHandler> handlers){
this.handlers = handlers;
it = this.handlers.iterator();
}
@Override
public void process(JSONObject jsonObj) throws Exception {
if(it.hasNext()){
ResponseHandler nextHandler = it.next();
//ResponseHandler 接口里的默认实现,java8的default
//!!!!关键的调用,实现了链式处理
nextHandler.process(jsonObj,this);
}
}
}
更新记录的handler
@Component
@Order(1) //定义顺序
public class UpdateStatusHandler implements ResponseHandler {
@Autowired
RequestMapper mapper;
@Override
public void process(JSONObject jsonObj) throws Exception {
RequestValue request = new RequestValue();
int requestky =jsonObj.getInt("requestKy");
request.setRequestKy(requestky);
request.setRqstStatus(jsonObj.getInt("code"));
mapper.processRequest(request);
}
}
发送通知给客户
@Component
@Order(2)//定义顺序
public class NotifyCustomerHandler implements ResponseHandler {
public void process(JSONObject jsonObj) throws Exception {
....
}
}
@Component
public class xxxMQListener {
//顺序的维护使用@Order接口
//Autowired也可以注入一种类型的集合
//也可以使用@Qualify自定义一个annotation,
//类似springcloud 里的 @loadbalance
@Autowired(required=false)
List<ResponseHandler> list = Collections.emptyList();
@JmsListener(destination="a.b.c.QUEUE")
public void onMessage(Message message) {
if(message instanceof JMSTextMessage){
JMSTextMessage objMsg = (JMSTextMessage)message;
try {
String json = objMsg.getText();
JSONObject jsonObj = new JSONObject(json);
//构造出一个chain
new ListenerHandlerChainExecutor(list).process(jsonObj);
} catch (Exception e) {
logger.error(....);
}
}