Topics vs. Queues Asynchronous vs. Synchronous Consumers

Best Practices for Application Design 3-3

3.4 Message Ordering

You should use the Message Unit-of-Order feature rather than Ordered Redelivery to guarantee ordered message processing. The advantages of Message Unit-of-Order over Ordered Redelivery are: ■ Ease of configuration. – Does not require a custom connection factory for asynchronous receivers, such as setting MessagingMaximum to 1 when using message-driven beans MDBs. – Simple configuration when using distributed destinations. ■ Preserves message order during processing delays. ■ Preserves message order during transaction rollback or session recovery. Oracle recommends applications that use Ordered Redelivery upgrade to Message Unit-of-Order. For more information, see Chapter 10, Using Message Unit-of-Order.

3.5 Topics vs. Queues

Surprisingly, when you are starting to design your application, it is not always immediately obvious whether it would be better to use a Topic or Queue. In general, you should choose a Topic only if one of the following conditions applies: ■ The same message must be replicated to multiple consumers. ■ A message should be dropped if there are no active consumers that would select it. ■ There are many subscribers, each with a unique selector. It is interesting to note that a topic with a single durable subscriber is semantically similar to a queue. The differences are as follows: ■ If you change a topic selector for a durable subscriber, all previous messages in the subscription are deleted, while if you change a queue selector for consumer, no messages in the queue are deleted. ■ A queue may have multiple consumers, and will distribute its messages in a round-robin fashion, whereas a topic subscriber is limited to only one consumer. For more information on configuring JMS queues and topics, see Queue and Topic Destination Resources in Configuring and Managing JMS for Oracle WebLogic Server.

3.6 Asynchronous vs. Synchronous Consumers

In general, asynchronous onMessage consumers perform and scale better than synchronous consumers: ■ Asynchronous consumers create less network traffic. Messages are pushed unidirectionally, and are pipelined to the message listener. Pipelining supports the aggregation of multiple messages into a single network call. Note: In WebLogic Server, your synchronous consumers can also use the same efficient behavior as asynchronous consumers by enabling the Prefetch Mode for Synchronous Consumers option on JMS connection factories, as described in Section 5.4.2.1, Use Prefetch Mode to Create a Synchronous Message Pipeline. 3-4 Programming JMS for Oracle WebLogic Server ■ Asynchronous consumers use fewer threads. An asynchronous consumer does not use a thread while it is inactive. A synchronous consumer consumes a thread for the duration of its receive call. As a result, a thread can remain idle for long periods, especially if the call specifies a blocking timeout. ■ For application code that runs on a server, it is almost always best to use asynchronous consumers, typically via MDBs. The use of asynchronous consumers prevents the application code from doing a blocking operation on the server. A blocking operation, in turn, idles a server-side thread; it can even cause deadlocks. Deadlocks occur when blocking operations consume all threads. When no threads remain to handle the operations required to unblock the blocking operation itself, that operation never stops blocking. For more information, see Section 5.4.1, Receiving Messages Asynchronously and Section 5.4.2, Receiving Messages Synchronously.

3.7 Persistent vs. Non-Persistent Messages