TTL(生存时间)如何应用于命名空间?

时间:2019-03-20 15:32:27

标签: apache-pulsar

Apache Pulsar具有TTL功能,如官方文档的Message retention and expiry主题所述。但是,我无法确定该检查在多长时间内设置一次。使用具有自定义名称空间的标准bin/pulsar standalone命令和配置为5秒bin/pulsar-admin namespaces set-message-ttl public/ttl-test --messageTTL 5的ttl。

我可以看到消息仅在设置的时间间隔后过期,并且以下日志消息被打印到控制台:

  

15:11:59.337 [pulsar-msg-expiry-monitor-52-1]信息   org.apache.pulsar.broker.service.persistent.PersistentMessageExpiryMonitor   -[persistent:// public / ttl-test / my-topic] [spark-shell]启动消息到期检查,ttl = 5秒

我的问题的关键是:如何提高检查邮件是否已超过TTL的速率?

2 个答案:

答案 0 :(得分:2)

代理中的配置messageExpiryCheckIntervalInMinutes确定检查名称空间主题中是否有过期消息的频率。

根据configuration

上的官方文档

答案 1 :(得分:0)

使用set-message-ttl命令并指定名称空间名称(对于永久主题,默认为public / default)和时间。

bin/pulsar-admin namespaces set-message-ttl public/default --messageTTL 120

生产者和消费者实现ttl(python客户端)的示例代码

import pulsar
client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer('my-topic-reader1')

producer.send(('Hello-Pulsar1').encode('utf-8'))
producer.send(('Hello-Pulsar2').encode('utf-8'))
producer.send(('Hello-Pulsar3').encode('utf-8'))

producer.close()
client.close()

您可以使用send方法发送多条消息。这两个课程的主题名称应该相同。

import pulsar
client = pulsar.Client('pulsar://localhost:6650')
consumer = client.subscribe("my-topic-reader1", "my-subscription")

//receive all the messages.whatever we publish
msg = consumer.receive()
print("Received message '{}' id='{}'".format(msg.data(), msg.message_id()))

//Here we are not acknowledge all the messages.

//close the consumer and client
consumer.close()
client.close()

在120秒内,我们再次打开客户端和消费者,并尝试读取未发布的相同消息。再次,我们关闭客户端和消费者。

稍后(120秒后),我们将再次打开客户端和消费者,然后尝试接收消息。但是它不应该来。在这种情况下,您将获得生存时间。