Java 消息服务 (JMS) 有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3015178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:35:22  来源:igfitidea点击:

What is Java Message Service (JMS) for?

javajms

提问by Daniel

I am currently evaluating JMS and I don't get what I could use it for.

我目前正在评估 JMS,但我不知道可以用它做什么。

Currently, I believe this would be a Usecase: I want to create a SalesInvoice PDF and print it when an SalesOrder leaves the Warehouse, so during the Delivery transaction I could send a transactional print request which just begins when the SalesOrder transaction completes successfully.

目前,我相信这将是一个用例:我想创建一个 SalesInvoice PDF 并在 SalesOrder 离开仓库时打印它,因此在交付事务期间,我可以发送一个事务性打印请求,该请求在 SalesOrder 事务成功完成时才开始。

Now I found out most JMS products are standalone server.

现在我发现大多数 JMS 产品都是独立服务器。

  • Why would a need a Standalone Server for Message Processing, vs. e.g. some simple inproc processing with Quartz scheduler?
  • How does it interact with my application?
  • Isn't it much too slow?
  • What are Usecases you already implemented successfully?
  • 为什么需要一个独立的消息处理服务器,而不是使用 Quartz 调度程序进行一些简单的 inproc 处理?
  • 它如何与我的应用程序交互?
  • 是不是太慢了?
  • 您已经成功实施了哪些用例?

采纳答案by Uri

JMS is an amazingly useful system, but not for every purpose.

JMS 是一个非常有用的系统,但并非适用于所有目的。

It's essentially a high-level framework for sending messages between nodes, with options for discovery, robustness, etc.

它本质上是一个用于在节点之间发送消息的高级框架,具有发现、健壮性等选项。

One useful use case is when you want a client and a server to talk to one another, but without the client actually having the server's address (E.g., you may have more than one server). The client only needs to know the broker and the queue/topic name, and the server can connect as well.

一个有用的用例是当您希望客户端和服务器相互通信,但客户端实际上没有服务器的地址时(例如,您可能有多个服务器)。客户端只需要知道代理和队列/主题名称,服务器也可以连接。

JMS also adds robustness. For instance, you can configure it so that if the server dies while the client sends messages or the other way around, you can still send messages from the client or poll messages from the server. If you ever tried implementing this directly with sockets - it's a nightmare.

JMS 还增加了健壮性。例如,您可以对其进行配置,以便如果在客户端发送消息时服务器死机或其他方式,您仍然可以从客户端发送消息或从服务器轮询消息。如果您曾经尝试过直接使用套接字来实现它 - 这是一场噩梦。

The scenario you describe sounds like a classic J2EE problem, why are you not using a J2EE framework? JMS is often used inside J2EE for communications, but you got all the other benefits.

您描述的场景听起来像是一个经典的 J2EE 问题,您为什么不使用 J2EE 框架?JMS 通常在 J2EE 内部用于通信,但您可以获得所有其他好处。

回答by Don Branson

I've used it on a number of projects. It can help with scalability, decoupling of services, high availability. Here's a description of how I used it on a project several years ago:

我已经在许多项目中使用过它。它可以帮助实现可扩展性、服务解耦、高可用性。这是我几年前如何在一个项目中使用它的描述:

http://coders-log.blogspot.com/2008/12/favorite-projects-series-installment-2.html

http://coders-log.blogspot.com/2008/12/favorite-projects-series-installment-2.html

The description explains what JMS brought to the table for this particular project, but other projects will use messaging systems for a variety of reasons.

该描述解释了 JMS 为这个特定项目带来了什么,但其他项目将出于各种原因使用消息传递系统。

回答by LB40

JMS is a message-oriented middleware.

JMS 是一个面向消息的中间件

  • Why would a need a Standalone Server for Message Processing, vs. e.g. some simple inproc processing with Quartz scheduler?
  • 为什么需要一个独立的消息处理服务器,而不是使用 Quartz 调度程序进行一些简单的 inproc 处理?

It depends on what other components you may have. I guess. But I don't know anything about Quartz

这取决于您可能拥有哪些其他组件。我猜。但我对石英一无所知

  • How does it interact with my application?
  • 它如何与我的应用程序交互?

You send messages to the broker.

您向代理发送消息。

  • Isn't it much too slow?
  • 是不是太慢了?

Compare to what ?

比较什么?

  • What are Usecases you already implemented successfully?
  • 您已经成功实施了哪些用例?

I've used JMS to implement a SIP application server, to communicate between the various components.

我已经使用 JMS 实现了一个 SIP 应用服务器,以便在各种组件之间进行通信。

回答by kgiannakakis

Messaging is usually used to interconnect different systems and send requests/commands asynchronously. A common example is a bank client application requesting an approval for a transaction. The server is located in another bank's system. Both systems are connected in an Enterprise Service Bus. The request goes into the messaging bus, which instantly acknowledges the reception of the message. The client can go on with processing. Whenever the server system becomes available, the bus forwards the message to it. Of course there needs to be a second path, for the server to inform the client that the transaction executed successfully or failed. This again can be implemented with JMS.

消息传递通常用于互连不同的系统并异步发送请求/命令。一个常见的例子是银行客户应用程序请求批准交易。服务器位于另一家银行的系统中。两个系统都连接在企业服务总线中。请求进入消息总线,它立即确认消息的接收。客户端可以继续处理。每当服务器系统可用时,总线就会将消息转发给它。当然还需要第二条路径,让服务端通知客户端事务执行成功或失败。这也可以用 JMS 实现。

Please note that the two systems need not to implement JMS. One can use JMS and the other one MSMQ. The bus will take care of the interconnection.

请注意,这两个系统不需要实现 JMS。一种可以使用 JMS,另一种可以使用 MSMQ。总线将负责互连。

回答by ewernli

Why would a need a Standalone Server for Message Processing, vs. e.g. some simple inproc processing with Quartz scheduler?

为什么需要一个独立的消息处理服务器,而不是使用 Quartz 调度程序进行一些简单的 inproc 处理?

The strength of JMS lies in the fact that you can have multiple producers and multiple consumersfor the same queue, and the JMS broker manages the load.

JMS 的优势在于您可以为同一个队列拥有多个生产者和多个消费者,并且 JMS 代理管理负载。

If you have multiple producers but a single consumer, you can use other approaches as well, such as a quartz scheduler and a database table. But as soon as you have multiple consumer, the locking scheme become very hard to design; better go for already approved messaging solution. See these other answers from me for a few more details: Why choosing JMS for asynchronous solution ?and Producer/consumer system using database

如果您有多个生产者但只有一个消费者,您也可以使用其他方法,例如石英调度程序和数据库表。但是一旦你有多个消费者,锁定方案就变得很难设计;最好选择已经批准的消息传递解决方案。有关更多详细信息,请参阅我的这些其他答案: 为什么选择 JMS 作为异步解决方案?使用数据库的生产者/消费者系统

The other points are just too vague to be answered.

其他点太模糊,无法回答。

回答by Pascal Thivent

What ist Java Message Service (JMS) for

Java 消息服务 (JMS) 的用途是什么

JMS is a messaging standardthat allows Java EE applications to create, send, receive, and consume messages in a loosely coupled, reliable, and asynchronous way. I'd suggest to read the Java Message Service API Overviewfor more details.

JMS 是一种消息传递标准,它允许 Java EE 应用程序以松散耦合、可靠和异步的方式创建、发送、接收和使用消息。我建议阅读Java 消息服务 API 概述以获取更多详细信息。

Why would a need a Standalone Server for Message Processing, vs. e.g. some simple inproc processing with Quartz scheduler?

为什么需要一个独立的消息处理服务器,而不是使用 Quartz 调度程序进行一些简单的 inproc 处理?

Sure, in your case, Quartz is an option. But what if the invoice system is a remote system? What if you don't want to wait for the answer? What if the remote system is down when you want to communicate with it? What if the network is not always available? This is where JMS comes in. JMS allows to send a message guaranteed to be delivered and to consume it in a transactional way (sending or consuming a message can be part of a global transaction).

当然,就您而言,Quartz 是一种选择。但是如果发票系统是远程系统呢?如果您不想等待答案怎么办?如果您想与远程系统通信时远程系统关闭了怎么办?如果网络不总是可用怎么办?这就是 JMS 的用武之地。JMS 允许发送保证交付的消息并以事务方式使用它(发送或使用消息可以是全局事务的一部分)。

How does it interact with my application?

它如何与我的应用程序交互?

JMS supports two communication modes: point-to-point and publish/subscribe (if this answers the question).

JMS 支持两种通信模式:点对点和发布/订阅(如果这回答了问题)。

Isn't it much too slow?

是不是太慢了?

The MOMs I've been working with were blazing fast.

与我一起工作的妈妈们的速度非常快。

What are Usecases you already implemented successfully?

您已经成功实施了哪些用例?

Used in system such as a reservation application, a banking back-office (processing market data), or more simply to send emails.

用于预订应用程序、银行后台(处理市场数据)等系统,或者更简单地用于发送电子邮件。

See also

也可以看看

回答by Jatinder Pal

I have found a very good explanation of JMS with an example.

我用一个例子找到了对 JMS 的很好的解释。

That is a simple chat application with JMS queues are used to communicate messages between users and messages stay in the queue if the receiver is offline.

这是一个带有 JMS 队列的简单聊天应用程序,用于在用户之间传递消息,如果接收方离线,消息将保留在队列中。

In this example implementation they have used

在这个示例实现中,他们使用了

  • XSD to generate domain classes.
  • Eclipse EE as IDE.
  • JBoss as web/application server.
  • HTML/JavaScript/JQuery for UI.
  • Servlet as controller.
  • MySQL as DB.
  • XSD 生成域类。
  • Eclipse EE 作为 IDE。
  • JBoss 作为 Web/应用程序服务器。
  • 用于 UI 的 HTML/JavaScript/JQuery。
  • Servlet 作为控制器。
  • MySQL 作为数据库。

The JBoss configuration step for queue is explained nicely Its available at http://coder2design.com/messaging-service/

队列的 JBoss 配置步骤很好地解释了它可以在http://coder2design.com/messaging-service/ 上找到

Even the downloadable code is also available there.

即使是可下载的代码也可以在那里获得。

回答by user207421

From the Javadoc:

Javadoc

The Java Message Service (JMS) API provides a common way for Java programs to create, send, receive and read an enterprise messaging system's messages.

Java 消息服务 (JMS) API 为 Java 程序提供了一种创建、发送、接收和读取企业消息传递系统消息的通用方法。

In other words, and contrary to every other answer here, JMS is nothing more than an API,which wrapsaccess to third-party Message Brokers, via 'JMS Providers' implemented by the vendor. Those Message Brokers, such as IBM MQ and dozens of others, have the features of reliability, asynchronicity, etc. that have been mentioned in other answers. JMS itself provides exactly none of them. It is to Message Brokers what JDBC is to SQL databases, or JNDI is to LDAP servers (among other things).

换句话说,与这里的所有其他答案相反,JMS 只不过是一个API,它通过供应商实现的“JMS 提供者”封装了对第三方消息代理的访问。那些Message Brokers,例如 IBM MQ 和其他几十个,具有其他答案中提到的可靠性、异步性等特性。JMS 本身完全没有提供它们。JDBC 之于 SQL 数据库,或者 JNDI 之于 LDAP 服务器(除其他外)对于 Message Brokers 而言。