ApsaraMQ for RocketMQ'daki normal mesajların özel teslimat semantiği yoktur; zamanlanmış, gecikmeli, sıralı ve işlemli mesajlardan farklıdır.
Her konu yalnızca bir mesaj türünü destekler. Normal mesajlar için oluşturulan bir konuya diğer mesaj türleri gönderilemez veya bu türlerden alınamaz.
Aşağıdaki bölümlerde, C/C++ için TCP istemci SDK'sı kullanılarak normal mesajların nasıl gönderileceği ve alınacağı adım adım açıklanmaktadır.
Ön koşullar
Başlamadan önce şunları yaptığınızdan emin olun:
Gerekli kaynakları oluşturun -- ApsaraMQ for RocketMQ konsolunda bir örnek, bir konu ve bir tüketici grubu
Normal mesajları gönderme
Aşağıdaki örnek bir üretici oluşturur, aracıya bağlanır ve tek bir normal mesaj gönderir.
Kodda yer alan yer tutucuları gerçek değerlerinizle değiştirin:
| Yer Tutucu | Açıklama | Nerede Bulunur |
|---|---|---|
<your-group-id> |
Üretici grup kimliği | ApsaraMQ for RocketMQ konsolu |
<your-tcp-endpoint> |
TCP uç noktası URL'si | Instance Details sayfasındaki TCP Endpoint bölümü |
<your-topic> |
Konu adı | ApsaraMQ for RocketMQ konsolu |
<your-message-content> |
Mesaj gövdesi içeriği | Uygulamanız tarafından tanımlanır |
#include "ONSFactory.h"
#include "ONSClientException.h"
using namespace ons;
int main()
{
// Configure producer properties
ONSFactoryProperty factoryInfo;
factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, "<your-group-id>");
factoryInfo.setFactoryProperty(ONSFactoryProperty::NAMESRV_ADDR, "<your-tcp-endpoint>");
factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics, "<your-topic>");
factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "<your-message-content>");
// Load 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() exactly once before sending any messages.
Producer *pProducer = ONSFactory::getInstance()->createProducer(factoryInfo);
pProducer->start();
// Build the message
Message msg(
factoryInfo.getPublishTopics(), // Topic (must be a normal-message topic)
"TagA", // Tag -- consumers use tags to filter messages
factoryInfo.getMessageContent() // Body -- cannot be empty
);
// Set a message key for traceability.
// Keys must be globally unique whenever possible (for example, an order ID).
// Use the key to look up messages in the ApsaraMQ for RocketMQ console.
msg.setKey("ORDERID_100");
// Send the message. If no exception is thrown, the message is sent.
try
{
SendResultONS sendResult = pProducer->send(msg);
}
catch(ONSClientException & e)
{
// Handle the send failure (log, retry, alert, etc.)
}
// Shut down the producer before your application exits to prevent issues such as memory leaks.
pProducer->shutdown();
return 0;
}
Mesaj özellikleri
| Özellik | Ayrıntılar |
|---|---|
| Tag | Her mesajda bulunan ikincil bir sınıflandırıcıdır. Tüketiciler etiketleri kullanarak broker tarafında mesajları filtreler; böylece yalnızca ilgili mesajlar teslim edilir. |
| Key | Mümkün olduğunca genel olarak benzersiz olması gereken bir iş tanımlayıcısıdır (örneğin, sipariş kimliği). ApsaraMQ for RocketMQ konsolunda belirli mesajları sorgulamak için kullanılır. Anahtar ayarlamak isteğe bağlıdır; mesajlar anahtar olmadan da gönderilebilir ve alınabilir. |
| Body | Mesaj yüküdür. ApsaraMQ for RocketMQ gövdeyi işlemez veya ayrıştırmaz. Üretici ve tüketici, serileştirme ve seri hale getirme formatı konusunda anlaşmalıdır. Gövde boş olamaz. |
Üretici yaşam döngüsü
| Aşama | Yöntem | Notlar |
|---|---|---|
| Başlatma | pProducer->start() |
Herhangi bir mesaj göndermeden önce bir kez çağırın. |
| Gönderme | pProducer->send(msg) |
Özel durum oluşmazsa mesaj gönderilir. |
| Kapatma | pProducer->shutdown() |
Uygulama çıkmadan önce çağırın. Bu adımı atlamak bellek sızıntısı gibi sorunlara neden olabilir. |
Normal mesajlara abonelik
C/C++ için TCP istemci SDK'sını kullanan tüketici kurulumu ve örnek kod için bkz. Mesajlara abone olma.