activemq-artemis/docs/user-manual/en/message-expiry.xml

101 lines
5.6 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- ============================================================================= -->
<!-- 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. -->
<!-- ============================================================================= -->
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % BOOK_ENTITIES SYSTEM "ActiveMQ_User_Manual.ent">
%BOOK_ENTITIES;
]>
<chapter id="message-expiry">
<title>Message Expiry</title>
<para>Messages can be set with an optional <emphasis>time to live</emphasis> when sending
them.</para>
<para>ActiveMQ will not deliver a message to a consumer after it's time to live has been exceeded.
If the message hasn't been delivered by the time that time to live is reached the server can
discard it.</para>
<para>ActiveMQ's addresses can be assigned a expiry address so that, when messages are expired,
they are removed from the queue and sent to the expiry address. Many different queues can be
bound to an expiry address. These <emphasis>expired</emphasis> messages can later be consumed
for further inspection.</para>
<section>
<title>Message Expiry</title>
<para>Using ActiveMQ Core API, you can set an expiration time directly on the message:</para>
<programlisting>
// message will expire in 5000ms from now
message.setExpiration(System.currentTimeMillis() + 5000);</programlisting>
<para>JMS MessageProducer allows to set a TimeToLive for the messages it sent:</para>
<programlisting>
// messages sent by this producer will be retained for 5s (5000ms) before expiration
producer.setTimeToLive(5000);</programlisting>
<para>Expired messages which are consumed from an expiry address have the following
properties:</para>
<itemizedlist>
<listitem>
<para><literal>_HQ_ORIG_ADDRESS</literal></para>
<para>a String property containing the <emphasis>original address</emphasis> of the
expired message </para>
</listitem>
<listitem>
<para><literal>_HQ_ORIG_QUEUE</literal></para>
<para>a String property containing the <emphasis>original queue</emphasis> of the
expired message </para>
</listitem>
<listitem>
<para><literal>_HQ_ACTUAL_EXPIRY</literal></para>
<para>a Long property containing the <emphasis>actual expiration time</emphasis> of the
expired message</para>
</listitem>
</itemizedlist>
</section>
<section id="message-expiry.configuring">
<title>Configuring Expiry Addresses</title>
<para>Expiry address are defined in the address-setting configuration:</para>
<programlisting>
&lt;!-- expired messages in exampleQueue will be sent to the expiry address expiryQueue -->
&lt;address-setting match="jms.queue.exampleQueue">
&lt;expiry-address>jms.queue.expiryQueue&lt;/expiry-address>
&lt;/address-setting></programlisting>
<para>If messages are expired and no expiry address is specified, messages are simply removed
from the queue and dropped. Address wildcards can be used to configure expiry address for a
set of addresses (see <xref linkend="wildcard-syntax"/>).</para>
</section>
<section id="configuring.expiry.reaper">
<title>Configuring The Expiry Reaper Thread</title>
<para>A reaper thread will periodically inspect the queues to check if messages have
expired.</para>
<para>The reaper thread can be configured with the following properties in <literal
>activemq-configuration.xml</literal></para>
<itemizedlist>
<listitem>
<para><literal>message-expiry-scan-period</literal></para>
<para>How often the queues will be scanned to detect expired messages (in milliseconds,
default is 30000ms, set to <literal>-1</literal> to disable the reaper thread)</para>
</listitem>
<listitem>
<para><literal>message-expiry-thread-priority</literal></para>
<para>The reaper thread priority (it must be between 0 and 9, 9 being the highest
priority, default is 3)</para>
</listitem>
</itemizedlist>
</section>
<section>
<title>Example</title>
<para>See <xref linkend="examples.expiry"/> for an example which shows how message expiry is
configured and used with JMS.</para>
</section>
</chapter>