5.6 为消息消费者缓存消息

5.6 Caching messages in the broker for consumers

5.6 为消息消费者缓存消息

 

Although one of the most important aspects of message persistence is that the messages

will survive in long-term storage, there are a number of cases where messages are

required to be available for consumers that were disconnected from the broker, but

persisting the messages in a database is too slow. Real-time data delivery of pricing

information for a trading platform is a good example. But typically real-time data

applications use messages that are only valid for a finite amount of time, often less

than a minute. So it’s pointless to persist them to survive a system outage because new

messages will arrive soon.

 

虽然消息持久化的一个重要方面是消息能够从长时间存储中恢复,但是在更多情况下要求消息在消息

消费者与代理之间的连接断开后仍然可用.而将消息持久化到数据库中太慢了.交易平台中价格信息的实时分发

就是一个很好的例子.通常,实时数据应用程序使用的消息仅仅在有限的时间里有效,这个有效时间通常小于

一分钟.所以,没必要为保证系统正常运行而持久化这些消息,因为新的消息很快会到来.

 

ActiveMQ supports the caching of messages for these types of systems using message

caching in the broker by using something called a subscription recovery policy. This

configurable policy is used for deciding which types of messages should be cached,

how many, and for how long. In the rest of this section we’ll explain how message

caching works in ActiveMQ and how to configure the different types of subscription

recovery policies that are available.

ActiveMQ通过在代理中使用一种称为订阅恢复策略,为使用这种类型消息的系统缓存消息提供支持.

这种策略支持配置哪种类型的消息应该被缓存,以及缓存多少,缓存多久.本节的剩余部分,我们将讨论

ActiveMQ如何缓存消息以及如何配置不同的订阅恢复策略.

 

5.6.1 How message caching for consumers works

5.6.1 如何为消息消费者缓存消息

 

The ActiveMQ message broker caches messages in memory for every topic that’s used.

The only types of topics that aren’t supported are temporary topics and ActiveMQ

advisory topics. Caching of messages in this way isn’t handled for queues, as the normal

operation of a queue is to hold every message sent to it.

 

除了临时主题和ActiveMQ建议(advisory)主题,ActiveMQ消息代理在内存中缓存所有的正在使用消息主题中的消息.

ActiveMQ不会通过上述方式为消息队列缓存消息,因为通常情况下队列会保存所有发送给它的消息.

 

Messages that are cached by the broker are only dispatched to a topic consumer if

the consumer is retroactive, and never to durable topic subscribers.

Topic consumers are marked as being retroactive by a property set on the destination

when the topic consumer is created. Here’s an example:

 

代理中缓存的消息只能发送到具有消息追溯能力的消费者(译注:打开retroactive开关的消费者),

不会发送到配置成持久化订阅的主题订阅者.

在创建主题消费者时,可以通过设置目的地属性,将主题消费者设置为可追溯的(retroactive).

下面是一个示例代码:

 

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.MessageConsumer;

import javax.jms.Session;

import javax.jms.Topic;

 

public void createRetroactiveConsumer() throws JMSException

{

  ConnectionFactory fac = new ActiveMQConnectionFactory();

  Connection connection = fac.createConnection();

  connection.start();

  Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

  Topic topic = session.createTopic("TEST.TOPIC?consumer.retroactive=true");

  MessageConsumer consumer = session.createConsumer(topic);

}

 

On the broker side, the message caching is controlled by a destination policy called a

subscriptionRecoveryPolicy. The default subscription recovery policy used in the

broker is a FixedSizeSubscriptionRecoveryPolicy. Let’s walk through the different

subscription recovery policies that are available.

 

在代理端,消息缓存通过一个名称为subscriptionRecoveryPolicy(订阅恢复策略)的目的地策略来控制.默认的

订阅恢复策略是FixedSizeSubscriptionRecoveryPolicy.下面让我们逐一了解可用的各种不同的

订阅恢复策略.

 

5.6.2 The ActiveMQ subscription recovery policies

5.6.2 ActiveMQ的订阅恢复策略

 

There are a number of different policies that allow for fine-tuning the duration and

type of messages that are cached for nondurable topic consumers. Each policy type is

explained here.

 

ActiveMQ提供多种策略用来调优持久化消息以及缓存非持久化主题消费者的消息.下面分别说明这些

策略.

 

THE ACTIVEMQ FIXED SIZE SUBSCRIPTION RECOVERY POLICY

ACTIVEMQ固定尺寸订阅恢复策略

 

This policy limits the number of messages cached for the topic based on the amount

of memory they use. This is the default subscription recovery policy in ActiveMQ. You

can choose to have the cache limit applied to all topics, or on a topic-by-topic basis.

The properties available are shown in table 5.6.

 

这种策略根据使用的内存大小对每个主题的可缓存消息数量做了限制.这也是ActiveMQ默认的订阅恢复策略.

你可以为所有主题设置一种缓存策略,或者给每一个主题单独设置.表5.6是配置该策略时可以使用的参数.

 

Table 5.6 Configuration properties for a fixed size subscription recovery policy

表5.6 固定尺寸订阅恢复策略可配置属性

 

Property name       Default value       Description

属性名称            默认值               描述

 

maximumSize         6553600         The memory size in bytes for this cache

maximumSize         6553600         缓存可用的内存大小(单位:byte) 

 

useSharedBuffer     true            If true, the amount of memory allocated will be used across all topics

useSharedBuffer     true            如果设置为true,maximumSize设置的内存大小为所有主题的缓存总额

                                    (而不是每个主题缓存都可以达到maximumSize设置的大小)                                            

 

THE ACTIVEMQ FIXED COUNT SUBSCRIPTION RECOVERY POLICY

固定数量策略

 

This policy limits the number of messages cached by the topic based on a static count.

Only one property is available, as listed in table 5.7.

该策略根据主题中消息的数量来限制消息缓存.只有一个属性可设置,如表5.7所示

 

Table 5.7 Configuration properties for a fixed count subscription recovery policy

固定数量策略配置参数列表

 

Property name       Default value       Description

属性名称            默认值               描述

maximumSize         100               The number of messages allowed in the topics cache

maximumSize         100               每个主题中可缓存的消息数量最大值

 

THE ACTIVEMQ QUERY-BASED SUBSCRIPTION RECOVERY POLICY

基于查询的策略

 

This policy limits the number of messages cached based on a JMS property selector that’s

applied to each message. Only one property is available, as shown in table 5.8.

该策略根据应用到每个消息的JMS属性选择器来限制缓存消息数量.只有一个可设置参数,如表5.8所示:

 

Table 5.8 Configuration properties for a query-based subscription recovery policy

表5.8 基于查询的策略配置参数列表

 

Property name       Default value       Description

属性名称            默认值               描述

query               null                Caches only messages that match the query

query               null                当消息符合设置选择器时,缓存消息.

 

THE ACTIVEMQ TIMED SUBSCRIPTION RECOVERY POLICY

时间策略

 

This policy limits the number of messages cached by the topic based on an expiration

time that’s applied to each message. Note that the expiration time on a message is

independent from the timeToLive that’s set by the MessageProducer. The configuration

properties for a timed subscription policy are shown in table 5.9.

该策略根据消息的过期时间来缓存主题中的消息.注意,消息的过期时间是独立的,该过期时间由消息

生产者设置消息的timeToLive参数决定.可配置属性如表5.9所示.

 

Table 5.9 Configuration properties for a timed subscription recovery policy

表5.9时间策略配置参数列表

 

Property name       Default value       Description

属性名称            默认值               描述

recoverDuration     60000                 The time in milliseconds to keep messages in the cache

recoverDuration     60000                 消息的缓存时间(毫秒)

 

THE ACTIVEMQ LAST IMAGE SUBSCRIPTION RECOVERY POLICY

最终映像策略

 

This policy holds only the last message sent to a topic. It can be useful for real-time

pricing information—where a price per topic is used, you might only want the last

price that’s sent to that topic. There are no configuration properties for this policy.

该策略仅缓存发送到主题的最后一个消息.在实时的价格信息中,每个主题就是一个价格信息,这个策略很有用,

因为你可能仅仅关注最后一个发送给主题的价格信息.该策略没有配置属性.

 

THE ACTIVEMQ NO SUBSCRIPTION RECOVERY POLICY

无缓存策略

This policy disables message caching for topics. There are no properties to configure

for this policy.

该策略禁止缓存主题消息.该策略也没有配置属性.

 

5.6.3 Configuring the subscription recovery policy

5.6.3 配置订阅恢复策略

 

You can configure the subscriptionRecoveryPolicy for either individual topics, or

you can use wildcards, in the ActiveMQ broker configuration. An example configuration

is shown here:

在ActiveMQ代理的配置中,可以为每个独立的主题配置subscriptionRecoveryPolicy,或者也可以使用通配符

为多个主题配置策略.下面是配置代码样例:

 

<?xml version="1.0" encoding="UTF-8"?>

<beans>

  <broker brokerName="test-broker" persistent="true" useShutdownHook="false"

  deleteAllMessagesOnStartup="true"

  xmlns="http://activemq.apache.org/schema/core">

    <transportConnectors>

      <transportConnector uri="tcp://localhost:61635"/>

    </transportConnectors>

    

    <destinationPolicy>

      <policyMap>

        <policyEntries>

        

          <policyEntry topic="Topic.FixedSizedSubs.>">

            <subscriptionRecoveryPolicy>

              <fixedSizeSubscriptionRecoveryPolicy maximumSize="2000000" useSharedBuffer="false"/>

            </subscriptionRecoveryPolicy>

          </policyEntry>

          

          <policyEntry topic="Topic.LastImageSubs.>">

            <subscriptionRecoveryPolicy>

              <lastImageSubscriptionRecoveryPolicy/>

            </subscriptionRecoveryPolicy>

          </policyEntry>

          

          <policyEntry topic="Topic.NoSubs.>">

            <subscriptionRecoveryPolicy>

              <noSubscriptionRecoveryPolicy/>

            </subscriptionRecoveryPolicy>

          </policyEntry>

        

          <policyEntry topic="Topic.TimedSubs.>">

            <subscriptionRecoveryPolicy>

              <timedSubscriptionRecoveryPolicy recoverDuration="25000"/>

            </subscriptionRecoveryPolicy>

          </policyEntry>

        </policyEntries>

      </policyMap>

    </destinationPolicy>

  </broker>

</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值