12.5. 调度延迟工作
AMQ C++ 能够延迟后执行代码。您可以使用它来实现应用程序中的基于时间的行为,如定期调度的工作或超时。
要延迟固定时间的工作,请使用 schedule 方法设置延迟并注册定义工作的功能。
示例:在延迟后发送消息
void on_sender_open(proton::sender& snd) override {
proton::duration interval {5 * proton::duration::SECOND};
snd.work_queue().schedule(interval, [=] { send(snd); });
}
void send(proton::sender snd) {
if (snd.credit() > 0) {
proton::message msg {"hello"};
snd.send(msg);
}
}
本例在发件人的工作队列中使用 schedule 方法,以将其建立为工作的执行上下文。