如何理解“同步”和“asynchronouns” JMS中的消息传递? [英] How to understand the "synchronous" and "asynchronouns" messaging in JMS?

查看:239
本文介绍了如何理解“同步”和“asynchronouns” JMS中的消息传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了JMS的一些文档后,我对同步 asynchronouns 这句话感到很困惑。

After reading some document of JMS, I totally puzzled by the phrase synchronous and asynchronouns.

参见本页: http://docs.oracle.com/cd/E19798-01/821-1841/bncdq/index.html

同步


您可以使用receive方法同步使用消息。
您可以在调用start方法后随时使用此方法:

You use the receive method to consume a message synchronously. You can use this method at any time after you call the start method:

connection.start();
Message m = consumer.receive();
connection.start();
Message m = consumer.receive(1000); // time out after a second

要异步使用消息,请使用消息监听器,如下所述下一部分。

To consume a message asynchronously, you use a message listener, described in the next section.

异步


JMS消息监听器
消息监听器是一个对象,充当消息的异步事件处理程序。此对象实现MessageListener接口,该接口包含一个onMessage方法。在onMessage方法中,定义消息到达时要采取的操作。

JMS Message Listeners A message listener is an object that acts as an asynchronous event handler for messages. This object implements the MessageListener interface, which contains one method, onMessage. In the onMessage method, you define the actions to be taken when a message arrives.

使用setMessageListener方法向特定的MessageConsumer注册消息侦听器。例如,如果定义一个名为Listener的类来实现MessageListener接口,则可以按如下方式注册消息监听器:

You register the message listener with a specific MessageConsumer by using the setMessageListener method. For example, if you define a class named Listener that implements the MessageListener interface, you can register the message listener as follows:

Listener myListener = new Listener();
consumer.setMessageListener(myListener);


我有两个问题:


  1. 据我所知,JMS的本质是异步的。生产者将消息发布到队列/主题,它不需要等待消费者。这是异步行为。它是如何同步的?

  1. As what I understood, the nature of JMS is asynchronous. Producer publishes messages to the queue/topic, it doesn't need to wait consumer. This is asynchronous behaviour. How can it be "synchronous"?

如果mesageListener是异步的,但在我使用spring-jms的测试中,我发现它总是运行在线。这意味着,如果我在 onMessage 中写 Thread.sleep(2000),则必须等待2秒才能处理下一条消息。它是异步吗?

If the "mesageListener" is asynchronous, but in my test with spring-jms, I found it always running in a thread. That means, if I write Thread.sleep(2000) in onMessage, it have to be wait 2 seconds before processing next message. Is it "asynchronous"?


推荐答案

如果你更了解它,就像这样, consumer.receive()使用 pull 模型:您从队列中读取并被阻止等待此消息,直到它出现,或者某些超时已经过去了。

If you understand it better like this, consumer.receive() uses a pull model: you read from a queue and are blocked waiting for this message until it comes, or some timeout has elapsed.

使用侦听器使用 push 模型:注册一个侦听器,当一条消息进来时,监听器被单独调用线程。

Using a listener uses a push model: you register a listener and, when a message comes in, the listener is called, in a separate thread.

一切都在Java中的线程中完成,并且侦听器调用也不例外。侦听器消息处理是否阻止处理队列中的其他消息取决于专用于消息处理的线程数。如果将Spring配置为使用5个线程池来异步处理消息,那么5个侦听器将能够并行处理消息。

Everything is done in a thread in Java, and the listener call is no exception. Whether the listener message handling prevents the processing of other messages in the queue depends on how many threads are dedicated to message processing. If you configure Spring to use a pool of 5 threads to process messages asynchronously, then 5 listeners will be able to process messages in parallel.

这篇关于如何理解“同步”和“asynchronouns” JMS中的消息传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆