Skip to content

Commit

Permalink
[ISSUE 4346] Fix start error and some code optimization. (apache#4347)
Browse files Browse the repository at this point in the history
* fix start error and some code optimization.

* fix code style
  • Loading branch information
Alonexc authored Aug 11, 2023
1 parent d3d9d4f commit dec34f3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.eventmesh.grpc.sub.app.controller;

import org.apache.eventmesh.common.protocol.http.common.ProtocolKey;
import org.apache.eventmesh.common.utils.JsonUtils;
import org.apache.eventmesh.grpc.sub.app.service.SubService;

Expand All @@ -43,9 +42,7 @@ public class SubController {

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String subTest(final HttpServletRequest request) {
final String protocolType = request.getHeader(ProtocolKey.PROTOCOL_TYPE);
final String content = request.getParameter("content");

if (log.isInfoEnabled()) {
log.info("=======receive message======= {}", content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import static org.apache.eventmesh.common.ExampleConstants.IDC;
import static org.apache.eventmesh.common.ExampleConstants.SERVER_PORT;
import static org.apache.eventmesh.common.ExampleConstants.SUB_SYS;
import static org.apache.eventmesh.util.Utils.getURL;

import org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig;
import org.apache.eventmesh.client.grpc.consumer.EventMeshGrpcConsumer;
import org.apache.eventmesh.common.ExampleConstants;
import org.apache.eventmesh.common.protocol.SubscriptionItem;
import org.apache.eventmesh.common.protocol.SubscriptionMode;
import org.apache.eventmesh.common.protocol.SubscriptionType;
import org.apache.eventmesh.common.utils.IPUtils;
import org.apache.eventmesh.grpc.pub.eventmeshmessage.AsyncPublishInstance;
import org.apache.eventmesh.util.Utils;

Expand Down Expand Up @@ -62,11 +62,10 @@ public class SubService implements InitializingBean {
}
}

private final String localIp = IPUtils.getLocalAddress();
private final String localPort = properties.getProperty(SERVER_PORT);
private final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
private final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);
private final String url = "http://" + localIp + ":" + localPort + "/sub/test";
private final String url = getURL(localPort, "/sub/test");

// CountDownLatch size is the same as messageSize in AsyncPublishInstance.java (Publisher)
private final CountDownLatch countDownLatch = new CountDownLatch(AsyncPublishInstance.MESSAGE_SIZE);
Expand Down Expand Up @@ -114,7 +113,7 @@ public void afterPropertiesSet() throws Exception {
@PreDestroy
public void cleanup() {
if (log.isInfoEnabled()) {
log.info("start destory ....");
log.info("start destroy....");
}

try {
Expand All @@ -129,7 +128,7 @@ public void cleanup() {
}

if (log.isInfoEnabled()) {
log.info("end destory.");
log.info("end destroy....");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.apache.eventmesh.common.ExampleConstants.IDC;
import static org.apache.eventmesh.common.ExampleConstants.SERVER_PORT;
import static org.apache.eventmesh.common.ExampleConstants.SUB_SYS;
import static org.apache.eventmesh.util.Utils.getURL;

import org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig;
import org.apache.eventmesh.client.http.consumer.EventMeshHttpConsumer;
Expand Down Expand Up @@ -66,11 +67,10 @@ public class SubService implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
properties = Utils.readPropertiesFile(ExampleConstants.CONFIG_FILE_NAME);
final String localIP = IPUtils.getLocalAddress();
final String localPort = properties.getProperty(SERVER_PORT);
final String eventmeshIP = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventmeshHttpPort = properties.getProperty(ExampleConstants.EVENTMESH_HTTP_PORT);
final String testURL = "http://" + localIP + ":" + localPort + "/sub/test";
final String testURL = getURL(localPort, "/sub/test");

final String eventMeshIPPort = eventmeshIP + ":" + eventmeshHttpPort;
final EventMeshHttpClientConfig eventMeshClientConfig = EventMeshHttpClientConfig.builder()
Expand Down Expand Up @@ -103,7 +103,7 @@ public void afterPropertiesSet() throws Exception {
@PreDestroy
public void cleanup() {
if (log.isInfoEnabled()) {
log.info("start destory ....");
log.info("start destroy....");
}

try {
Expand All @@ -121,7 +121,7 @@ public void cleanup() {
}

if (log.isInfoEnabled()) {
log.info("end destory.");
log.info("end destroy....");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.eventmesh.util;

import org.apache.eventmesh.common.ExampleConstants;
import org.apache.eventmesh.common.utils.IPUtils;

import org.apache.commons.lang3.SystemUtils;

Expand Down Expand Up @@ -92,4 +93,13 @@ public static Properties readPropertiesFile(final String fileName) throws IOExce
}
}

/**
* @param port server port
* @param path path
* @return url
*/
public static String getURL(String port, String path) {
return "http://" + IPUtils.getLocalAddress() + ":" + port + path;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private CloudEvent deserializeHttpProtocol(String requestCode,
org.apache.eventmesh.common.protocol.http.header.Header header,
Body body) throws ProtocolHandleException {

switch (RequestCode.valueOf(requestCode)) {
switch (RequestCode.get(Integer.parseInt(requestCode))) {
case MSG_BATCH_SEND:
return SendMessageBatchProtocolResolver.buildEvent(header, body);
case MSG_BATCH_SEND_V2:
Expand All @@ -98,7 +98,6 @@ private CloudEvent deserializeHttpProtocol(String requestCode,
default:
throw new ProtocolHandleException(String.format("unsupported requestCode: %s", requestCode));
}

}

@Override
Expand Down

0 comments on commit dec34f3

Please sign in to comment.