Help Center/
Distributed Message Service/
Developer Guide/
Standard Queue Developer Guide/
Code of the Sample Project
Updated on 2023-11-30 GMT+08:00
Code of the Sample Project
The DMS TCP sample project contains two examples:
DmsProducerDemo: demonstrates the message production process. DmsConsumerDemo: demonstrates the message consumption and acknowledgment processes.
Producing Messages
- Create and start DmsProducer.
DmsProducer producer = new DmsProducerImpl(); producer.start();
- Construct the content of the message to be sent.
List<DmsMessage> messages = new ArrayList<>(); final int messageNum = 10; for (int i = 0; i < messageNum; i++) { DmsMessage record = new DmsMessage(); record.setBody(("Hello DMS syn produce: " + i).getBytes()); messages.add(record); }
- Send the message.
try { List<DmsProduceResult> result = producer.produce(queueId, messages); result.forEach(r -> { System.out.println(r.getState()); }); } catch (Throwable t) { t.printStackTrace(); }
- Stop message production.
producer.stop();
Consuming Messages
- Create and start a consumer instance.
DmsConsumer consumer = new DmsConsumerImpl(); consumer.start();
- Consume messages.
List<DmsConsumeResult> records = consumer.consume(queueId, groupId);
- Construct a request for acknowledging the message consumption.
DmsCommitRequest commitRequest = new DmsCommitRequest(); List<DmsCommitItem> messages = new ArrayList<>(); for (DmsConsumeResult record : records) { System.out.println(record.getHandler()); DmsCommitItem commitItem = new DmsCommitItem(); //The corresponding message handler needs to be filled in during acknowledgment. commitItem.setHandler(record.getHandler()); //Set the message handling status to true or false. commitItem.setStatus(true); messages.add(commitItem); } //Multiple messages can be acknowledged at a time. commitRequest.setMessage(messages);
- Acknowledge the message consumption.
DmsCommitResult commitResult = consumer.commit(queueId, groupId, commitRequest);
- Stop message consumption.
consumer.stop();
Parent topic: Standard Queue Developer Guide
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot