Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #4631]Optimize the message body of the Java SDK's returned reply message #4632

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ProtocolKey {
public static final String GRPC_RESPONSE_TIME = "time";

public static final String SUB_MESSAGE_TYPE = "submessagetype";
public static final String SUB_REPLY_MESSAGE = "subscription_reply";

/**
* CloudEvents spec
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.eventmesh.common.protocol.grpc.common.EventMeshCloudEventUtils;
import org.apache.eventmesh.common.protocol.grpc.common.ProtocolKey;
import org.apache.eventmesh.common.protocol.grpc.common.StatusCode;
import org.apache.eventmesh.common.protocol.grpc.common.SubscriptionReply;
import org.apache.eventmesh.runtime.boot.EventMeshGrpcServer;
import org.apache.eventmesh.runtime.constants.EventMeshConstants;
import org.apache.eventmesh.runtime.core.protocol.grpc.processor.ReplyMessageProcessor;
Expand Down Expand Up @@ -87,7 +86,7 @@ public StreamObserver<CloudEvent> subscribeStream(StreamObserver<CloudEvent> res
public void onNext(CloudEvent subscription) {
final String subMessageType = Optional.ofNullable(subscription.getAttributesMap().get(ProtocolKey.SUB_MESSAGE_TYPE))
.orElse(CloudEventAttributeValue.newBuilder().build()).getCeString();
if (StringUtils.equals(subMessageType, SubscriptionReply.TYPE)) {
if (StringUtils.equals(subMessageType, ProtocolKey.SUB_REPLY_MESSAGE)) {
log.info("cmd={}|{}|client2eventMesh|from={}|to={}", "reply-to-server", EventMeshConstants.PROTOCOL_GRPC,
EventMeshCloudEventUtils.getIp(subscription), eventMeshGrpcServer.getEventMeshGrpcConfiguration().getEventMeshIp());
handleSubscribeReply(subscription, emitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@
import org.apache.eventmesh.common.protocol.grpc.cloudevents.ConsumerServiceGrpc.ConsumerServiceStub;
import org.apache.eventmesh.common.protocol.grpc.common.EventMeshCloudEventUtils;
import org.apache.eventmesh.common.protocol.grpc.common.ProtocolKey;
import org.apache.eventmesh.common.protocol.grpc.common.SubscriptionReply;
import org.apache.eventmesh.common.utils.JsonUtils;
import org.apache.eventmesh.common.utils.LogUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -118,26 +114,13 @@ public void onCompleted() {
private CloudEvent buildReplyMessage(final CloudEvent reqMessage, final T replyMessage) {
final CloudEvent cloudEvent = EventMeshCloudEventBuilder.buildEventMeshCloudEvent(replyMessage,
clientConfig, listener.getProtocolType());
SubscriptionReply subscriptionReply = SubscriptionReply.builder().producerGroup(clientConfig.getConsumerGroup())
.topic(EventMeshCloudEventUtils.getSubject(cloudEvent))
.content(EventMeshCloudEventUtils.getDataContent(cloudEvent))
.seqNum(EventMeshCloudEventUtils.getSeqNum(cloudEvent))
.uniqueId(EventMeshCloudEventUtils.getUniqueId(cloudEvent))
.ttl(EventMeshCloudEventUtils.getTtl(cloudEvent)).build();

Map<String, String> prop = new HashMap<>();
Map<String, CloudEventAttributeValue> reqMessageMap = reqMessage.getAttributesMap();
reqMessageMap.entrySet().forEach(entry -> prop.put(entry.getKey(), entry.getValue().getCeString()));
Map<String, CloudEventAttributeValue> cloudEventMap = cloudEvent.getAttributesMap();
cloudEventMap.entrySet().forEach(entry -> prop.put(entry.getKey(), entry.getValue().getCeString()));
subscriptionReply.putAllProperties(prop);

return CloudEvent.newBuilder(cloudEvent).putAllAttributes(reqMessageMap)

return CloudEvent.newBuilder(cloudEvent).putAllAttributes(reqMessage.getAttributesMap()).putAllAttributes(cloudEvent.getAttributesMap())
.putAttributes(ProtocolKey.DATA_CONTENT_TYPE,
CloudEventAttributeValue.newBuilder().setCeString(EventMeshDataContentType.JSON.getCode()).build())
//Indicate that it is a subscription response
.putAttributes(ProtocolKey.SUB_MESSAGE_TYPE, CloudEventAttributeValue.newBuilder().setCeString(SubscriptionReply.TYPE).build())
.setTextData(JsonUtils.toJSONString(subscriptionReply)).build();
// Indicate that it is a subscription response
.putAttributes(ProtocolKey.SUB_MESSAGE_TYPE, CloudEventAttributeValue.newBuilder().setCeString(ProtocolKey.SUB_REPLY_MESSAGE).build())
.build();
}

@Override
Expand Down