Skip to content

Commit

Permalink
[ISSUE #806] code optimization and delete invalid code in eventmesh-e… (
Browse files Browse the repository at this point in the history
#807)

* [ISSUE #806] code optimization and delete invalid code in eventmesh-examples module

* use ExampleConstants to hold constants in examples
* delete invalid code

* [ISSUE #806] code optimization and delete invalid code in eventmesh-examples module

Motivation
Code style consistency in log msg

Co-authored-by: fengyongshe <fengyongshe@cmss.chinamobile.com>

close #806
  • Loading branch information
fengyongshe authored Mar 10, 2022
1 parent e5bf731 commit 8fe412c
Show file tree
Hide file tree
Showing 33 changed files with 113 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@
public class ExampleConstants {

public static final String CONFIG_FILE_NAME = "application.properties";
public static final String CLOUDEVENT_CONTENT_TYPE = "application/cloudevents+json";

public static final String EVENTMESH_IP = "eventmesh.ip";

public static final String EVENTMESH_HTTP_PORT = "eventmesh.http.port";

public static final String EVENTMESH_TCP_PORT = "eventmesh.tcp.port";

public static final String EVENTMESH_GRPC_PORT = "eventmesh.grpc.port";

public static final String DEFAULT_EVENTMESH_IP = "127.0.0.1";
public static final String DEFAULT_EVENTMESH_IP_PORT = "127.0.0.1:10105";

public static final String EVENTMESH_GRPC_ASYNC_TEST_TOPIC = "TEST-TOPIC-GRPC-ASYNC";
public static final String EVENTMESH_GRPC_RR_TEST_TOPIC = "TEST-TOPIC-GRPC-RR";
public static final String EVENTMESH_GRPC_BROADCAT_TEST_TOPIC = "TEST-TOPIC-GRPC-BROADCAST";
public static final String EVENTMESH_HTTP_ASYNC_TEST_TOPIC = "TEST-TOPIC-HTTP-ASYNC";
public static final String EVENTMESH_HTTP_SYNC_TEST_TOPIC = "TEST-TOPIC-HTTP-SYNC";
public static final String EVENTMESH_TCP_ASYNC_TEST_TOPIC = "TEST-TOPIC-TCP-ASYNC";
public static final String EVENTMESH_TCP_SYNC_TEST_TOPIC = "TEST-TOPIC-TCP-SYNC";
public static final String EVENTMESH_TCP_BROADCAST_TEST_TOPIC = "TEST-TOPIC-TCP-BROADCAST";

public static final String DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP = "EventMeshTest-producerGroup";
public static final String DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP = "EventMeshTest-consumerGroup";

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ public static void main(String[] args) throws Exception {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-ASYNC";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.producerGroup("EventMeshTest-producerGroup")
.producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

Expand All @@ -68,9 +66,9 @@ public static void main(String[] args) throws Exception {
for (int i = 0; i < 5; i++) {
CloudEvent event = CloudEventBuilder.v1()
.withId(UUID.randomUUID().toString())
.withSubject(topic)
.withSubject(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC)
.withSource(URI.create("/"))
.withDataContentType("application/cloudevents+json")
.withDataContentType(ExampleConstants.CLOUDEVENT_CONTENT_TYPE)
.withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME)
.withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8))
.withExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ public static void main(String[] args) throws Exception {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-ASYNC";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.producerGroup("EventMeshTest-producerGroup")
.producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

Expand All @@ -68,9 +66,9 @@ public static void main(String[] args) throws Exception {
for (int i = 0; i < messageSize; i++) {
CloudEvent event = CloudEventBuilder.v1()
.withId(UUID.randomUUID().toString())
.withSubject(topic)
.withSubject(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC)
.withSource(URI.create("/"))
.withDataContentType("application/cloudevents+json")
.withDataContentType(ExampleConstants.CLOUDEVENT_CONTENT_TYPE)
.withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME)
.withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8))
.withExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ public static void main(String[] args) throws Exception {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-RR";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.producerGroup("EventMeshTest-producerGroup")
.producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

Expand All @@ -68,9 +66,9 @@ public static void main(String[] args) throws Exception {
for (int i = 0; i < messageSize; i++) {
CloudEvent event = CloudEventBuilder.v1()
.withId(UUID.randomUUID().toString())
.withSubject(topic)
.withSubject(ExampleConstants.EVENTMESH_GRPC_RR_TEST_TOPIC)
.withSource(URI.create("/"))
.withDataContentType("application/cloudevents+json")
.withDataContentType(ExampleConstants.CLOUDEVENT_CONTENT_TYPE)
.withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME)
.withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8))
.withExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ public static void main(String[] args) throws Exception {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-BROADCAST";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.producerGroup("EventMeshTest-producerGroup")
.producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

Expand All @@ -63,7 +61,7 @@ public static void main(String[] args) throws Exception {
for (int i = 0; i < messageSize; i++) {
EventMeshMessage eventMeshMessage = EventMeshMessage.builder()
.content(JsonUtils.serialize(content))
.topic(topic)
.topic(ExampleConstants.EVENTMESH_GRPC_BROADCAT_TEST_TOPIC)
.uniqueId(RandomStringUtils.generateNum(30))
.bizSeqNo(RandomStringUtils.generateNum(30))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ public static void main(String[] args) throws Exception {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-ASYNC";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.producerGroup("EventMeshTest-producerGroup")
.producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

Expand All @@ -63,7 +61,7 @@ public static void main(String[] args) throws Exception {
for (int i = 0; i < messageSize; i++) {
EventMeshMessage eventMeshMessage = EventMeshMessage.builder()
.content(JsonUtils.serialize(content))
.topic(topic)
.topic(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC)
.uniqueId(RandomStringUtils.generateNum(30))
.bizSeqNo(RandomStringUtils.generateNum(30))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ public static void main(String[] args) throws Exception {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-ASYNC";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.producerGroup("EventMeshTest-producerGroup")
.producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

Expand All @@ -62,7 +60,7 @@ public static void main(String[] args) throws Exception {
List<EventMeshMessage> messageList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
EventMeshMessage message = EventMeshMessage.builder()
.topic(topic)
.topic(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC)
.content((JsonUtils.serialize(content)))
.uniqueId(RandomStringUtils.generateNum(30))
.bizSeqNo(RandomStringUtils.generateNum(30))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ public static void main(String[] args) throws Exception {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-RR";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.producerGroup("EventMeshTest-producerGroup")
.producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

Expand All @@ -64,7 +62,7 @@ public static void main(String[] args) throws Exception {
for (int i = 0; i < messageSize; i++) {
EventMeshMessage eventMeshMessage = EventMeshMessage.builder()
.content(JsonUtils.serialize(content))
.topic(topic)
.topic(ExampleConstants.EVENTMESH_GRPC_RR_TEST_TOPIC)
.uniqueId(RandomStringUtils.generateNum(30))
.bizSeqNo(RandomStringUtils.generateNum(30))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ public static void main(String[] args) throws InterruptedException {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-ASYNC";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.consumerGroup("EventMeshTest-consumerGroup")
.consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

org.apache.eventmesh.common.protocol.SubscriptionItem subscriptionItem = new SubscriptionItem();
subscriptionItem.setTopic(topic);
subscriptionItem.setTopic(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC);
subscriptionItem.setMode(SubscriptionMode.CLUSTERING);
subscriptionItem.setType(SubscriptionType.ASYNC);


EventMeshGrpcConsumer eventMeshGrpcConsumer = new EventMeshGrpcConsumer(eventMeshClientConfig);

eventMeshGrpcConsumer.init();
Expand All @@ -74,7 +71,7 @@ public static void main(String[] args) throws InterruptedException {

@Override
public Optional<CloudEvent> handle(CloudEvent msg) {
log.info("receive async msg====================={}", msg);
log.info("receive async msg: {}", msg);
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ public static void main(String[] args) throws InterruptedException {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-RR";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.consumerGroup("EventMeshTest-consumerGroup")
.consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

SubscriptionItem subscriptionItem = new SubscriptionItem();
subscriptionItem.setTopic(topic);
subscriptionItem.setTopic(ExampleConstants.EVENTMESH_GRPC_RR_TEST_TOPIC);
subscriptionItem.setMode(SubscriptionMode.CLUSTERING);
subscriptionItem.setType(SubscriptionType.SYNC);


EventMeshGrpcConsumer eventMeshGrpcConsumer = new EventMeshGrpcConsumer(eventMeshClientConfig);

eventMeshGrpcConsumer.init();
Expand All @@ -74,7 +71,7 @@ public static void main(String[] args) throws InterruptedException {

@Override
public Optional<CloudEvent> handle(CloudEvent msg) {
log.info("receive request-reply msg====================={}", msg);
log.info("receive request-reply msg: {}", msg);
if (msg != null) {
return Optional.of(msg);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ public static void main(String[] args) throws InterruptedException {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-ASYNC";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.consumerGroup("EventMeshTest-consumerGroup")
.consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

SubscriptionItem subscriptionItem = new SubscriptionItem();
subscriptionItem.setTopic(topic);
subscriptionItem.setTopic(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC);
subscriptionItem.setMode(SubscriptionMode.CLUSTERING);
subscriptionItem.setType(SubscriptionType.ASYNC);

Expand All @@ -72,7 +70,7 @@ public static void main(String[] args) throws InterruptedException {

@Override
public Optional<EventMeshMessage> handle(EventMeshMessage msg) {
log.info("receive async msg====================={}", msg);
log.info("receive async msg: {}", msg);
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ public static void main(String[] args) throws InterruptedException {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-BROADCAST";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.consumerGroup("EventMeshTest-consumerGroup")
.consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

SubscriptionItem subscriptionItem = new SubscriptionItem();
subscriptionItem.setTopic(topic);
subscriptionItem.setTopic(ExampleConstants.EVENTMESH_GRPC_BROADCAT_TEST_TOPIC);
subscriptionItem.setMode(SubscriptionMode.BROADCASTING);
subscriptionItem.setType(SubscriptionType.ASYNC);

Expand All @@ -72,7 +70,7 @@ public static void main(String[] args) throws InterruptedException {

@Override
public Optional<EventMeshMessage> handle(EventMeshMessage msg) {
log.info("receive async broadcast msg====================={}", msg);
log.info("receive async broadcast msg: {}", msg);
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ public static void main(String[] args) throws InterruptedException {
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);

final String topic = "TEST-TOPIC-GRPC-RR";

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.consumerGroup("EventMeshTest-consumerGroup")
.consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
.env("env").idc("idc")
.sys("1234").build();

SubscriptionItem subscriptionItem = new SubscriptionItem();
subscriptionItem.setTopic(topic);
subscriptionItem.setTopic(ExampleConstants.EVENTMESH_GRPC_RR_TEST_TOPIC);
subscriptionItem.setMode(SubscriptionMode.CLUSTERING);
subscriptionItem.setType(SubscriptionType.SYNC);

Expand All @@ -72,7 +70,7 @@ public static void main(String[] args) throws InterruptedException {

@Override
public Optional<EventMeshMessage> handle(EventMeshMessage msg) {
log.info("receive request-reply msg====================={}", msg);
log.info("receive request-reply msg: {}", msg);
if (msg != null) {
return Optional.of(msg);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public void afterPropertiesSet() throws Exception {
EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.consumerGroup("EventMeshTest-consumerGroup2")
.consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
.env(env).idc(idc)
.sys(subsys).build();

eventMeshGrpcConsumer = new EventMeshGrpcConsumer(eventMeshClientConfig);
eventMeshGrpcConsumer.init();

subscriptionItem.setTopic("TEST-TOPIC-GRPC-ASYNC");
subscriptionItem.setTopic(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC);
subscriptionItem.setMode(SubscriptionMode.CLUSTERING);
subscriptionItem.setType(SubscriptionType.ASYNC);

Expand Down Expand Up @@ -115,8 +115,8 @@ public void cleanup() {
* Count the message already consumed
*/
public void consumeMessage(String msg) {
logger.info("consume message {}", msg);
logger.info("consume message: {}", msg);
countDownLatch.countDown();
logger.info("remaining number of messages to be consumed {}", countDownLatch.getCount());
logger.info("remaining number of messages to be consumed: {}", countDownLatch.getCount());
}
}
Loading

0 comments on commit 8fe412c

Please sign in to comment.