Improve RocketMQ integration example (#1757)
- The demo was unable to run and stop because of missing namesrv configuration, and now fixed.
This commit is contained in:
parent
bf83f058c8
commit
fe59485fa9
|
|
@ -19,6 +19,7 @@ public final class Constants {
|
||||||
|
|
||||||
public static final String TEST_GROUP_NAME = "sentinel-group";
|
public static final String TEST_GROUP_NAME = "sentinel-group";
|
||||||
public static final String TEST_TOPIC_NAME = "SentinelTopicTest";
|
public static final String TEST_TOPIC_NAME = "SentinelTopicTest";
|
||||||
|
public static final String TEST_NAMESRV_ADDR = "127.0.0.1:9876";
|
||||||
|
|
||||||
private Constants() {}
|
private Constants() {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ package com.alibaba.csp.sentinel.demo.rocketmq;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
|
@ -55,10 +56,16 @@ public class PullConsumerDemo {
|
||||||
initFlowControlRule();
|
initFlowControlRule();
|
||||||
|
|
||||||
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(Constants.TEST_GROUP_NAME);
|
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(Constants.TEST_GROUP_NAME);
|
||||||
|
consumer.setNamesrvAddr(Constants.TEST_NAMESRV_ADDR);
|
||||||
consumer.start();
|
consumer.start();
|
||||||
|
|
||||||
Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(Constants.TEST_TOPIC_NAME);
|
Set<MessageQueue> mqs = new HashSet<>();
|
||||||
|
try {
|
||||||
|
mqs = consumer.fetchSubscribeMessageQueues(Constants.TEST_TOPIC_NAME);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
for (MessageQueue mq : mqs) {
|
for (MessageQueue mq : mqs) {
|
||||||
System.out.printf("Consuming messages from the queue: %s%n", mq);
|
System.out.printf("Consuming messages from the queue: %s%n", mq);
|
||||||
SINGLE_MQ:
|
SINGLE_MQ:
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ public class SyncProducer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
// Instantiate with a producer group name.
|
// Instantiate with a producer group name.
|
||||||
DefaultMQProducer producer = new
|
DefaultMQProducer producer = new DefaultMQProducer(Constants.TEST_GROUP_NAME);
|
||||||
DefaultMQProducer(Constants.TEST_GROUP_NAME);
|
producer.setNamesrvAddr(Constants.TEST_NAMESRV_ADDR);
|
||||||
// Launch the instance.
|
// Launch the instance.
|
||||||
producer.start();
|
producer.start();
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
|
@ -33,9 +33,14 @@ public class SyncProducer {
|
||||||
Message msg = new Message(Constants.TEST_TOPIC_NAME, "TagA",
|
Message msg = new Message(Constants.TEST_TOPIC_NAME, "TagA",
|
||||||
("Hello RocketMQ From Sentinel " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
|
("Hello RocketMQ From Sentinel " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
|
||||||
);
|
);
|
||||||
// Call send message to deliver message to one of brokers.
|
|
||||||
SendResult sendResult = producer.send(msg);
|
try {
|
||||||
System.out.printf("%s%n", sendResult);
|
// Call send message to deliver message to one of brokers.
|
||||||
|
SendResult sendResult = producer.send(msg);
|
||||||
|
System.out.printf("%s%n", sendResult);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Shut down once the producer instance is not longer in use.
|
// Shut down once the producer instance is not longer in use.
|
||||||
producer.shutdown();
|
producer.shutdown();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue