我是一名有用的助手,可以为您翻译文本。
问题是:如何从验证器处理程序深度返回处理结果到起始点(请求处理程序),以便用户可以接收HTTP响应?
我考虑了两个选项:
我有一个网络服务,可以接收来自另一个系统的XML事件,并使用特定的工作流程进行处理,然后将潜在错误列表作为HTTP响应发送回去。
事件处理工作流程由几个处理程序(例如:预处理器,持久化器和验证器)组成,使用Guava's EventBus实现。处理程序相互发送事件,类似于以下内容:
public class RequestHandler {
@RequestMapping
public Errors handleRequest(String xmlData) {
eventBus.post(new XmlReceivedEvent(xmlData));
...
return errors; // how to get errors object from the last handler in chain ?
}
}
public class Preprocessor {
@Subscribe
public void onXmlReceived(XmlReceivedEvent event) {
// do some pre-processing
...
eventBus.post(new PreprocessingCompleteEvent(preprocessingResult));
}
}
public class Persister {
@Subscribe
public void onPreprocessingComplete(PreprocessingCompleteEvent event) {
// do some persistence stuff
...
eventBus.post(new PersistenceCompleteEvent(persistenceResult));
}
}
public class Validator {
@Subscribe
public void onPersistenceComplete(PersistenceCompleteEvent event) {
// do validation
...
eventBus.post(new ValidationCompleteEvent(errors)); // errors object created, should be returned back to the RequestHandler
}
}
问题是:如何从验证器处理程序深度返回处理结果到起始点(请求处理程序),以便用户可以接收HTTP响应?
我考虑了两个选项:
Set errors object to the initial XmlReceivedEvent and retrieve it after processing complete:
public class RequestHandler { @RequestMapping public Errors handleRequest(String xmlData) { XmlReceivedEvent event = new XmlReceivedEvent(xmlData); eventBus.post(event); ... return event.getErrors(); } }
Subscribe RequestHandler to ValidationCompleteEvent from Validator with populated errors object inside.
public class RequestHandler { private Errors errors; @RequestMapping public Errors handleRequest(String xmlData) { XmlReceivedEvent event = new XmlReceivedEvent(xmlData); eventBus.post(event); ... return this.errors; // ??? } @Subscribe public void onValidationComplete(ValidationCompleteEvent event) { this.errors = event.getErrors(); } }
但是,不幸的是,RequestHandler是一个Spring无状态服务(单例),所以我想避免在类字段中保存任何数据。
感谢任何想法。