它是使用JMS临时队列同步使用好的做法呢? [英] Is it a good practice to use JMS Temporary Queue for synchronous use?

查看:288
本文介绍了它是使用JMS临时队列同步使用好的做法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们使用使用临时队列JMS请求/应答机制,将是code可扩展性?

If we use JMS request/reply mechanism using "Temporary Queue", will that code be scalable?

截至目前,我们不知道我们是否会支持每秒100个请求,或每秒请求1000。

As of now, we don't know if we will supporting 100 requests per second, or 1000s of requests per second.

在code以下就是我想实现的。这使得在一个同步方式使用JMS的。关键部位是哪里消费者被创建指向一个临时队列这是为这个会话创建。我只是想不通使用这种临时队列是否是一个可扩展的设计。

The code below is what I am thinking of implementing. It makes use of JMS in a 'Synchronous' fashion. The key parts are where the 'Consumer' gets created to point a 'Temporary Queue' that was created for this session. I just can't figure out whether using such Temporary Queues is a scalable design.

  destination = session.createQueue("queue:///Q1");
  producer = session.createProducer(destination);
  tempDestination = session.createTemporaryQueue();
  consumer = session.createConsumer(tempDestination);

  long uniqueNumber = System.currentTimeMillis() % 1000;
  TextMessage message = session
      .createTextMessage("SimpleRequestor: Your lucky number today is " + uniqueNumber);

  // Set the JMSReplyTo
  message.setJMSReplyTo(tempDestination);

  // Start the connection
  connection.start();

  // And, send the request
  producer.send(message);
  System.out.println("Sent message:\n" + message);

  // Now, receive the reply
  Message receivedMessage = consumer.receive(15000); // in ms or 15 seconds
  System.out.println("\nReceived message:\n" + receivedMessage);

更新:

我遇到另一种模式来了,见<一href=\"http://$c$cdependents.com/2010/03/04/synchronous-request-response-with-activemq-and-spring/\">this博客
我们的想法是使用常规队列发送和接收。然而,对于同步电话,以获得所需的响应(即符合要求),您可以创建监听使用选择接收队列中的消费者。

I came across another pattern, see this blog The idea is to use 'regular' Queues for both Send and Receive. However for 'Synchronous' calls, in order to get the desired Response (i.e. matching the request), you create a Consumer that listens to the Receive queue using a 'Selector'.

步骤:

    // 1. Create Send and Receive Queue.
    // 2. Create a msg with a specific ID
 final String correlationId = UUID.randomUUID().toString();
 final TextMessage textMessage = session.createTextMessage( msg );
 textMessage.setJMSCorrelationID( correlationId );

    // 3. Start a consumer that receives using a 'Selector'.
           consumer = session.createConsumer( replyQueue, "JMSCorrelationID = '" + correlationId + "'" );

因此​​,在这种模式不同的是,我们并没有创建一个新请求一个新的临时队列。
相反,所有答复都只有一个队列,但使用一个选择以确保每个请求线程接收的只有大约关心的响应。

So the difference in this pattern is that we don't create a new temp Queue for each new request. Instead all responses come to only one queue, but use a 'selector' to make sure each request-thread receives the only the response that is cares about.

我觉得这里的缺点是,你必须使用一个'选择'。我还不知道这是不太preferred或pferred比前面提到的模式更为$ P $。思考?

I think the downside here is that you have to use a 'selector'. I don't know yet if that is less preferred or more preferred than earlier mentioned pattern. Thoughts?

推荐答案

关于在您的文章更新 - 选择是,如果对邮件头进行的,就像你与相关ID做非常有效的。 Spring集成内部也做这行<一个href=\"https://github.com/SpringSource/spring-integration/blob/master/spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsOutboundGateway.java\">implementing一个JMS出站网关。

Regarding the update in your post - selectors are very efficient if performed on the message headers, like you are doing with the Correlation ID. Spring Integration also internally does this for implementing a JMS Outbound gateway.

这篇关于它是使用JMS临时队列同步使用好的做法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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