如何以面向对象的方式编写消息队列处理?

发布于 2024-07-10 01:51:05 字数 104 浏览 9 评论 0原文

如果您必须编写从消息队列中获取消息并更新数据库中的表的代码,您将如何以良好的面向对象方式构建它。 你会如何构建它? 消息是 XML 数据,表中每行一个节点。 表中的行可以更新、删除或插入。

If you had to write code that takes messages from a message queue and updates a table in a database, how would you go about structuring it in a good oo way. How would you structure it? The messages is XML data, one node per row in the table. The rows in the table could be updated, deleted or inserted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红焚 2024-07-17 01:51:07

通常,通过消息队列的面向对象实现,您可以自己创建表示各个消息类型的类。 如果您期望获得的不同消息类型是彼此的派生类,那么这将为您提供消息的类层次结构。

使用基于配置的持久性框架,您可以直接为这些类设置持久性。

然后有一个或多个类监听消息队列并仅保留消息,可能只有一个。 它不必比这更复杂。

Normally with OO implementations of message queues you make the classes that represent the individual types of messages yourself. To the extent that the different message types that you expect to get are derivates of each other, this provides your class hierarchy for the messages.

With configuration based persistence frameworks you can just set up presistence for these classes directly.

Then there's one or more classes that listen to the message queue and just persist the messages, probably just one. It doesn't have to be more elaborate than that.

来世叙缘 2024-07-17 01:51:07

在进行消息传递或处理任何类型的中间件时,构建 OO 代码的最佳方法是在代码中隐藏中间件 API,而只处理业务逻辑。

例如,请参阅这些示例

然后您只需要定义您的数据传输对象的样子; 您希望如何以 XML/JSON/其他方式对线路上的内容进行编码。

这种方法的好处是您的代码现在完全与中间件无关 - 您可以交换消息队列并使用 数据库或 JavaSpace 或内存中 SEDA 或文件或任何其他通信协议或中间件 API

The best way of building OO code when doing messaging or dealing with any kind of middleware is to hide the middleware APIs from your code and just deal with business logic.

e.g. see these examples

Then you just need to define what your Data Transfer Objects look like; how you want to encode things on the wire in XML / JSON / whatever.

The great thing about this approach is your code is now totally middleware agnostic - you could swap out your message queue and use a database or JavaSpace or in-memory SEDA or files or any other communication protocol or middleware API.

尸血腥色 2024-07-17 01:51:06

我认为您没有提供足够的信息来获得良好的答案。 这些消息是什么样的? 它们的内容/类型是否有所不同,或者它们都只是“消息”? 它们是否相互交互,或者这只是数据格式转换? 面向对象开发的关键之一是认识到“查找名词与动词”游戏(正如您所描述的那样)很少会带来最佳解决方案。 这当然不会是最糟糕的,但您最终会得到数据聚合和一堆程序代码。

不过,程序代码还不错。 为什么需要面向对象? 问题本身是否需要多态性和数据隐藏? 您是否尝试建模任何复杂的行为? 当问题很简单时,使用非面向对象的解决方案并没有什么可耻的。

I don't believe you've provided enough information for a good answer. What do the messages look like? Do they vary in contents/type, or are they all just "messages"? Do they interact with each other, or is this just a data format conversion? One of the keys to OO development is to realize that the "find the nouns-n-verbs" game (which is as much as you've described) rarely leads to the best solution. It certainly won't be the worst, but you'll end up with data aggregation and a bunch of procedural code.

Procedural code isn't bad, though. Why does it need to be OO? Does the problem itself require polymorphism and data hiding? Is there any complex behavior that you are trying to model? There's no shame in using a non-OO solution, when the problem is simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文