简体   繁体   English

将Spring MVC的Controller与HTTPServlet分离

[英]Decoupling Spring MVC's Controller from the HTTPServlet

I have been working with Spring for a while now to realize that not all of the incoming requests I receive in my app are HTTP-based. 我一直在使用Spring一段时间才意识到我在应用程序中收到的所有传入请求都不是基于HTTP的。 Some requests are email-based, and need email-based responses, others are socket-based (receiving notifications when a value changes in my NOSQL store). 有些请求是基于电子邮件的,需要基于电子邮件的响应,其他请求是基于套接字的(在我的NOSQL存储中值发生变化时接收通知)。 All of them, though use more or less the same MVC infrastructure. 所有这些,尽管使用或多或少相同的MVC基础设施。

Therefore, I thought that maybe rearchitecting the application, in order to remove the coupling between controllers and the HTTP infrastructure will help. 因此,我认为可能重新构建应用程序,以消除控制器和HTTP基础结构之间的耦合将有所帮助。

The dispatcher should no longer call controller methods directly, but rather extract the request parameters, and use them to create an abstract message (or event), which it then puts on a message bus. 调度程序不应再直接调用控制器方法,而是提取请求参数,并使用它们创建抽象消息(或事件),然后将其放在消息总线上。 On the other hand, every controller will subscribe its actions (instances of the Action class - an implementation of the Command pattern) for different events. 另一方面,每个控制器将为不同的事件订阅其动作(Action类的实例 - Command模式的实现)。

Since I am quite new to Spring Integration, JMS, and other things like that, I have no idea, which messaging technology to choose. 由于我对Spring Integration,JMS和其他类似的东西都很陌生,所以我不知道选择哪种消息传递技术。 Also, I am pretty sure that an architecture like this has been developed already. 此外,我很确定已经开发出这样的架构。 Perhaps, I may not even be on the right track. 也许,我甚至可能没有走上正轨。

I accept all kinds of suggestions on how to proceed. 我接受有关如何进行的各种建议。

You are right that messaging solution with a little help of some integration patterns is the "right" way to go. 你是正确的,在一些集成模式的帮助下,消息传递解决方案是“正确”的方式。

You are saying that e-mails and some NoSQL database are already hitting your controller? 你是说电子邮件和一些NoSQL数据库已经击中了你的控制器? This means there is some translation layer between these systems and your controllers. 这意味着这些系统和控制器之间存在一些转换层。 With Spring integration you would use Mail-Receiving Channel Adapter to process incoming e-mails and TCP and UDP Support for NoSQL notifications. 使用Spring集成,您将使用邮件接收通道适配器来处理传入的电子邮件以及对NoSQL通知的TCP和UDP支持 Spring MVC controllers can still be used for "true" web requests, accessing message channel underneath. Spring MVC控制器仍可用于“真正的”Web请求,访问下面的消息通道。

Each channel adapter would have a unique set of transformers to translate adapter-specific message into canonic format. 每个通道适配器都有一组唯一的变换器,用于将特定于适配器的消息转换为canonic格式。 At the end messages from each endpoints would be routed to the same message channel . 最后,来自每个端点的消息将被路由到同一个消息通道

So indeed, your example is a perfect fit for ESB-like solution. 确实,您的示例非常适合类似ESB的解决方案。 Also check out Mule ESB , which is even more mature and powerful. 还可以看看Mule ESB ,它更加成熟和强大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM