DMS Java SDK Demo
Distributed Message Service (DMS) is a message middleware service based on distributed, high-availability clustering technology. It provides reliable, scalable, fully managed queues for storing messages. DMS enables cloud applications to decouple from each other, achieving high cost-effectiveness.
Creating a Queue
You can create a queue using OpenStack4j based on the following code. After the queue is created, messages will be sent to this queue.
1 2 3 4 |
String name = randomName();
String description = "sdk-unittest"
Queue queue = null;
queue = osclient.messageQueue().queue().create(name, description);
|
Creating a Consumer Group
You can create a consumer group using OpenStack4j based on the following code. After the consumer group is created, it can consume messages in the queue.
1 2 3 4 |
List<ConsumerGroup> groups = null;
List<String> groupNames = Lists.newArrayList("consumer-group-1", "consumer-group-2");
queueId queueID = queue.getId();
groups = osclient.messageQueue().consumerGroups().create(queueID, groupNames);
|
Producing Messages
You can produce messages using OpenStack4j based on the following code:
1 2 3 4 5 6 7 |
public void testProduceMessage() {
HashMap<String, Object> attributes1 = Maps.newHashMap();
attributes1.put("attr1", 1);
attributes1.put("attr2", false);
QueueMessage message = QueueMessage.builder().body("sdk-unittests").attributes(attributes1).build();
ActionResponse produce = osclient.messageQueue().messages().produce(queue.getId(), message);
}
|
Consume Messages
You can consume messages using OpenStack4j based on the following code:
1 2 3 4 5 6 7 8 |
public void testConsumeMessages() {
ConsumerGroup consumerGroup1 = groups.get(0);
List<QueueMessageWithHandler> all = Lists.newArrayList();
for (int i = 0; i < 3; i++) {
List<QueueMessageWithHandler> temp = osclient.messageQueue().messages().consume(queue.getId(), consumerGroup1.getId(), 5, 10);
all.addAll(temp);
}
}
|
Last Article: Anti-DDoS Java SDK Demo
Next Article: MRS Java SDK Demo
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.