Zamanlanmış mesajlar, anında iletilmek yerine belirtilen bir zaman damgasında tüketicilere teslim edilir. Mesaj üretimi ile tüketimi arasında bir süre beklenmesi gereken veya mesajlarla tetiklenen planlı görevlerin çalıştırıldığı senaryolarda (örneğin, 30 dakika sonra ödenmemiş bir siparişi iptal etme veya periyodik temizlik görevini başlatma) bu mesajları kullanın.
Aşağıdaki .NET örnek kodu, TCP istemci SDK'sı ile zamanlanmış mesajların nasıl gönderileceğini gösterir. Zamanlanmış mesajlar ile gecikmeli mesajlar arasındaki farklar hakkında bilgi edinmek için bkz. Zamanlanmış mesajlar ve gecikmeli mesajlar.
Desteklenen bölgeler
Zamanlanmış mesajlar aşağıdaki bölgelerde kullanılabilir:
İnternet
China (Hangzhou)
China (Beijing)
China (Shanghai)
China (Shenzhen)
Çalışma şekli
Üretici, mesaja teslimat zaman damgası ayarlamak için
setStartDeliverTimeyöntemini çağırır.Broker, belirtilen zaman damgasına kadar mesajı bekletir.
Teslimat zamanı geldiğinde, mesaj tüketiciler tarafından alınabilir hale gelir.
Teslimat zamanı, milisaniye düzeyinde bir Unix zaman damgasıdır (bağıl gecikme değil, mutlak saat).
Ön koşullar
Başlamadan önce aşağıdakileri hazırlayın:
.NET SDK indirilmiş ve yüklenmiş olmalıdır
Hazırlanmış geliştirme ortamı mevcut olmalıdır
Konsolda oluşturulmuş bir ApsaraMQ for RocketMQ örneği, konusu ve tüketici grubu kaynakları bulunmalıdır
Alibaba Cloud hesabınız için bir AccessKey çifti tanımlanmış olmalıdır
Zamanlanmış mesaj gönderme
Aşağıdaki örnek, 3 saniye sonra teslim edilmek üzere zamanlanmış bir mesaj gönderir. Daha fazla örnek için ApsaraMQ for RocketMQ kod deposuna bakabilirsiniz.
Kodu çalıştırmadan önce yer tutucuları aşağıdaki değerlerle değiştirin:
| Yer Tutucu | Açıklama | Nereden Bulunur |
|---|---|---|
<your-group-id> |
Tüketici grubu kimliği | ApsaraMQ for RocketMQ konsolu |
<your-tcp-endpoint> |
TCP uç noktası | Instance Details sayfası, TCP Endpoint bölümü |
<your-topic> |
Konu adı | ApsaraMQ for RocketMQ konsolu |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using ons;
namespace ons
{
class onscsharp
{
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long CurrentTimeMillis()
{
return (long) (DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
static void Main(string[] args)
{
// Configure the producer.
ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
// Consumer group ID created in the ApsaraMQ for RocketMQ console.
factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, "<your-group-id>");
// TCP endpoint from the Instance Details page.
factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "<your-tcp-endpoint>");
// Topic created in the ApsaraMQ for RocketMQ console.
factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, "<your-topic>");
// Message content.
factoryInfo.setFactoryProperty(ONSFactoryProperty.MsgContent, "scheduled_msg_body");
// Load AccessKey credentials from environment variables.
// Make sure ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Create and start the producer. Call start() only once before sending messages.
Producer pProducer = ONSFactory.getInstance().createProducer(factoryInfo);
pProducer.start();
Message msg = new Message(
factoryInfo.getPublishTopics(), // Topic
"TagA", // Message tag for filtering
factoryInfo.getMessageContent() // Message body
);
// Set a business key for message tracing in the console.
// Optional -- messages send and receive correctly without a key.
msg.setKey("ORDERID_100");
// Set the delivery time to 3 seconds from now (millisecond Unix timestamp).
long deliverTime = CurrentTimeMillis() + 3000;
msg.setStartDeliverTime(deliverTime);
// Send the message. A successful call returns a SendResultONS object.
try
{
SendResultONS sendResult = pProducer.send(msg);
}
catch(ONSClientException e)
{
// Handle send failures (network issues, invalid parameters, etc.).
}
// Shut down the producer before exiting to avoid memory leaks.
pProducer.shutdown();
}
}
}
Zamanlanmış mesajlara abone olma
Zamanlanmış mesajlar için özel tüketici yapılandırması gerekmez. Teslimat zamanı geldiğinde, mesaj diğer tüm mesajlar gibi konuda görünür. Normal mesajlar için kullandığınız tüketici kodunun aynısını kullanın.
Örnek kod için bkz. Mesajlara abone olma.
Kullanım notları
Zamanlanmış mesajlarda kullanılan terimler ve dikkate almanız gereken hususlar hakkında bilgi almak için bkz. Zamanlanmış mesajlar ve gecikmeli mesajlar.