blob: 12a699a4f6bbbcf4f1e8ceb38df0f1cc49a7f8ea [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Detecting Dead Connections | ActiveMQ Artemis Documentation</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.1.0">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="next" href="./slow-consumers.html" />
<link rel="prev" href="./configuring-transports.html" />
</head>
<body>
<div class="book" data-level="17" data-basepath="." data-revision="Fri Dec 18 2015 09:51:48 GMT+0000 (GMT)">
<div class="book-summary">
<div class="book-search">
<input type="text" placeholder="Type to search" class="form-control" />
</div>
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="notice.html">
<a href="./notice.html">
<i class="fa fa-check"></i>
<b>1.</b>
Legal Notice
</a>
</li>
<li class="chapter " data-level="2" data-path="preface.html">
<a href="./preface.html">
<i class="fa fa-check"></i>
<b>2.</b>
Preface
</a>
</li>
<li class="chapter " data-level="3" data-path="project-info.html">
<a href="./project-info.html">
<i class="fa fa-check"></i>
<b>3.</b>
Project Info
</a>
</li>
<li class="chapter " data-level="4" data-path="messaging-concepts.html">
<a href="./messaging-concepts.html">
<i class="fa fa-check"></i>
<b>4.</b>
Messaging Concepts
</a>
</li>
<li class="chapter " data-level="5" data-path="architecture.html">
<a href="./architecture.html">
<i class="fa fa-check"></i>
<b>5.</b>
Architecture
</a>
</li>
<li class="chapter " data-level="6" data-path="using-server.html">
<a href="./using-server.html">
<i class="fa fa-check"></i>
<b>6.</b>
Using the Server
</a>
</li>
<li class="chapter " data-level="7" data-path="using-jms.html">
<a href="./using-jms.html">
<i class="fa fa-check"></i>
<b>7.</b>
Using JMS
</a>
</li>
<li class="chapter " data-level="8" data-path="using-core.html">
<a href="./using-core.html">
<i class="fa fa-check"></i>
<b>8.</b>
Using Core
</a>
</li>
<li class="chapter " data-level="9" data-path="jms-core-mapping.html">
<a href="./jms-core-mapping.html">
<i class="fa fa-check"></i>
<b>9.</b>
Mapping JMS Concepts to the Core API
</a>
</li>
<li class="chapter " data-level="10" data-path="client-classpath.html">
<a href="./client-classpath.html">
<i class="fa fa-check"></i>
<b>10.</b>
The Client Classpath
</a>
</li>
<li class="chapter " data-level="11" data-path="examples.html">
<a href="./examples.html">
<i class="fa fa-check"></i>
<b>11.</b>
Examples
</a>
</li>
<li class="chapter " data-level="12" data-path="wildcard-routing.html">
<a href="./wildcard-routing.html">
<i class="fa fa-check"></i>
<b>12.</b>
Routing Messages With Wild Cards
</a>
</li>
<li class="chapter " data-level="13" data-path="wildcard-syntax.html">
<a href="./wildcard-syntax.html">
<i class="fa fa-check"></i>
<b>13.</b>
Understanding the Apache ActiveMQ Artemis Wildcard Syntax
</a>
</li>
<li class="chapter " data-level="14" data-path="filter-expressions.html">
<a href="./filter-expressions.html">
<i class="fa fa-check"></i>
<b>14.</b>
Filter Expressions
</a>
</li>
<li class="chapter " data-level="15" data-path="persistence.html">
<a href="./persistence.html">
<i class="fa fa-check"></i>
<b>15.</b>
Persistence
</a>
</li>
<li class="chapter " data-level="16" data-path="configuring-transports.html">
<a href="./configuring-transports.html">
<i class="fa fa-check"></i>
<b>16.</b>
Configuring Transports
</a>
</li>
<li class="chapter active" data-level="17" data-path="connection-ttl.html">
<a href="./connection-ttl.html">
<i class="fa fa-check"></i>
<b>17.</b>
Detecting Dead Connections
</a>
</li>
<li class="chapter " data-level="18" data-path="slow-consumers.html">
<a href="./slow-consumers.html">
<i class="fa fa-check"></i>
<b>18.</b>
Detecting Slow Consumers
</a>
</li>
<li class="chapter " data-level="19" data-path="transaction-config.html">
<a href="./transaction-config.html">
<i class="fa fa-check"></i>
<b>19.</b>
Resource Manager Configuration
</a>
</li>
<li class="chapter " data-level="20" data-path="flow-control.html">
<a href="./flow-control.html">
<i class="fa fa-check"></i>
<b>20.</b>
Flow Control
</a>
</li>
<li class="chapter " data-level="21" data-path="send-guarantees.html">
<a href="./send-guarantees.html">
<i class="fa fa-check"></i>
<b>21.</b>
Guarantees of sends and commits
</a>
</li>
<li class="chapter " data-level="22" data-path="undelivered-messages.html">
<a href="./undelivered-messages.html">
<i class="fa fa-check"></i>
<b>22.</b>
Message Redelivery and Undelivered Messages
</a>
</li>
<li class="chapter " data-level="23" data-path="message-expiry.html">
<a href="./message-expiry.html">
<i class="fa fa-check"></i>
<b>23.</b>
Message Expiry
</a>
</li>
<li class="chapter " data-level="24" data-path="large-messages.html">
<a href="./large-messages.html">
<i class="fa fa-check"></i>
<b>24.</b>
Large Messages
</a>
</li>
<li class="chapter " data-level="25" data-path="paging.html">
<a href="./paging.html">
<i class="fa fa-check"></i>
<b>25.</b>
Paging
</a>
</li>
<li class="chapter " data-level="26" data-path="queue-attributes.html">
<a href="./queue-attributes.html">
<i class="fa fa-check"></i>
<b>26.</b>
Queue Attributes
</a>
</li>
<li class="chapter " data-level="27" data-path="scheduled-messages.html">
<a href="./scheduled-messages.html">
<i class="fa fa-check"></i>
<b>27.</b>
Scheduled Messages
</a>
</li>
<li class="chapter " data-level="28" data-path="last-value-queues.html">
<a href="./last-value-queues.html">
<i class="fa fa-check"></i>
<b>28.</b>
Last-Value Queues
</a>
</li>
<li class="chapter " data-level="29" data-path="message-grouping.html">
<a href="./message-grouping.html">
<i class="fa fa-check"></i>
<b>29.</b>
Message Grouping
</a>
</li>
<li class="chapter " data-level="30" data-path="pre-acknowledge.html">
<a href="./pre-acknowledge.html">
<i class="fa fa-check"></i>
<b>30.</b>
Extra Acknowledge Modes
</a>
</li>
<li class="chapter " data-level="31" data-path="management.html">
<a href="./management.html">
<i class="fa fa-check"></i>
<b>31.</b>
Management
</a>
</li>
<li class="chapter " data-level="32" data-path="security.html">
<a href="./security.html">
<i class="fa fa-check"></i>
<b>32.</b>
Security
</a>
</li>
<li class="chapter " data-level="33" data-path="resource-limits.html">
<a href="./resource-limits.html">
<i class="fa fa-check"></i>
<b>33.</b>
Resource Limits
</a>
</li>
<li class="chapter " data-level="34" data-path="jms-bridge.html">
<a href="./jms-bridge.html">
<i class="fa fa-check"></i>
<b>34.</b>
The JMS Bridge
</a>
</li>
<li class="chapter " data-level="35" data-path="client-reconnection.html">
<a href="./client-reconnection.html">
<i class="fa fa-check"></i>
<b>35.</b>
Client Reconnection and Session Reattachment
</a>
</li>
<li class="chapter " data-level="36" data-path="diverts.html">
<a href="./diverts.html">
<i class="fa fa-check"></i>
<b>36.</b>
Diverting and Splitting Message Flows
</a>
</li>
<li class="chapter " data-level="37" data-path="core-bridges.html">
<a href="./core-bridges.html">
<i class="fa fa-check"></i>
<b>37.</b>
Core Bridges
</a>
</li>
<li class="chapter " data-level="38" data-path="duplicate-detection.html">
<a href="./duplicate-detection.html">
<i class="fa fa-check"></i>
<b>38.</b>
Duplicate Message Detection
</a>
</li>
<li class="chapter " data-level="39" data-path="clusters.html">
<a href="./clusters.html">
<i class="fa fa-check"></i>
<b>39.</b>
Clusters
</a>
</li>
<li class="chapter " data-level="40" data-path="ha.html">
<a href="./ha.html">
<i class="fa fa-check"></i>
<b>40.</b>
High Availability and Failover
</a>
</li>
<li class="chapter " data-level="41" data-path="graceful-shutdown.html">
<a href="./graceful-shutdown.html">
<i class="fa fa-check"></i>
<b>41.</b>
Graceful Server Shutdown
</a>
</li>
<li class="chapter " data-level="42" data-path="libaio.html">
<a href="./libaio.html">
<i class="fa fa-check"></i>
<b>42.</b>
Libaio Native Libraries
</a>
</li>
<li class="chapter " data-level="43" data-path="thread-pooling.html">
<a href="./thread-pooling.html">
<i class="fa fa-check"></i>
<b>43.</b>
Thread management
</a>
</li>
<li class="chapter " data-level="44" data-path="logging.html">
<a href="./logging.html">
<i class="fa fa-check"></i>
<b>44.</b>
Logging
</a>
</li>
<li class="chapter " data-level="45" data-path="rest.html">
<a href="./rest.html">
<i class="fa fa-check"></i>
<b>45.</b>
REST Interface
</a>
</li>
<li class="chapter " data-level="46" data-path="embedding-activemq.html">
<a href="./embedding-activemq.html">
<i class="fa fa-check"></i>
<b>46.</b>
Embedding Apache ActiveMQ Artemis
</a>
</li>
<li class="chapter " data-level="47" data-path="spring-integration.html">
<a href="./spring-integration.html">
<i class="fa fa-check"></i>
<b>47.</b>
Spring Integration
</a>
</li>
<li class="chapter " data-level="48" data-path="aerogear-integration.html">
<a href="./aerogear-integration.html">
<i class="fa fa-check"></i>
<b>48.</b>
AeroGear Integration
</a>
</li>
<li class="chapter " data-level="49" data-path="vertx-integration.html">
<a href="./vertx-integration.html">
<i class="fa fa-check"></i>
<b>49.</b>
VertX Integration
</a>
</li>
<li class="chapter " data-level="50" data-path="intercepting-operations.html">
<a href="./intercepting-operations.html">
<i class="fa fa-check"></i>
<b>50.</b>
Intercepting Operations
</a>
</li>
<li class="chapter " data-level="51" data-path="protocols-interoperability.html">
<a href="./protocols-interoperability.html">
<i class="fa fa-check"></i>
<b>51.</b>
Protocols and Interoperability
</a>
</li>
<li class="chapter " data-level="52" data-path="tools.html">
<a href="./tools.html">
<i class="fa fa-check"></i>
<b>52.</b>
Tools
</a>
</li>
<li class="chapter " data-level="53" data-path="maven-plugin.html">
<a href="./maven-plugin.html">
<i class="fa fa-check"></i>
<b>53.</b>
Maven Plugin
</a>
</li>
<li class="chapter " data-level="54" data-path="perf-tuning.html">
<a href="./perf-tuning.html">
<i class="fa fa-check"></i>
<b>54.</b>
Troubleshooting and Performance Tuning
</a>
</li>
<li class="chapter " data-level="55" data-path="configuration-index.html">
<a href="./configuration-index.html">
<i class="fa fa-check"></i>
<b>55.</b>
Configuration Reference
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
<div id="font-settings-wrapper" class="dropdown pull-left">
<a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" id="reduce-font-size" class="button size-2">A</button>
<button type="button" id="enlarge-font-size" class="button size-2">A</button>
</div>
<div class="buttons font-family-list">
<button type="button" data-font="0" class="button">Serif</button>
<button type="button" data-font="1" class="button">Sans</button>
</div>
<div class="buttons color-theme-list">
<button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
<button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
<button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
</div>
</div>
</div>
<!-- Actions Right -->
<div class="dropdown pull-right">
<a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
</a>
<div class="dropdown-menu font-settings dropdown-left">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" data-sharing="twitter" class="button">
Share on Twitter
</button>
<button type="button" data-sharing="google-plus" class="button">
Share on Google
</button>
<button type="button" data-sharing="facebook" class="button">
Share on Facebook
</button>
<button type="button" data-sharing="weibo" class="button">
Share on Weibo
</button>
<button type="button" data-sharing="instapaper" class="button">
Share on Instapaper
</button>
</div>
</div>
</div>
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >ActiveMQ Artemis Documentation</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="detecting-dead-connections">Detecting Dead Connections</h1>
<p>In this section we will discuss connection time-to-live (TTL) and
explain how Apache ActiveMQ Artemis deals with crashed clients and clients which have
exited without cleanly closing their resources.</p>
<h2 id="cleaning-up-dead-connection-resources-on-the-server">Cleaning up Dead Connection Resources on the Server</h2>
<p>Before an Apache ActiveMQ Artemis client application exits it is considered good
practice that it should close its resources in a controlled manner,
using a <code>finally</code> block.</p>
<p>Here&apos;s an example of a well behaved core client application closing its
session and session factory in a finally block:</p>
<pre><code class="lang-java">ServerLocator locator = <span class="hljs-keyword">null</span>;
ClientSessionFactory sf = <span class="hljs-keyword">null</span>;
ClientSession session = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">try</span>
{
locator = ActiveMQClient.createServerLocatorWithoutHA(..);
sf = locator.createClientSessionFactory();;
session = sf.createSession(...);
... do some stuff with the session...
}
<span class="hljs-keyword">finally</span>
{
<span class="hljs-keyword">if</span> (session != <span class="hljs-keyword">null</span>)
{
session.close();
}
<span class="hljs-keyword">if</span> (sf != <span class="hljs-keyword">null</span>)
{
sf.close();
}
<span class="hljs-keyword">if</span>(locator != <span class="hljs-keyword">null</span>)
{
locator.close();
}
}
</code></pre>
<p>And here&apos;s an example of a well behaved JMS client application:</p>
<pre><code class="lang-java">Connection jmsConnection = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">try</span>
{
ConnectionFactory jmsConnectionFactory = ActiveMQJMSClient.createConnectionFactoryWithoutHA(...);
jmsConnection = jmsConnectionFactory.createConnection();
... do some stuff with the connection...
}
<span class="hljs-keyword">finally</span>
{
<span class="hljs-keyword">if</span> (connection != <span class="hljs-keyword">null</span>)
{
connection.close();
}
}
</code></pre>
<p>Unfortunately users don&apos;t always write well behaved applications, and
sometimes clients just crash so they don&apos;t have a chance to clean up
their resources!</p>
<p>If this occurs then it can leave server side resources, like sessions,
hanging on the server. If these were not removed they would cause a
resource leak on the server and over time this result in the server
running out of memory or other resources.</p>
<p>We have to balance the requirement for cleaning up dead client resources
with the fact that sometimes the network between the client and the
server can fail and then come back, allowing the client to reconnect.
Apache ActiveMQ Artemis supports client reconnection, so we don&apos;t want to clean up
&quot;dead&quot; server side resources too soon or this will prevent any client
from reconnecting, as it won&apos;t be able to find its old sessions on the
server.</p>
<p>Apache ActiveMQ Artemis makes all of this configurable. For each <code>ClientSessionFactory</code>
we define a <em>connection TTL</em>. Basically, the TTL determines how long the
server will keep a connection alive in the absence of any data arriving
from the client. The client will automatically send &quot;ping&quot; packets
periodically to prevent the server from closing it down. If the server
doesn&apos;t receive any packets on a connection for the connection TTL time,
then it will automatically close all the sessions on the server that
relate to that connection.</p>
<p>If you&apos;re using JMS, the connection TTL is defined by the
<code>ConnectionTTL</code> attribute on a <code>ActiveMQConnectionFactory</code> instance, or
if you&apos;re deploying JMS connection factory instances direct into JNDI on
the server side, you can specify it in the xml config, using the
parameter <code>connectionTtl</code>.</p>
<p>The default value for connection ttl on an &quot;unreliable&quot; connection (e.g.
a Netty connection) is <code>60000</code>ms, i.e. 1 minute. The default value for
connection ttl on a &quot;reliable&quot; connection (e.g. an in-vm connection) is
<code>-1</code>. A value of <code>-1</code> for <code>ConnectionTTL</code> means the server will never
time out the connection on the server side.</p>
<p>If you do not wish clients to be able to specify their own connection
TTL, you can override all values used by a global value set on the
server side. This can be done by specifying the
<code>connection-ttl-override</code> attribute in the server side configuration.
The default value for <code>connection-ttl-override</code> is <code>-1</code> which means &quot;do
not override&quot; (i.e. let clients use their own values).</p>
<h2 id="closing-core-sessions-or-jms-connections-that-you-have-failed-to-close">Closing core sessions or JMS connections that you have failed to close</h2>
<p>As previously discussed, it&apos;s important that all core client sessions
and JMS connections are always closed explicitly in a <code>finally</code> block
when you are finished using them.</p>
<p>If you fail to do so, Apache ActiveMQ Artemis will detect this at garbage collection
time, and log a warning similar to the following in the logs (If you are
using JMS the warning will involve a JMS connection not a client
session):</p>
<pre><code>[Finalizer] 20:14:43,244 WARNING [org.apache.activemq.artemis.core.client.impl.DelegatingSession] I&apos;m closing a ClientSession you left open. Please make sure you close all ClientSessions explicitly before let
ting them go out of scope!
[Finalizer] 20:14:43,244 WARNING [org.apache.activemq.artemis.core.client.impl.DelegatingSession] The session you didn&apos;t close was created here:
java.lang.Exception
at org.apache.activemq.artemis.core.client.impl.DelegatingSession.&lt;init&gt;(DelegatingSession.java:83)
at org.acme.yourproject.YourClass (YourClass.java:666)
</code></pre><p>Apache ActiveMQ Artemis will then close the connection / client session for you.</p>
<p>Note that the log will also tell you the exact line of your user code
where you created the JMS connection / client session that you later did
not close. This will enable you to pinpoint the error in your code and
correct it appropriately.</p>
<h2 id="detecting-failure-from-the-client-side">Detecting failure from the client side.</h2>
<p>In the previous section we discussed how the client sends pings to the
server and how &quot;dead&quot; connection resources are cleaned up by the server.
There&apos;s also another reason for pinging, and that&apos;s for the <em>client</em> to
be able to detect that the server or network has failed.</p>
<p>As long as the client is receiving data from the server it will consider
the connection to be still alive.</p>
<p>If the client does not receive any packets for
<code>client-failure-check-period</code> milliseconds then it will consider the
connection failed and will either initiate failover, or call any
<code>FailureListener</code> instances (or <code>ExceptionListener</code> instances if you are
using JMS) depending on how it has been configured.</p>
<p>If you&apos;re using JMS it&apos;s defined by the <code>ClientFailureCheckPeriod</code>
attribute on a <code>ActiveMQConnectionFactory</code> instance..</p>
<p>The default value for client failure check period on an &quot;unreliable&quot;
connection (e.g. a Netty connection) is <code>30000</code>ms, i.e. 30 seconds. The
default value for client failure check period on a &quot;reliable&quot; connection
(e.g. an in-vm connection) is <code>-1</code>. A value of <code>-1</code> means the client
will never fail the connection on the client side if no data is received
from the server. Typically this is much lower than connection TTL to
allow clients to reconnect in case of transitory failure.</p>
<h2 id="configuring-asynchronous-connection-execution">Configuring Asynchronous Connection Execution</h2>
<p>Most packets received on the server side are executed on the remoting
thread. These packets represent short-running operations and are always
executed on the remoting thread for performance reasons.</p>
<p>However, by default some kinds of packets are executed using a thread
from a thread pool so that the remoting thread is not tied up for too
long. Please note that processing operations asynchronously on another
thread adds a little more latency. These packets are:</p>
<ul>
<li><p><code>org.apache.activemq.artemis.core.protocol.core.impl.wireformat.RollbackMessage</code></p>
</li>
<li><p><code>org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionCloseMessage</code></p>
</li>
<li><p><code>org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionCommitMessage</code></p>
</li>
<li><p><code>org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionXACommitMessage</code></p>
</li>
<li><p><code>org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionXAPrepareMessage</code></p>
</li>
<li><p><code>org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionXARollbackMessage</code></p>
</li>
</ul>
<p>To disable asynchronous connection execution, set the parameter
<code>async-connection-execution-enabled</code> in <code>broker.xml</code> to
<code>false</code> (default value is <code>true</code>).</p>
</section>
</div>
</div>
</div>
<a href="./configuring-transports.html" class="navigation navigation-prev " aria-label="Previous page: Configuring Transports"><i class="fa fa-angle-left"></i></a>
<a href="./slow-consumers.html" class="navigation navigation-next " aria-label="Next page: Detecting Slow Consumers"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>