blob: cd28243765956f2fac82760bfba2ef13ab6a8db7 [file] [log] [blame]
<!DOCTYPE html>
<!--
-
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
-
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>5.4.&#160;Session - Apache Qpid&#8482;</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="/site.css" type="text/css" async="async"/>
<link rel="stylesheet" href="/deferred.css" type="text/css" defer="defer"/>
<script type="text/javascript">var _deferredFunctions = [];</script>
<script type="text/javascript" src="/deferred.js" defer="defer"></script>
<!--[if lte IE 8]>
<link rel="stylesheet" href="/ie.css" type="text/css"/>
<script type="text/javascript" src="/html5shiv.js"></script>
<![endif]-->
<!-- Redirects for `go get` and godoc.org -->
<meta name="go-import"
content="qpid.apache.org git https://git-wip-us.apache.org/repos/asf/qpid-proton.git"/>
<meta name="go-source"
content="qpid.apache.org
https://github.com/apache/qpid-proton/blob/go1/README.md
https://github.com/apache/qpid-proton/tree/go1{/dir}
https://github.com/apache/qpid-proton/blob/go1{/dir}/{file}#L{line}"/>
</head>
<body>
<div id="-content">
<div id="-top" class="panel">
<a id="-menu-link"><img width="16" height="16" src="" alt="Menu"/></a>
<a id="-search-link"><img width="22" height="16" src="" alt="Search"/></a>
<ul id="-global-navigation">
<li><a id="-logotype" href="/index.html">Apache Qpid<sup>&#8482;</sup></a></li>
<li><a href="/documentation.html">Documentation</a></li>
<li><a href="/download.html">Download</a></li>
<li><a href="/discussion.html">Discussion</a></li>
</ul>
</div>
<div id="-menu" class="panel" style="display: none;">
<div class="flex">
<section>
<h3>Project</h3>
<ul>
<li><a href="/overview.html">Overview</a></li>
<li><a href="/components/index.html">Components</a></li>
<li><a href="/releases/index.html">Releases</a></li>
</ul>
</section>
<section>
<h3>Messaging APIs</h3>
<ul>
<li><a href="/proton/index.html">Qpid Proton</a></li>
<li><a href="/components/jms/index.html">Qpid JMS</a></li>
<li><a href="/components/messaging-api/index.html">Qpid Messaging API</a></li>
</ul>
</section>
<section>
<h3>Servers and tools</h3>
<ul>
<li><a href="/components/broker-j/index.html">Broker-J</a></li>
<li><a href="/components/cpp-broker/index.html">C++ broker</a></li>
<li><a href="/components/dispatch-router/index.html">Dispatch router</a></li>
</ul>
</section>
<section>
<h3>Resources</h3>
<ul>
<li><a href="/dashboard.html">Dashboard</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/qpid/Index">Wiki</a></li>
<li><a href="/resources.html">More resources</a></li>
</ul>
</section>
</div>
</div>
<div id="-search" class="panel" style="display: none;">
<form action="http://www.google.com/search" method="get">
<input type="hidden" name="sitesearch" value="qpid.apache.org"/>
<input type="text" name="q" maxlength="255" autofocus="autofocus" tabindex="1"/>
<button type="submit">Search</button>
<a href="/search.html">More ways to search</a>
</form>
</div>
<div id="-middle" class="panel">
<ul id="-path-navigation"><li><a href="/index.html">Home</a></li><li><a href="/releases/index.html">Releases</a></li><li><a href="/releases/qpid-jms-amqp-0-x-6.3.1/index.html">Qpid JMS AMQP 0-x 6.3.1</a></li><li><a href="/releases/qpid-jms-amqp-0-x-6.3.1/jms-amqp-0-8-book/index.html">Apache Qpid JMS AMQP 0-8/0-9/0-9-1</a></li><li>5.4.&#160;Session</li></ul>
<div id="-middle-content">
<div class="docbook"><div class="navheader"><table summary="Navigation header" width="100%"><tr><th align="center" colspan="3">5.4.&#160;Session</th></tr><tr><td align="left" width="20%"><a accesskey="p" href="JMS-Client-0-8-Client-Understanding-Connection.html">Prev</a>&#160;</td><th align="center" width="60%">Chapter&#160;5.&#160;Understanding the Client</th><td align="right" width="20%">&#160;<a accesskey="n" href="JMS-Client-0-8-Client-Understanding-MessageProducer.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="JMS-Client-0-8-Client-Understanding-Session"></a>5.4.&#160;Session</h2></div></div></div><p>A Session object is a single-threaded context for producing and consuming messages.</p><p>Session objects are created from the Connection. Whilst Session objects are relatively
lightweight, patterns utilising a single Session per message are not recommended.</p><p>The number of sessions open per connection at any one time is limited. This value is
negotiated when the connection is made. It defaults to 256.</p><p>Qpid JMS Sessions have the ability to prefetch messages to improve consumer performance.
This feature is described next.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="JMS-Client-0-8-Client-Understanding-Session-Prefecth"></a>5.4.1.&#160;Prefetch</h3></div></div></div><p>Prefetch specifies how many messages the client will optimistically cache for delivery
to a consumer. This is a useful parameter to tune that can improve the throughput of an
application. The prefetch buffer is scoped per <span class="emphasis"><em>Session</em></span>.</p><p>The size of the prefetch buffer can be tuned per Connection using the connection url
option <a class="link" href="JMS-Client-0-8-Connection-URL.html#JMS-Client-0-8-Connection-URL-ConnectionOptions-Maxprefetch"><code class="literal">maxprefetch</code></a> (or JVM wide using the system property <a class="link" href="JMS-Client-0-8-System-Properties.html#JMS-Client-0-8-System-Properties-Maxprefetch"><code class="literal">max_prefetch</code></a>). By default, prefetch defaults to 500.</p><p>There are situations when you may wish to consider reducing the size of prefetch:</p><p>
</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>When using a <a class="link" href="http://www.eaipatterns.com/CompetingConsumers.html" target="_top">Competing Consumers</a> pattern, prefetch can give the appearance of unequal
division of work. This will be apparent on startup when the queue has messages. The
first consumer started will cache prefetch size number of messages, possibly leaving
the other consumers with no initial work.</p></li><li class="listitem"><p>When using special queue types (such as LVQs, Sorted Queue and Priority Queues).
For these queue types the special delivery rules apply whilst the message resides on
the Broker. As soon as the message is sent to the client it delivery order is then
fixed. For example, if using a priority queue, and a prefetch of 100, and 100 messages
arrive with priority 2, the broker will send these to the client. If then a new
message arrives with priority 1, the broker cannot leap frog messages of the lower
priority. The priority 1 message will be delivered at the front of the next
batch.</p></li><li class="listitem"><p>When message size is large and you do not wish the memory footprint of the
application to grow (or suffer an OutOfMemoryError).</p></li></ol></div><p>
</p><p>Finally, if using multiple MessageConsumers on a single Session, keep in mind that
unless you keep polling <span class="emphasis"><em>all</em></span> consumers, it is possible for some traffic
patterns to result in consumer starvation and an application level deadlock. For example, if
prefetch is 100, and 100 hundred messages arrive suitable for consumer A, those messages
will be prefetched by the session, entirely filling the prefetch buffer. Now if the
application performs a blocking <a class="link" href="http://docs.oracle.com/javaee/6/api/javax/jms/MessageConsumer.html#receive()" target="_top">MessageConsumer#receive()</a> for Consumer B on the same Session, the application
will hang indefinitely as even if messages suitable for B arrive at the Broker. Those
messages can never be sent to the Session as no space is available in prefetch. </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>Please note, when the acknowledgement mode
<span class="emphasis"><em>Session#SESSION_TRANSACTED</em></span> or
<span class="emphasis"><em>Session#CLIENT_ACKNOWLEDGE</em></span> is set on a consuming session, the
prefetched messages are released from the prefetch buffer on transaction commit/rollback
(in case of acknowledgement mode <span class="emphasis"><em>Session#SESSION_TRANSACTED</em></span> ) or
acknowledgement of the messages receipt (in case of acknowledgement mode
<span class="emphasis"><em>Session#CLIENT_ACKNOWLEDGE</em></span> ). If the consuming application does not
commit/rollback the receiving transaction (for example, due to mistakes in application
exception handling logic), the prefetched messages continue to remain in the prefetch
buffer preventing the delivery of the following messages. As result, the application might
stop the receiving of the messages until the transaction is committed/rolled back (for
<span class="emphasis"><em>Session#SESSION_TRANSACTED</em></span> ) or received messages are acknowledged
(for <span class="emphasis"><em>Session#CLIENT_ACKNOWLEDGE</em></span>).</p></div><p>
Settings maxprefetch to 0 ( either globally via JVM system property
<a class="link" href="JMS-Client-0-8-System-Properties.html#JMS-Client-0-8-System-Properties-Maxprefetch"><code class="literal">max_prefetch</code></a>
or on a connection level as a connection option
<a class="link" href="JMS-Client-0-8-Connection-URL.html#JMS-Client-0-8-Connection-URL-ConnectionOptions-Maxprefetch"><code class="literal">maxprefetch</code></a> )
switches off the pre-fetching functionality. With maxprefetch=0 messages are fetched one by one without caching on the client.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p> Setting maxprefetch to 0 is recommended in Spring-JMS based applications whenever
<span class="emphasis"><em>DefaultMassgeListenerContainer</em></span> is configured with a
<span class="emphasis"><em>CachingConnectionFactory</em></span> that has <span class="emphasis"><em>cacheLevel</em></span>
set to either <span class="emphasis"><em>CACHE_CONSUMER</em></span> or <span class="emphasis"><em>CACHE_SESSION</em></span>.
In these configurations the Qpid JMS <span class="emphasis"><em>Session</em></span> objects remain open in
Spring's dynamically scaled pools. If maxprefetch is not 0, any prefetched messages held
by the <span class="emphasis"><em>Session</em></span> and any new ones subsequently sent to it (in the
background until prefetch is reached) will be effectively by 'stuck' (unavailable to the
application) until Spring decides to utilise the cached Session again. This can give the
impression that message delivery has stopped even though messages remain of the queue.
Setting maxprefetch to 0 prevents this problem from occurring.</p><p> If using maxprefetch &gt; 0 <span class="emphasis"><em>SingleConnectionFactory</em></span> must be
used. SingleConnectionFactory does not have the same session/consumer caching behaviour so
does not exhibit the same problem. </p></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="JMS-Client-0-8-Client-Understanding-Session-TemporaryQueues"></a>5.4.2.&#160;TemporaryQueues</h3></div></div></div><p>Temporary queues are exposed to Management in the same way as normal queues. Temporary
queue names take the form string <code class="literal">TempQueue</code> followed by a random
UUID.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="JMS-Client-0-8-Client-Understanding-Session-CreateQueue"></a>5.4.3.&#160;CreateQueue</h3></div></div></div><p>In the Client, <a class="link" href="http://docs.oracle.com/javaee/6/api/javax/jms/Session.html#createQueue(java.lang.String)" target="_top">Session#createQueue()</a> accepts either a queue name, or a Binding URL. If only name
is specified the destination will be resolved into binding URL:
direct://amq.direct//&lt;queue name&gt;?routingkey='&lt;queue name&gt;'&amp;durable='true'. </p><p>Calling Session#createQueue() has no effect on the Broker.</p><p>Reiterating the advice from the JMS javadoc, it is suggested that this method is not
generally used. Instead, application should lookup Destinations declared within JNDI.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="JMS-Client-0-8-Client-Understanding-Session-CreateTopic"></a>5.4.4.&#160;CreateTopic</h3></div></div></div><p>In the Client, <a class="link" href="http://docs.oracle.com/javaee/6/api/javax/jms/Session.html#createTopic(java.lang.String)" target="_top">Session#createTopic()</a> accepts either a topic name, or a Binding URL. If only name
is specified the destination will be resolved into binding URL: topic://amq.topic//&lt;topic
name&gt;?routingkey='&lt;topic name&gt;'.</p><p>Calling Session#createTopic() has no effect on the Broker.</p><p>Reiterating the advice from the JMS javadoc, it is suggested that this method is not
generally used. Instead, application should lookup Destinations declared within JNDI.</p></div></div><div class="navfooter"><hr /><table summary="Navigation footer" width="100%"><tr><td align="left" width="40%"><a accesskey="p" href="JMS-Client-0-8-Client-Understanding-Connection.html">Prev</a>&#160;</td><td align="center" width="20%"><a accesskey="u" href="JMS-Client-0-8-Client-Understanding.html">Up</a></td><td align="right" width="40%">&#160;<a accesskey="n" href="JMS-Client-0-8-Client-Understanding-MessageProducer.html">Next</a></td></tr><tr><td align="left" valign="top" width="40%">5.3.&#160;Connection&#160;</td><td align="center" width="20%"><a accesskey="h" href="JMS-Client-Book.html">Home</a></td><td align="right" valign="top" width="40%">&#160;5.5.&#160;MessageProducer</td></tr></table></div></div>
<hr/>
<ul id="-apache-navigation">
<li><a href="http://www.apache.org/">Apache</a></li>
<li><a href="http://www.apache.org/licenses/">License</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Thanks!</a></li>
<li><a href="/security.html">Security</a></li>
<li><a href="http://www.apache.org/"><img id="-apache-feather" width="48" height="14" src="" alt="Apache"/></a></li>
</ul>
<p id="-legal">
Apache Qpid, Messaging built on AMQP; Copyright &#169; 2015
The Apache Software Foundation; Licensed under
the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache
License, Version 2.0</a>; Apache Qpid, Qpid, Qpid Proton,
Proton, Apache, the Apache feather logo, and the Apache Qpid
project logo are trademarks of The Apache Software
Foundation; All other marks mentioned may be trademarks or
registered trademarks of their respective owners
</p>
</div>
</div>
</div>
</body>
</html>