Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

Chapter 14. Temporary Queues and Runtime Queues


When designing a request-reply pattern where a client sends a request and waits for a reply, you must consider whether each runtime instance of the client requires a dedicated queue for its replies, or whether the runtime instances can access a shared queue, selecting their specific reply messages based on an appropriate attribute.

If multiple queues are required, then clients need the ability to create a queue dynamically. JMS provides this facility using the concept of temporary queues. A TemporaryQueue is created on request by the Session. It exists for the life of the Connection, for example until the connection is closed, or until the temporary queue is deleted. This means that although the temporary queue is created by a specific session, it can be reused by any other sessions created from the same connection.

The trade-off between using a shared queue and individual temporary queues for replies is influenced by the potential number of active client instances. With a shared-queue approach, at some provider-specific threshold, contention for access to the queue can become a concern. This has to be contrasted against the additional overhead associated with the provider creating queue storage at runtime and the impact on machine memory of hosting a potentially large number of temporary queues.

The following example creates a temporary queue and consumer for each client on startup. It sets the JMSReplyTo property on each message to the temporary queue, and then sets a correlation ID on each message to correlate request messages to response messages. This avoids the overhead of creating and closing a consumer for each request, which is expensive. The same producer and consumer can be shared or pooled across many threads. Any messages that have been received, but not yet acknowledged when the session terminates, are retained and redelivered when a consumer next accesses the queue.

Temporary Queue Code Example

...
// Create a temporary queue, one per client
Destination temporaryQueue = session.createTemporaryQueue();
MessageConsumer responseConsumer = session.createConsumer(temporaryQueue);

// This class handles messages to the temporary queue
responseConsumer.setMessageListener(this);

// Create the message to send
TextMessage textMessage = session.createTextMessage();
textMessage.setText("My new message!");

// Set the reply to field and correlation ID
textMessage.setJMSReplyTo(temporaryQueue);
textMessage.setJMSCorrelationID(myCorrelationID);

producer.send(textMessage);
...

In a similar manner, temporary topics are created using the Session.createTemporaryTopic() method.

Red Hat logoGithubRedditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

© 2024 Red Hat, Inc.