All examples assume you included the header file
KawaiiMQ/kawaiiMQ.h
To use a message queue, you need to create a queue and a topic.
KawaiiMQ::Queue queue;
KawaiiMQ::Topic topic("topic1");
Then, you need to relate the queue with the topic.
KawaiiMQ::MessageQueueManager::Instance()->relate(topic, queue);
After that, you can create a producer and subscribe to the topic.
KawaiiMQ::Producer producer;
producer.subscribe(topic);
To make a message, use the makeMessage
function.
auto message = KawaiiMQ::makeMessage(0);
Then, you can publish the message.
producer.publishMessage(topic, message);
To consume the message, you need to create a consumer and fetch the message.
auto consumer = KawaiiMQ::Consumer({topic});
auto messages = consumer.fetchMessage();
The return value is a map of topics and messages. You can use the getMessage
function to get the message.
std::cout << getMessage<int>(messages[topic][0]) << std::endl;
You can of course only publish message on a single topic or consume messages from a single topic.
KawaiiMQ::Queue queue;
KawaiiMQ::Topic topic("topic1");
KawaiiMQ::MessageQueueManager::Instance()->relate(topic, queue);
KawaiiMQ::Producer producer;
producer.subscribe(topic);
auto message = KawaiiMQ::makeMessage(0);
producer.publishMessage(topic, message);
auto consumer = KawaiiMQ::Consumer({topic});
consumer.subscribe(topic);
auto messages = consumer.fetchSingleTopic(topic);
std::cout << getMessage<int>(messages[0]) << std::endl;
KawaiiMQ::Queue queue;
KawaiiMQ::Queue queue;
KawaiiMQ::Topic topic("topic1");
KawaiiMQ::MessageQueueManager::Instance()->relate(topic, queue);
KawaiiMQ::Queue queue;
KawaiiMQ::Topic topic("topic1");
KawaiiMQ::MessageQueueManager::Instance()->relate(topic, queue);
KawaiiMQ::Producer producer;
producer.subscribe(topic);
auto message = KawaiiMQ::makeMessage(0);
producer.publishMessage(topic, message);
KawaiiMQ::Queue queue;
KawaiiMQ::Topic topic("topic1");
KawaiiMQ::MessageQueueManager::Instance()->relate(topic, queue);
KawaiiMQ::Producer producer;
producer.subscribe(topic);
auto message = KawaiiMQ::makeMessage(0);
producer.publishMessage(topic, message);
auto consumer = KawaiiMQ::Consumer({topic});
auto messages = consumer.fetchMessage();
std::cout << getMessage<int>(messages[topic][0]) << std::endl;
The output will be 0
.
KawaiiMQ::Queue queue;
KawaiiMQ::Topic topic("topic1");
KawaiiMQ::MessageQueueManager::Instance()->relate(topic, queue);
KawaiiMQ::MessageQueueManager::Instance()->unrelate(topic, queue);
Note this method will wait till the queue is empty before unrelating the queue with the topic.
KawaiiMQ::Queue queue;
KawaiiMQ::Topic topic("topic1");
KawaiiMQ::MessageQueueManager::Instance()->relate(topic, queue);
KawaiiMQ::Producer producer;
auto consumer = KawaiiMQ::Consumer({topic});
producer.subscribe(topic);
consumer.unsubscribe(topic);
producer.unsubscribe(topic);