• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. JAVA
  6. DMS Java SDK Demo

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.

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.

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:

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:

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);
}
}