简体   繁体   English

JMS替代方案

[英]JMS Alternative

I was reading a blog, and one of the points was 'if you're using queues, you messed up', in the context of JMS. 我正在阅读一篇博客,其中一个观点是“如果你正在使用队列,那么你就搞砸了”,在JMS的背景下。

I was thinking, do we even need JMS? 我在想,我们甚至需要JMS吗? A simple alternative would be, if you need to do something asynchronously, why not just put a job request in a table somewhere, and have some process(es) polling the db every X time units looking for new jobs? 一个简单的替代方案是,如果您需要异步执行某些操作,为什么不将某个作业请求放在某个表的某个位置,并让每个X时间单元查找新作业的某些进程轮询数据库?

This approach is simpler than JMS, its easy to understand, and basically removes a dependency from an application. 这种方法比JMS简单,易于理解,并且基本上消除了应用程序的依赖性。

What am I losing if I use the alternative I described? 如果我使用我描述的替代方案,我会失去什么? Perhaps one loses the possibility of using JMX to be able to administer things, but if your job 'queue' is fed off a table, you can write some simple code to 'manage' the processing. 也许人们失去了使用JMX来管理事物的可能性,但是如果你的工作“队列”是从表中提取的,你可以编写一些简单的代码来“管理”处理。

I was reading a blog, and one of the points was 'if you're using queues, you messed up', in the context of JMS. 我正在阅读一篇博客,其中一个观点是“如果你正在使用队列,那么你就搞砸了”,在JMS的背景下。

This is simply wrong. 这是完全错误的。

You might mess up by choosing to use a queue when it's not appropriate, but if it is JMS is a fine choice. 如果不合适,可以选择使用队列,但如果是JMS则是一个不错的选择。

I'd be more discerning about what I read on the Internet if I were you. 如果我是你,我会更加清楚我在互联网上读到的内容。 Sounds to me like the author liked making inflammatory statements to spice up his blog and bump up his Google Analytics statistics. 听起来像是作者喜欢制作煽动性言论来为他的博客增添趣味并提升他的Google Analytics统计数据。 It's one person's opinion, nothing more. 这是一个人的意见,仅此而已。

The polling solution is more complex, wasteful of CPU cycles, and not real time in my opinion. 在我看来,轮询解决方案更复杂,浪费CPU周期,而不是实时。

You can use an Executor or FutureTask if you want asynchronous processing. 如果需要异步处理,可以使用Executor或FutureTask。 Those would be reasonable alternatives to a queue if asynch is all you need. 如果你需要asynch,这些将是队列的合理替代品。

I would really like to know where you read that. 我真的很想知道你在哪里读到的。 JMS is pretty much proven technology, but as with all solutions, you can also misuse it. JMS是经过验证的技术,但与所有解决方案一样,您也可以滥用它。 If you need to schedule tasks, you can use one of the many task scheduling libraries. 如果需要安排任务,可以使用众多任务调度库之一。 Here is an overview: http://java-source.net/open-source/job-schedulers 以下是概述: http//java-source.net/open-source/job-schedulers

For very simple requirements of just asynchronous processing, you can use any suitable class from java.util.concurrent package. 对于只是异步处理的非常简单的要求,您可以使用java.util.concurrent包中的任何合适的类。

If you need transactions or the guarantee that a job must be successfully completed once submitted, even in case of system failures (software or even hardware crashes), or you want to offload job processing to another process you need some other solution. 如果您需要事务或保证在提交后必须成功完成作业,即使系统出现故障(软件甚至硬件崩溃),或者您希望将作业处理卸载到另一个进程,您还需要其他一些解决方案。

JMS approach can provide a very sophisticated solution with relatively less effort. JMS方法可以相对较少的努力提供非常复杂的解决方案。

Messaging (or JMS) is a very standard integration solution which can solve the problem of keeping submission of jobs asynchronous and keeping submission of tasks decoupled from the actual processing. 消息传递(或JMS)是一种非常标准的集成解决方案,它可以解决保持作业异步提交以及将任务提交与实际处理分离的问题。 Messaging based solution can be easily scaled by increasing the number of 'job processor' threads listening on the job queue. 通过增加侦听作业队列的“作业处理器”线程数,可以轻松扩展基于消息传递的解决方案。 The traffic will be automatically load balanced. 流量将自动负载平衡。 Messaging systems can also provide transaction support, ie automatically put the message back on the queue if job processing fails so that it can be retried. 消息传递系统还可以提供事务支持,即如果作业处理失败则自动将消息放回队列,以便可以重试。

Many enterprise integration patterns are based on Messaging systems (Message oriented middlewares). 许多企业集成模式都基于消息系统(面向消息的中间件)。 This book on Enterprise Integration Patterns by Gregor Hohpe has most popular patterns of how to use messaging in your applications. Gregor Hohpe撰写的关于企业集成模式的书籍介绍了如何在应用程序中使用消息传递的最常用模式。

The database approach requires another process to 数据库方法需要另一个过程

1) Poll the table for 'new jobs', update the status of the row when job processing starts by the processing application and eventually either update the row status of the job to 'done' (or delete the job altogether from the table). 1)轮询表中的“新作业”,在处理应用程序启动作业处理时更新行的状态,并最终将作业的行状态更新为“完成”(或从表中完全删除作业)。 2) If something goes wrong during the job processing, the job status should be changed back to 'new' on the table, so that the 'polling' mechanism can pick up the job again. 2)如果在作业处理过程中出现问题,应将作业状态更改回表格中的“新”状态,以便“轮询”机制可以再次获取作业。 Also, need will arise to write some 'recovery thread' on system start-up to find out jobs which might be in an inconsistent state and put them back in 'new' state to start the processing again. 此外,还需要在系统启动时编写一些“恢复线程”,以找出可能处于不一致状态的作业,并将它们置于“新”状态以再次开始处理。

Bottom line is, it takes a lot of effort to build an integration solution which is based on a database. 最重要的是,构建基于数据库的集成解决方案需要花费很多精力。 It also tightly couples both 'job submitter' and 'job processor' applications with the database schema which breaks the encapsulation of your database. 它还将“作业提交者”和“作业处理器”应用程序与数据库模式紧密结合,从而打破了数据库的封装。 If your 'job processor' has multiple threads (which you will probably need if you want to scale) then you need to make sure that only one thread picks up the job and updates 'that' row. 如果你的“作业处理器”有多个线程(如果你想扩展你可能需要它),那么你需要确保只有一个线程获取作业并更新“那个”行。

A JMS solution solves this problem very easily without hand-coding all this logic. JMS解决方案可以非常轻松地解决这个问题,而无需手动编码所有这些逻辑。

Surely, if you are using queues you are NOT messed up. 当然,如果你使用队列,你不会搞砸。 But you should have some valid use-case to introduce a messaging middleware. 但是你应该有一些有效的用例来介绍一个消息传递中间件。

This approach is simpler than JMS, its easy to understand, and basically removes a dependency from an application. 这种方法比JMS简单,易于理解,并且基本上消除了应用程序的依赖性。

Well, that's only true if you come from a Database background. 嗯,如果你来自数据库背景,那是唯一的。 I would think there is nothing easier than sending a message for every state change and 'forget about it'. 我认为没有什么比为每个州的变化发送信息更容易了,“忘掉它”。

One advantage of message oriented middleware (eg JMS) is the higher performance - you can have rates of 1M msgs/sec - but the bigger benefit is usually a cleaner architecture: a message bus where multiple components plug into. 面向消息的中间件(例如JMS)的一个优点是性能更高 - 您可以拥有1M msgs /秒的速率 - 但更大的好处通常是更简洁的架构:多个组件插入的消息总线。 Right now you have your one-to-one communication, but in 2 years you might want to log that traffic, in 3 years you might want to monitor errors and in 4 years you might want to automatically record and replay and test it... 现在您可以进行一对一的通信,但是在2年内您可能想要记录该流量,在3年内您可能希望监控错误,并且在4年内您可能希望自动记录和重放并测试它。 。

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

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