This guide is based on the current public API in client/include/MYMQ_C.h.
- Config files are loaded only at startup.
- Common client config files:
client/config/communication.iniclient/config/sys.iniclient/config/business.ini
#include "MYMQ_C.h"
#include "MYMQ_PublicCodes.h"Core types:
MYMQ_ProducerMYMQ_ConsumerMYMQ_Public::TopicPartitionMYMQ_Public::ClientErrorCodeMYMQ_Public::PushResponceMYMQ_Public::CommitAsyncResponce
// ack_level:
// 1 = wait for server ACK
// 0 = no ACK (callbacks will not fire)
MYMQ_Producer producer("producer-1", 1);producer.create_topic("topic_demo", 4);MYMQ_Public::TopicPartition tp("topic_demo", 0);
auto err = producer.push(tp, "k1", "v1");
if (err != MYMQ_Public::ClientErrorCode::Success) {
// handle error
}MYMQ_Public::TopicPartition tp("topic_demo", 0);
auto on_push = [](MYMQ_Public::PushResponce resp) {
// resp.tp / resp.errorcode / resp.offset
};
auto err = producer.push(tp, "k2", "v2", on_push);Notes:
- With
ack_level=0, the server does not send push ACK, so callback will not be called. - Typical return errors include
QUEUE_FULL,NOT_REGISTER,TIMEOUT.
producer.stop();MYMQ_Consumer consumer("consumer-1", 1);consumer.subscribe_topic("topic_demo");
consumer.join_group("group_demo");Optional checks:
bool in_group = consumer.get_is_ingroup();
auto assigned = consumer.get_assigned_partition();consumer.trigger_pull();std::vector<MYMQ_Public::ConsumerRecord> records;
auto err = consumer.pull(records, 5000); // timeout ms
if (err == MYMQ_Public::ClientErrorCode::Success) {
for (const auto& r : records) {
auto key = r.getKey();
auto val = r.getValue();
auto off = r.getOffset();
}
}Pull with latency output:
std::vector<MYMQ_Public::ConsumerRecord> records;
int64_t latency_us = 0;
auto err = consumer.pull(records, 5000, latency_us);Sync commit:
MYMQ_Public::TopicPartition tp("topic_demo", 0);
size_t next_offset_to_consume = 100;
auto err = consumer.commit_sync(tp, next_offset_to_consume);Async commit (no callback):
MYMQ_Public::TopicPartition tp("topic_demo", 0);
size_t next_offset_to_consume = 100;
auto err = consumer.commit_async(tp, next_offset_to_consume);Async commit (with callback):
MYMQ_Public::TopicPartition tp("topic_demo", 0);
size_t next_offset_to_consume = 100;
auto on_commit = [](MYMQ_Public::CommitAsyncResponce resp) {
// resp.groupid / resp.tp / resp.committed_offset / resp.error
};
auto err = consumer.commit_async(tp, next_offset_to_consume, on_commit);MYMQ_Public::TopicPartition tp("topic_demo", 0);
auto err = consumer.seek(tp, 0);consumer.set_pull_max_record_num_local(100000);
consumer.set_pull_fetch_min_bytes(1024 * 1024);Get local consumed position:
MYMQ_Public::TopicPartition tp("topic_demo", 0);
size_t pos = 0;
auto err = consumer.get_position_consumed(tp, pos);Leave group:
auto err = consumer.leave_group();- API calls return
MYMQ_Public::ClientErrorCode. - Use
MYMQ_Public::to_string(err)for readable logs. - Common values:
SuccessPULL_TIMEOUTEMPTY_RECORDNOT_IN_GROUPREACHED_MAX_FLYING_REQUESTQUEUE_FULLNETWORK_FATAL
client/examples/main.cppclient/examples/example_test_perf.cppclient/examples/example_test_seek.cppclient/examples/example_test_backpressure.cpp