From fee81c05a467f898170271559c627f805cac9eab Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Tue, 23 Nov 2021 18:27:50 +0800 Subject: [PATCH 01/13] update java sdk --- .../tcp/common/EventMeshTestUtils.java | 57 ++++--- .../http/producer/CloudEventProducer.java | 4 + .../producer/EventMeshMessageProducer.java | 9 ++ .../client/tcp/common/EventMeshCommon.java | 2 + .../client/tcp/common/MessageUtils.java | 27 +++- .../client/tcp/common/TcpClient.java | 3 +- .../cloudevent/CloudEventTCPPubClient.java | 142 +++++++++++++++-- .../cloudevent/CloudEventTCPSubClient.java | 150 ++++++++++++++++-- .../EventMeshMessageTCPPubClient.java | 10 +- .../EventMeshMessageTCPSubClient.java | 41 ++--- 10 files changed, 359 insertions(+), 86 deletions(-) diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java index 88f5016e9d..854eb5b58c 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java @@ -24,7 +24,6 @@ import java.util.concurrent.ThreadLocalRandom; -import org.apache.eventmesh.client.tcp.common.EventMeshCommon; import org.apache.eventmesh.common.protocol.tcp.Command; import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; import org.apache.eventmesh.common.protocol.tcp.Header; @@ -35,37 +34,37 @@ public class EventMeshTestUtils { private static final int seqLength = 10; public static UserAgent generateClient1() { - UserAgent user = new UserAgent(); - user.setEnv("test"); - user.setHost("127.0.0.1"); - user.setPassword(generateRandomString(8)); - user.setUsername("PU4283"); - user.setProducerGroup("EventmeshTest-ProducerGroup"); - user.setConsumerGroup("EventmeshTest-ConsumerGroup"); - user.setPath("/data/app/umg_proxy"); - user.setPort(8362); - user.setSubsystem("5023"); - user.setPid(32893); - user.setVersion("2.0.11"); - user.setIdc("FT"); - return user; + return UserAgent.builder() + .env("test") + .host("127.0.0.1") + .password(generateRandomString(8)) + .username("PU4283") + .producerGroup("EventmeshTest-ProducerGroup") + .consumerGroup("EventmeshTest-ConsumerGroup") + .path("/data/app/umg_proxy") + .port(8362) + .subsystem("5023") + .pid(32893) + .version("2.0.11") + .idc("FT") + .build(); } public static UserAgent generateClient2() { - UserAgent user = new UserAgent(); - user.setEnv("test"); - user.setHost("127.0.0.1"); - user.setPassword(generateRandomString(8)); - user.setUsername("PU4283"); - user.setConsumerGroup("EventmeshTest-ConsumerGroup"); - user.setProducerGroup("EventmeshTest-ProducerGroup"); - user.setPath("/data/app/umg_proxy"); - user.setPort(9362); - user.setSubsystem("5017"); - user.setPid(42893); - user.setVersion("2.0.11"); - user.setIdc("FT"); - return user; + return UserAgent.builder() + .env("test") + .host("127.0.0.1") + .password(generateRandomString(8)) + .username("PU4283") + .producerGroup("EventmeshTest-ProducerGroup") + .consumerGroup("EventmeshTest-ConsumerGroup") + .path("/data/app/umg_proxy") + .port(9362) + .subsystem("5017") + .pid(42893) + .version("2.0.11") + .idc("FT") + .build(); } public static Package syncRR() { diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java index 6d6ff2f63d..49905ad1ac 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java @@ -29,6 +29,8 @@ class CloudEventProducer extends AbstractHttpClient implements EventMeshProtocol private static final String PROTOCOL_TYPE = "cloudevents"; + private static final String PROTOCOL_DESC = "http"; + public CloudEventProducer(EventMeshHttpClientConfig eventMeshHttpClientConfig) throws EventMeshException { super(eventMeshHttpClientConfig); } @@ -105,6 +107,8 @@ private RequestParam buildCommonPostParam(CloudEvent cloudEvent) { .addHeader(ProtocolKey.ClientInstanceKey.PASSWD, eventMeshHttpClientConfig.getPassword()) .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) .addHeader(ProtocolKey.PROTOCOL_TYPE, PROTOCOL_TYPE) + .addHeader(ProtocolKey.PROTOCOL_DESC, PROTOCOL_DESC) + .addHeader(ProtocolKey.PROTOCOL_VERSION, cloudEvent.getSpecVersion().toString()) // todo: move producerGroup tp header .addBody(SendMessageRequestBody.PRODUCERGROUP, eventMeshHttpClientConfig.getProducerGroup()) .addBody(SendMessageRequestBody.CONTENT, JsonUtils.serialize(cloudEvent)); diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/EventMeshMessageProducer.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/EventMeshMessageProducer.java index 85f6153953..e9c79f92c6 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/EventMeshMessageProducer.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/EventMeshMessageProducer.java @@ -1,5 +1,6 @@ package org.apache.eventmesh.client.http.producer; +import io.cloudevents.SpecVersion; import org.apache.eventmesh.client.http.AbstractHttpClient; import org.apache.eventmesh.client.http.EventMeshRetObj; import org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig; @@ -26,6 +27,10 @@ @Slf4j class EventMeshMessageProducer extends AbstractHttpClient implements EventMeshProtocolProducer { + private static final String PROTOCOL_TYPE = "eventmeshmessage"; + + private static final String PROTOCOL_DESC = "http"; + public EventMeshMessageProducer(EventMeshHttpClientConfig eventMeshHttpClientConfig) throws EventMeshException { super(eventMeshHttpClientConfig); } @@ -106,6 +111,10 @@ private RequestParam buildCommonPostParam(EventMeshMessage message) { .addHeader(ProtocolKey.ClientInstanceKey.USERNAME, eventMeshHttpClientConfig.getUserName()) .addHeader(ProtocolKey.ClientInstanceKey.PASSWD, eventMeshHttpClientConfig.getPassword()) .addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion()) + .addHeader(ProtocolKey.PROTOCOL_TYPE, PROTOCOL_TYPE) + .addHeader(ProtocolKey.PROTOCOL_DESC, PROTOCOL_DESC) + //default ce version is 1.0 + .addHeader(ProtocolKey.PROTOCOL_VERSION, SpecVersion.V1.toString()) .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) .addBody(SendMessageRequestBody.PRODUCERGROUP, eventMeshHttpClientConfig.getProducerGroup()) // todo: set message to content is better diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshCommon.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshCommon.java index 8238f52ceb..ca56422ff1 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshCommon.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/EventMeshCommon.java @@ -122,4 +122,6 @@ public class EventMeshCommon { public static String PREFIX_SESSION_TPS_STAT_EVENTREV = "event_rev_tps_"; public static String CLOUD_EVENTS_PROTOCOL_NAME = "cloudevents"; + + public static String EM_MESSAGE_PROTOCOL_NAME = "eventmeshmessage"; } diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java index a0c1ba85bc..e799fb726d 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java @@ -22,6 +22,7 @@ import java.util.concurrent.ThreadLocalRandom; import io.cloudevents.CloudEvent; +import io.cloudevents.SpecVersion; import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.common.protocol.SubscriptionType; import org.apache.eventmesh.common.protocol.tcp.Subscription; @@ -78,16 +79,26 @@ public static Package asyncMessageAck(Package in) { return msg; } - public static Package asyncCloudEvent(CloudEvent cloudEvent) { + public static Package buildPackage(Object message, Command command) { Package msg = new Package(); - msg.setHeader(new Header(Command.ASYNC_MESSAGE_TO_SERVER, 0, - null, generateRandomString(seqLength))); - msg.getHeader().putProperty(Constants.PROTOCOL_TYPE, - EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME); - msg.getHeader().putProperty(Constants.PROTOCOL_VERSION, - cloudEvent.getSpecVersion().toString()); + msg.setHeader(new Header(command, 0, + null, generateRandomString(seqLength))); + if (message instanceof CloudEvent) { + msg.getHeader().putProperty(Constants.PROTOCOL_TYPE, + EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME); + msg.getHeader().putProperty(Constants.PROTOCOL_VERSION, + ((CloudEvent) message).getSpecVersion().toString()); + } else if (message instanceof EventMeshMessage) { + msg.getHeader().putProperty(Constants.PROTOCOL_TYPE, + EventMeshCommon.EM_MESSAGE_PROTOCOL_NAME); + msg.getHeader().putProperty(Constants.PROTOCOL_VERSION, + SpecVersion.V1.toString()); + } else { + // unsupported protocol for server + return msg; + } msg.getHeader().putProperty(Constants.PROTOCOL_DESC, "tcp"); - msg.setBody(cloudEvent); + msg.setBody(message); return msg; } diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java index 2b9bfaf38b..46cf370e23 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java @@ -106,14 +106,13 @@ public void initChannel(SocketChannel ch) { } @Override - public void close() throws EventMeshException { + public void close() { try { channel.disconnect().sync(); workers.shutdownGracefully(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); log.warn("close tcp client failed.|remote address={}", channel.remoteAddress(), e); - throw new EventMeshException(e); } } diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPPubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPPubClient.java index 51341e16e0..7a1d03b1bc 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPPubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPPubClient.java @@ -1,54 +1,132 @@ package org.apache.eventmesh.client.tcp.impl.cloudevent; + + import org.apache.eventmesh.client.tcp.EventMeshTCPPubClient; -import org.apache.eventmesh.client.tcp.common.AsyncRRCallback; -import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.common.*; +import org.apache.eventmesh.client.tcp.conf.EventMeshTcpClientConfig; +import org.apache.eventmesh.client.tcp.impl.eventmeshmessage.EventMeshMessageTCPPubClient; +import org.apache.eventmesh.common.EventMeshMessage; import org.apache.eventmesh.common.exception.EventMeshException; +import org.apache.eventmesh.common.protocol.tcp.Command; import org.apache.eventmesh.common.protocol.tcp.Package; import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; import io.cloudevents.CloudEvent; import lombok.extern.slf4j.Slf4j; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + /** * A CloudEvent TCP publish client implementation. */ @Slf4j -public class CloudEventTCPPubClient implements EventMeshTCPPubClient { +public class CloudEventTCPPubClient extends TcpClient implements EventMeshTCPPubClient { + + private final UserAgent userAgent; + + private ReceiveMsgHook callback; + + private final ConcurrentHashMap callbackConcurrentHashMap = new ConcurrentHashMap<>(); + private ScheduledFuture task; + + public CloudEventTCPPubClient(EventMeshTcpClientConfig eventMeshTcpClientConfig) { + super(eventMeshTcpClientConfig); + this.userAgent = eventMeshTcpClientConfig.getUserAgent(); + } @Override public void init() throws EventMeshException { - + try { + open(new Handler()); + hello(); + } catch (Exception ex) { + throw new EventMeshException("Initialize EventMeshMessageTCPPubClient error", ex); + } } @Override public void heartbeat() throws EventMeshException { - + if (task != null) { + synchronized (EventMeshMessageTCPPubClient.class) { + task = scheduler.scheduleAtFixedRate(() -> { + try { + if (!isActive()) { + reconnect(); + } + Package msg = MessageUtils.heartBeat(); + io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } catch (Exception ignore) { + // ignore + } + }, EventMeshCommon.HEARTBEAT, EventMeshCommon.HEARTBEAT, TimeUnit.MILLISECONDS); + } + } } @Override public void reconnect() throws EventMeshException { - + try { + super.reconnect(); + hello(); + } catch (Exception ex) { + throw new EventMeshException("reconnect error", ex); + } } @Override - public Package rr(CloudEvent msg, long timeout) throws EventMeshException { - return null; + public Package rr(CloudEvent event, long timeout) throws EventMeshException { + try { + Package msg = MessageUtils.buildPackage(event, Command.REQUEST_TO_SERVER); + log.info("{}|rr|send|type={}|msg={}", clientNo, msg, msg); + return io(msg, timeout); + } catch (Exception ex) { + throw new EventMeshException("rr error"); + } } @Override - public void asyncRR(CloudEvent msg, AsyncRRCallback callback, long timeout) throws EventMeshException { - + public void asyncRR(CloudEvent event, AsyncRRCallback callback, long timeout) throws EventMeshException { + try { + Package msg = MessageUtils.buildPackage(event, Command.REQUEST_TO_SERVER); + super.send(msg); + this.callbackConcurrentHashMap.put((String) RequestContext._key(msg), callback); + } catch (Exception ex) { + // should trigger callback? + throw new EventMeshException("asyncRR error", ex); + } } @Override public Package publish(CloudEvent cloudEvent, long timeout) throws EventMeshException { - return null; + try { + // todo: transform EventMeshMessage to Package + Package msg = MessageUtils.buildPackage(cloudEvent, Command.ASYNC_MESSAGE_TO_SERVER); + log.info("SimplePubClientImpl cloud event|{}|publish|send|type={}|protocol={}|msg={}", + clientNo, msg.getHeader().getCommand(), + msg.getHeader().getProperty(PropertyConst.PROPERTY_MESSAGE_PROTOCOL), msg); + return io(msg, timeout); + } catch (Exception ex) { + throw new EventMeshException("publish error", ex); + } } @Override public void broadcast(CloudEvent cloudEvent, long timeout) throws EventMeshException { - + try { + // todo: transform EventMeshMessage to Package + Package msg = MessageUtils.buildPackage(cloudEvent, Command.BROADCAST_MESSAGE_TO_SERVER); + log.info("{}|publish|send|type={}|protocol={}|msg={}", clientNo, msg.getHeader().getCommand(), + msg.getHeader().getProperty(PropertyConst.PROPERTY_MESSAGE_PROTOCOL), msg); + super.send(msg); + } catch (Exception ex) { + throw new EventMeshException("Broadcast message error", ex); + } } @Override @@ -57,7 +135,45 @@ public void registerBusiHandler(ReceiveMsgHook handler) throws Event } @Override - public void close() throws EventMeshException { + public void close() { + + } + + // todo: move to abstract class + @ChannelHandler.Sharable + private class Handler extends SimpleChannelInboundHandler { + @Override + protected void channelRead0(ChannelHandlerContext ctx, Package msg) throws Exception { + log.info("SimplePubClientImpl|{}|receive|type={}|msg={}", clientNo, msg.getHeader(), msg); + + Command cmd = msg.getHeader().getCommand(); + if (cmd == Command.RESPONSE_TO_CLIENT) { + if (callback != null) { + callback.handle(msg, ctx); + } + Package pkg = MessageUtils.responseToClientAck(msg); + send(pkg); + } else if (cmd == Command.SERVER_GOODBYE_REQUEST) { + //TODO + } + + RequestContext context = contexts.get(RequestContext._key(msg)); + if (context != null) { + contexts.remove(context.getKey()); + context.finish(msg); + } + } + } + + // todo: remove hello + private void hello() throws Exception { + Package msg = MessageUtils.hello(userAgent); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } + // todo: remove goodbye + private void goodbye() throws Exception { + Package msg = MessageUtils.goodbye(); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } } diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java index d726c4b90c..716feae3cb 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java @@ -1,59 +1,191 @@ package org.apache.eventmesh.client.tcp.impl.cloudevent; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import org.apache.commons.collections4.CollectionUtils; import org.apache.eventmesh.client.tcp.EventMeshTCPSubClient; -import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.common.*; +import org.apache.eventmesh.client.tcp.conf.EventMeshTcpClientConfig; +import org.apache.eventmesh.client.tcp.impl.eventmeshmessage.EventMeshMessageTCPSubClient; +import org.apache.eventmesh.common.EventMeshMessage; import org.apache.eventmesh.common.exception.EventMeshException; +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.protocol.tcp.Command; +import org.apache.eventmesh.common.protocol.tcp.Package; import org.apache.eventmesh.common.protocol.tcp.UserAgent; import io.cloudevents.CloudEvent; import lombok.extern.slf4j.Slf4j; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + /** * CloudEvent TCP subscribe client implementation. */ @Slf4j -public class CloudEventTCPSubClient implements EventMeshTCPSubClient { +public class CloudEventTCPSubClient extends TcpClient implements EventMeshTCPSubClient { + + private final UserAgent userAgent; + private final List subscriptionItems = Collections.synchronizedList(new ArrayList<>()); + private ReceiveMsgHook callback; + private ScheduledFuture task; + + public CloudEventTCPSubClient(EventMeshTcpClientConfig eventMeshTcpClientConfig) { + super(eventMeshTcpClientConfig); + this.userAgent = eventMeshTcpClientConfig.getUserAgent(); + } @Override public void init() throws EventMeshException { - + try { + open(new Handler()); + hello(); + log.info("SimpleSubClientImpl|{}|started!", clientNo); + } catch (Exception ex) { + throw new EventMeshException("Initialize EventMeshMessageTcpSubClient error", ex); + } } @Override public void heartbeat() throws EventMeshException { - + if (task == null) { + synchronized (EventMeshMessageTCPSubClient.class) { + task = scheduler.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + try { + if (!isActive()) { + reconnect(); + } + Package msg = MessageUtils.heartBeat(); + io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } catch (Exception ignore) { + // + } + } + }, EventMeshCommon.HEARTBEAT, EventMeshCommon.HEARTBEAT, TimeUnit.MILLISECONDS); + } + } } @Override public void reconnect() throws EventMeshException { - + try { + super.reconnect(); + hello(); + if (!CollectionUtils.isEmpty(subscriptionItems)) { + for (SubscriptionItem item : subscriptionItems) { + Package request = MessageUtils.subscribe(item.getTopic(), item.getMode(), item.getType()); + this.io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } + } + listen(); + } catch (Exception ex) { + // + } } @Override public void subscribe(String topic, SubscriptionMode subscriptionMode, SubscriptionType subscriptionType) throws EventMeshException { - + try { + subscriptionItems.add(new SubscriptionItem(topic, subscriptionMode, subscriptionType)); + Package request = MessageUtils.subscribe(topic, subscriptionMode, subscriptionType); + io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } catch (Exception ex) { + throw new EventMeshException("Subscribe error", ex); + } } @Override public void unsubscribe() throws EventMeshException { - + try { + Package request = MessageUtils.unsubscribe(); + io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } catch (Exception ex) { + throw new EventMeshException("Unsubscribe error", ex); + } } @Override public void listen() throws EventMeshException { + try { + Package request = MessageUtils.listen(); + io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } catch (Exception ex) { + throw new EventMeshException("Listen error", ex); + } + } + + private void goodbye() throws Exception { + Package msg = MessageUtils.goodbye(); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } + private void hello() throws Exception { + Package msg = MessageUtils.hello(userAgent); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); } @Override public void registerBusiHandler(ReceiveMsgHook handler) throws EventMeshException { - + this.callback = handler; } @Override - public void close() throws EventMeshException{ + public void close() { + try { + task.cancel(false); + goodbye(); + super.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + private class Handler extends SimpleChannelInboundHandler { + @SuppressWarnings("Duplicates") + @Override + protected void channelRead0(ChannelHandlerContext ctx, Package msg) throws Exception { + Command cmd = msg.getHeader().getCommand(); + log.info("|receive|type={}|msg={}", cmd, msg); + if (cmd == Command.REQUEST_TO_CLIENT) { + if (callback != null) { + callback.handle(msg, ctx); + } + Package pkg = MessageUtils.requestToClientAck(msg); + send(pkg); + } else if (cmd == Command.ASYNC_MESSAGE_TO_CLIENT) { + Package pkg = MessageUtils.asyncMessageAck(msg); + if (callback != null) { + callback.handle(msg, ctx); + } + send(pkg); + } else if (cmd == Command.BROADCAST_MESSAGE_TO_CLIENT) { + Package pkg = MessageUtils.broadcastMessageAck(msg); + if (callback != null) { + callback.handle(msg, ctx); + } + send(pkg); + } else if (cmd == Command.SERVER_GOODBYE_REQUEST) { + //TODO + } else { + log.error("msg ignored|{}|{}", cmd, msg); + } + RequestContext context = contexts.get(RequestContext._key(msg)); + if (context != null) { + contexts.remove(context.getKey()); + context.finish(msg); + } else { + log.error("msg ignored,context not found.|{}|{}", cmd, msg); + } + } } + } diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java index ba9e0afda4..1f4e774507 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java @@ -85,7 +85,7 @@ public void reconnect() throws EventMeshException { @Override public Package rr(EventMeshMessage eventMeshMessage, long timeout) throws EventMeshException { try { - Package msg = MessageUtils.asyncCloudEvent(eventMeshMessage); + Package msg = MessageUtils.buildPackage(eventMeshMessage, Command.REQUEST_TO_SERVER); log.info("{}|rr|send|type={}|msg={}", clientNo, msg, msg); return io(msg, timeout); } catch (Exception ex) { @@ -96,7 +96,7 @@ public Package rr(EventMeshMessage eventMeshMessage, long timeout) throws EventM @Override public void asyncRR(EventMeshMessage eventMeshMessage, AsyncRRCallback callback, long timeout) throws EventMeshException { try { - Package msg = MessageUtils.asyncCloudEvent(eventMeshMessage); + Package msg = MessageUtils.buildPackage(eventMeshMessage, Command.REQUEST_TO_SERVER); super.send(msg); this.callbackConcurrentHashMap.put((String) RequestContext._key(msg), callback); } catch (Exception ex) { @@ -109,7 +109,7 @@ public void asyncRR(EventMeshMessage eventMeshMessage, AsyncRRCallback callback, public Package publish(EventMeshMessage eventMeshMessage, long timeout) throws EventMeshException { try { // todo: transform EventMeshMessage to Package - Package msg = MessageUtils.asyncCloudEvent(eventMeshMessage); + Package msg = MessageUtils.buildPackage(eventMeshMessage, Command.ASYNC_MESSAGE_TO_SERVER); log.info("SimplePubClientImpl cloud event|{}|publish|send|type={}|protocol={}|msg={}", clientNo, msg.getHeader().getCommand(), msg.getHeader().getProperty(PropertyConst.PROPERTY_MESSAGE_PROTOCOL), msg); @@ -123,7 +123,7 @@ public Package publish(EventMeshMessage eventMeshMessage, long timeout) throws E public void broadcast(EventMeshMessage eventMeshMessage, long timeout) throws EventMeshException { try { // todo: transform EventMeshMessage to Package - Package msg = MessageUtils.asyncCloudEvent(eventMeshMessage); + Package msg = MessageUtils.buildPackage(eventMeshMessage, Command.BROADCAST_MESSAGE_TO_SERVER); log.info("{}|publish|send|type={}|protocol={}|msg={}", clientNo, msg.getHeader().getCommand(), msg.getHeader().getProperty(PropertyConst.PROPERTY_MESSAGE_PROTOCOL), msg); super.send(msg); @@ -138,7 +138,7 @@ public void registerBusiHandler(ReceiveMsgHook receiveMsgHook) } @Override - public void close() throws EventMeshException { + public void close() { try { task.cancel(false); goodbye(); diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java index 08aa170083..cdce845a96 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java @@ -113,6 +113,25 @@ public void unsubscribe() throws EventMeshException { } } + private void goodbye() throws Exception { + Package msg = MessageUtils.goodbye(); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } + + private void hello() throws Exception { + Package msg = MessageUtils.hello(userAgent); + this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } + + public void listen() throws EventMeshException { + try { + Package request = MessageUtils.listen(); + io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } catch (Exception ex) { + throw new EventMeshException("Listen error", ex); + } + } + @Override public void registerBusiHandler(ReceiveMsgHook receiveMsgHook) throws EventMeshException { @@ -120,13 +139,13 @@ public void registerBusiHandler(ReceiveMsgHook receiveMsgHook) } @Override - public void close() throws EventMeshException { + public void close() { try { task.cancel(false); goodbye(); super.close(); } catch (Exception ex) { - throw new EventMeshException("Close EventMeshMessageTcpSubClient error", ex); + ex.printStackTrace(); } } @@ -169,22 +188,4 @@ protected void channelRead0(ChannelHandlerContext ctx, Package msg) throws Excep } } - private void goodbye() throws Exception { - Package msg = MessageUtils.goodbye(); - this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); - } - - private void hello() throws Exception { - Package msg = MessageUtils.hello(userAgent); - this.io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); - } - - public void listen() throws EventMeshException { - try { - Package request = MessageUtils.listen(); - io(request, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); - } catch (Exception ex) { - throw new EventMeshException("Listen error", ex); - } - } } From add1d1c319eed5f13f94852f2b6ccc5e34ff58ac Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Wed, 24 Nov 2021 15:18:30 +0800 Subject: [PATCH 02/13] fix compile error --- eventmesh-common/build.gradle | 6 +- .../SendMessageBatchV2ProtocolResolver.java | 58 ++++++------ .../processor/SendSyncMessageProcessor.java | 7 +- .../http/push/AsyncHTTPPushRequest.java | 4 +- .../group/ClientSessionGroupMapping.java | 94 +++++++++++-------- .../eventmesh/runtime/util/EventMeshUtil.java | 57 ++++++----- .../eventmesh/runtime/util/OMSUtil.java | 73 -------------- 7 files changed, 126 insertions(+), 173 deletions(-) delete mode 100644 eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/OMSUtil.java diff --git a/eventmesh-common/build.gradle b/eventmesh-common/build.gradle index 84a03a5d39..699e9c4fdb 100644 --- a/eventmesh-common/build.gradle +++ b/eventmesh-common/build.gradle @@ -34,9 +34,9 @@ dependencies { implementation "com.lmax:disruptor" - implementation "com.fasterxml.jackson.core:jackson-databind" - implementation "com.fasterxml.jackson.core:jackson-core" - implementation "com.fasterxml.jackson.core:jackson-annotations" + api "com.fasterxml.jackson.core:jackson-databind" + api "com.fasterxml.jackson.core:jackson-core" + api "com.fasterxml.jackson.core:jackson-annotations" implementation "org.apache.httpcomponents:httpclient" diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java index fedbbe046e..2c33aec9bf 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java @@ -5,7 +5,9 @@ import io.cloudevents.core.builder.CloudEventBuilder; import io.cloudevents.core.v03.CloudEventV03; import io.cloudevents.core.v1.CloudEventV1; + import org.apache.commons.lang3.StringUtils; + import org.apache.eventmesh.common.protocol.http.body.Body; import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody; import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; @@ -42,37 +44,37 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) { event = JsonUtils.deserialize(content, CloudEventV1.class); event = CloudEventBuilder.from(event) - .withExtension(ProtocolKey.REQUEST_CODE, code) - .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) - .withExtension(ProtocolKey.ClientInstanceKey.IDC, idc) - .withExtension(ProtocolKey.ClientInstanceKey.IP, ip) - .withExtension(ProtocolKey.ClientInstanceKey.PID, pid) - .withExtension(ProtocolKey.ClientInstanceKey.SYS, sys) - .withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username) - .withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd) - .withExtension(ProtocolKey.VERSION, version.getVersion()) - .withExtension(ProtocolKey.LANGUAGE, language) - .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) - .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) - .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) - .build(); + .withExtension(ProtocolKey.REQUEST_CODE, code) + .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) + .withExtension(ProtocolKey.ClientInstanceKey.IDC, idc) + .withExtension(ProtocolKey.ClientInstanceKey.IP, ip) + .withExtension(ProtocolKey.ClientInstanceKey.PID, pid) + .withExtension(ProtocolKey.ClientInstanceKey.SYS, sys) + .withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username) + .withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd) + .withExtension(ProtocolKey.VERSION, version.getVersion()) + .withExtension(ProtocolKey.LANGUAGE, language) + .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) + .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) + .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) + .build(); } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) { event = JsonUtils.deserialize(content, CloudEventV03.class); event = CloudEventBuilder.from(event) - .withExtension(ProtocolKey.REQUEST_CODE, code) - .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) - .withExtension(ProtocolKey.ClientInstanceKey.IDC, idc) - .withExtension(ProtocolKey.ClientInstanceKey.IP, ip) - .withExtension(ProtocolKey.ClientInstanceKey.PID, pid) - .withExtension(ProtocolKey.ClientInstanceKey.SYS, sys) - .withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username) - .withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd) - .withExtension(ProtocolKey.VERSION, version.getVersion()) - .withExtension(ProtocolKey.LANGUAGE, language) - .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) - .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) - .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) - .build(); + .withExtension(ProtocolKey.REQUEST_CODE, code) + .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) + .withExtension(ProtocolKey.ClientInstanceKey.IDC, idc) + .withExtension(ProtocolKey.ClientInstanceKey.IP, ip) + .withExtension(ProtocolKey.ClientInstanceKey.PID, pid) + .withExtension(ProtocolKey.ClientInstanceKey.SYS, sys) + .withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username) + .withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd) + .withExtension(ProtocolKey.VERSION, version.getVersion()) + .withExtension(ProtocolKey.LANGUAGE, language) + .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) + .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) + .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) + .build(); } return event; } catch (Exception e) { diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java index ef025a007a..5842eed21b 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java @@ -273,8 +273,11 @@ public void onSuccess(CloudEvent event) { HttpCommand succ = asyncContext.getRequest().createHttpCommandResponse( sendMessageResponseHeader, SendMessageResponseBody.buildBody(EventMeshRetCode.SUCCESS.getRetCode(), - JsonUtils.serialize(new SendMessageResponseBody.ReplyMessage(topic, - rtnMsg)))); + JsonUtils.serialize(SendMessageResponseBody.ReplyMessage.builder() + .topic(topic) + .body(rtnMsg) + .properties(EventMeshUtil.getEventProp(event)) + .build()))); asyncContext.onComplete(succ, handler); } catch (Exception ex) { HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java index 5fa56ef70f..0bcf02c6c6 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java @@ -36,7 +36,7 @@ import org.apache.eventmesh.protocol.api.ProtocolPluginFactory; import org.apache.eventmesh.runtime.constants.EventMeshConstants; import org.apache.eventmesh.runtime.core.protocol.http.consumer.HandleMsgContext; -import org.apache.eventmesh.runtime.util.OMSUtil; +import org.apache.eventmesh.runtime.util.EventMeshUtil; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; @@ -150,7 +150,7 @@ public void tryHTTPRequest() { body.add(new BasicNameValuePair(PushMessageRequestBody.TOPIC, handleMsgContext.getTopic())); body.add(new BasicNameValuePair(PushMessageRequestBody.EXTFIELDS, - JsonUtils.serialize(OMSUtil.getEventProp(handleMsgContext.getEvent())))); + JsonUtils.serialize(EventMeshUtil.getEventProp(handleMsgContext.getEvent())))); try { builder.setEntity(new UrlEncodedFormEntity(body)); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java index 674fb18e83..8f3e383eb4 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java @@ -46,6 +46,7 @@ import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; import org.apache.eventmesh.runtime.util.EventMeshUtil; import org.apache.eventmesh.runtime.util.RemotingHelper; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,9 +57,11 @@ public class ClientSessionGroupMapping { private ConcurrentHashMap sessionTable = new ConcurrentHashMap<>(); - private ConcurrentHashMap clientGroupMap = new ConcurrentHashMap(); + private ConcurrentHashMap clientGroupMap = + new ConcurrentHashMap(); - private ConcurrentHashMap lockMap = new ConcurrentHashMap(); + private ConcurrentHashMap lockMap = + new ConcurrentHashMap(); private EventMeshTCPServer eventMeshTCPServer; @@ -123,7 +126,7 @@ public synchronized void closeSession(ChannelHandlerContext ctx) throws Exceptio @Override public void operationComplete(ChannelFuture future) throws Exception { logger.info("close the connection to remote address[{}] result: {}", remoteAddress, - future.isSuccess()); + future.isSuccess()); } }); sessionLogger.info("session|close|succeed|address={}|msg={}", addr, "no session was found"); @@ -169,7 +172,7 @@ private void closeSession(Session session) throws Exception { @Override public void operationComplete(ChannelFuture future) throws Exception { logger.info("close the connection to remote address[{}] result: {}", remoteAddress, - future.isSuccess()); + future.isSuccess()); } }); } @@ -179,7 +182,8 @@ public void operationComplete(ChannelFuture future) throws Exception { private ClientGroupWrapper constructClientGroupWrapper(String sysId, String producerGroup, String consumerGroup, EventMeshTCPServer eventMeshTCPServer, DownstreamDispatchStrategy downstreamDispatchStrategy) { - return new ClientGroupWrapper(sysId, producerGroup, consumerGroup, eventMeshTCPServer, downstreamDispatchStrategy); + return new ClientGroupWrapper(sysId, producerGroup, consumerGroup, eventMeshTCPServer, + downstreamDispatchStrategy); } private void initClientGroupWrapper(UserAgent user, Session session) throws Exception { @@ -192,7 +196,7 @@ private void initClientGroupWrapper(UserAgent user, Session session) throws Exce synchronized (lockMap.get(user.getSubsystem())) { if (!clientGroupMap.containsKey(user.getSubsystem())) { ClientGroupWrapper cgw = constructClientGroupWrapper(user.getSubsystem(), user.getProducerGroup(), - user.getConsumerGroup(), eventMeshTCPServer, new FreePriorityDispatchStrategy()); + user.getConsumerGroup(), eventMeshTCPServer, new FreePriorityDispatchStrategy()); clientGroupMap.put(user.getSubsystem(), cgw); logger.info("create new ClientGroupWrapper, subsystem:{}", user.getSubsystem()); } @@ -297,32 +301,40 @@ private void handleUnackMsgsInSession(Session session) { for (Map.Entry entry : unAckMsg.entrySet()) { DownStreamMsgContext downStreamMsgContext = entry.getValue(); if (SubscriptionMode.BROADCASTING.equals(downStreamMsgContext.subscriptionItem.getMode())) { - logger.warn("exist broadcast msg unack when closeSession,seq:{},bizSeq:{},client:{}", downStreamMsgContext.seq, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.event), session.getClient()); + logger.warn("exist broadcast msg unack when closeSession,seq:{},bizSeq:{},client:{}", + downStreamMsgContext.seq, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.event), + session.getClient()); continue; } - Session reChooseSession = session.getClientGroupWrapper().get().getDownstreamDispatchStrategy().select(session.getClientGroupWrapper().get().getConsumerGroup(), - downStreamMsgContext.event.getSubject(), Objects.requireNonNull(session.getClientGroupWrapper().get()).groupConsumerSessions); + Session reChooseSession = session.getClientGroupWrapper().get().getDownstreamDispatchStrategy() + .select(session.getClientGroupWrapper().get().getConsumerGroup(), + downStreamMsgContext.event.getSubject(), + Objects.requireNonNull(session.getClientGroupWrapper().get()).groupConsumerSessions); if (reChooseSession != null) { downStreamMsgContext.session = reChooseSession; reChooseSession.getPusher().unAckMsg(downStreamMsgContext.seq, downStreamMsgContext); reChooseSession.downstreamMsg(downStreamMsgContext); - logger.info("rePush msg form unAckMsgs,seq:{},rePushClient:{}", entry.getKey(), downStreamMsgContext.session.getClient()); + logger.info("rePush msg form unAckMsgs,seq:{},rePushClient:{}", entry.getKey(), + downStreamMsgContext.session.getClient()); } else { - logger.warn("select session fail in handleUnackMsgsInSession,seq:{},topic:{}", entry.getKey(), downStreamMsgContext.event.getSubject()); + logger.warn("select session fail in handleUnackMsgsInSession,seq:{},topic:{}", entry.getKey(), + downStreamMsgContext.event.getSubject()); } } } } private void cleanClientGroupWrapperCommon(Session session) throws Exception { - logger.info("GroupConsumerSessions size:{}", session.getClientGroupWrapper().get().getGroupConsumerSessions().size()); + logger.info("GroupConsumerSessions size:{}", + session.getClientGroupWrapper().get().getGroupConsumerSessions().size()); if (session.getClientGroupWrapper().get().getGroupConsumerSessions().size() == 0) { shutdownClientGroupConsumer(session); } - logger.info("GroupProducerSessions size:{}", session.getClientGroupWrapper().get().getGroupProducerSessions().size()); + logger.info("GroupProducerSessions size:{}", + session.getClientGroupWrapper().get().getGroupProducerSessions().size()); if ((session.getClientGroupWrapper().get().getGroupConsumerSessions().size() == 0) - && (session.getClientGroupWrapper().get().getGroupProducerSessions().size() == 0)) { + && (session.getClientGroupWrapper().get().getGroupProducerSessions().size() == 0)) { shutdownClientGroupProducer(session); clientGroupMap.remove(session.getClientGroupWrapper().get().getSysId()); @@ -350,22 +362,24 @@ private void shutdownClientGroupProducer(Session session) throws Exception { private void initSessionCleaner() { eventMeshTCPServer.getScheduler().scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - Iterator sessionIterator = sessionTable.values().iterator(); - while (sessionIterator.hasNext()) { - Session tmp = sessionIterator.next(); - if (System.currentTimeMillis() - tmp.getLastHeartbeatTime() > eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSessionExpiredInMills) { - try { - logger.warn("clean expired session,client:{}", tmp.getClient()); - closeSession(tmp.getContext()); - } catch (Exception e) { - logger.error("say goodbye to session error! {}", tmp, e); - } - } - } - } - }, 1000, eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSessionExpiredInMills, TimeUnit.MILLISECONDS); + @Override + public void run() { + Iterator sessionIterator = sessionTable.values().iterator(); + while (sessionIterator.hasNext()) { + Session tmp = sessionIterator.next(); + if (System.currentTimeMillis() - tmp.getLastHeartbeatTime() > + eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSessionExpiredInMills) { + try { + logger.warn("clean expired session,client:{}", tmp.getClient()); + closeSession(tmp.getContext()); + } catch (Exception e) { + logger.error("say goodbye to session error! {}", tmp, e); + } + } + } + } + }, 1000, eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSessionExpiredInMills, + TimeUnit.MILLISECONDS); } private void initDownStreamMsgContextCleaner() { @@ -386,7 +400,7 @@ public void run() { downStreamMsgContext.ackMsg(); tmp.getPusher().getUnAckMsg().remove(seqKey); logger.warn("remove expire downStreamMsgContext, session:{}, topic:{}, seq:{}", tmp, - downStreamMsgContext.event.getSubject(), seqKey); + downStreamMsgContext.event.getSubject(), seqKey); } } } @@ -406,8 +420,8 @@ public void start() throws Exception { public void shutdown() throws Exception { logger.info("begin to close sessions gracefully"); - for(ClientGroupWrapper clientGroupWrapper : clientGroupMap.values()){ - for(Session subSession : clientGroupWrapper.getGroupConsumerSessions()){ + for (ClientGroupWrapper clientGroupWrapper : clientGroupMap.values()) { + for (Session subSession : clientGroupWrapper.getGroupConsumerSessions()) { try { EventMeshTcp2Client.serverGoodby2Client(eventMeshTCPServer, subSession, this); } catch (Exception e) { @@ -415,7 +429,7 @@ public void shutdown() throws Exception { } } - for(Session pubSession : clientGroupWrapper.getGroupProducerSessions()){ + for (Session pubSession : clientGroupWrapper.getGroupProducerSessions()) { try { EventMeshTcp2Client.serverGoodby2Client(eventMeshTCPServer, pubSession, this); } catch (Exception e) { @@ -437,7 +451,7 @@ public void shutdown() throws Exception { sessionTable.values().parallelStream().forEach(itr -> { try { - EventMeshTcp2Client.serverGoodby2Client(this.eventMeshTCPServer,itr, this); + EventMeshTcp2Client.serverGoodby2Client(this.eventMeshTCPServer, itr, this); } catch (Exception e) { logger.error("say goodbye to session error! {}", itr, e); } @@ -470,15 +484,15 @@ public Map> prepareEventMeshClientDistributionData( return result; } - public Map> prepareProxyClientDistributionData(){ + public Map> prepareProxyClientDistributionData() { Map> result = null; - if(!clientGroupMap.isEmpty()){ + if (!clientGroupMap.isEmpty()) { result = new HashMap<>(); - for(Map.Entry entry : clientGroupMap.entrySet()){ + for (Map.Entry entry : clientGroupMap.entrySet()) { Map map = new HashMap(); - map.put(EventMeshConstants.PURPOSE_SUB,entry.getValue().getGroupConsumerSessions().size()); - map.put(EventMeshConstants.PURPOSE_PUB,entry.getValue().getGroupProducerSessions().size()); + map.put(EventMeshConstants.PURPOSE_SUB, entry.getValue().getGroupConsumerSessions().size()); + map.put(EventMeshConstants.PURPOSE_PUB, entry.getValue().getGroupProducerSessions().size()); result.put(entry.getKey(), map); } } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java index 58e05e0d65..035e9d4c97 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java @@ -17,9 +17,6 @@ package org.apache.eventmesh.runtime.util; - -import static org.apache.eventmesh.runtime.util.OMSUtil.isOMSHeader; - import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; @@ -28,6 +25,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Enumeration; +import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -149,6 +147,15 @@ public static String getMessageBizSeq(CloudEvent event) { return keys; } + public static Map getEventProp(CloudEvent event) { + Set extensionSet = event.getExtensionNames(); + Map prop = new HashMap<>(); + for (String extensionKey : extensionSet) { + prop.put(extensionKey, event.getExtension(extensionKey).toString()); + } + return prop; + } + // public static org.apache.rocketmq.common.message.Message decodeMessage(AccessMessage accessMessage) { // org.apache.rocketmq.common.message.Message msg = new org.apache.rocketmq.common.message.Message(); // msg.setTopic(accessMessage.getTopic()); @@ -160,28 +167,28 @@ public static String getMessageBizSeq(CloudEvent event) { // return msg; // } - public static Message decodeMessage(EventMeshMessage eventMeshMessage) { - Message omsMsg = new Message(); - omsMsg.setBody(eventMeshMessage.getBody().getBytes()); - omsMsg.setTopic(eventMeshMessage.getTopic()); - Properties systemProperties = new Properties(); - Properties userProperties = new Properties(); - - final Set> entries = eventMeshMessage.getProperties().entrySet(); - - for (final Map.Entry entry : entries) { - if (isOMSHeader(entry.getKey())) { - systemProperties.put(entry.getKey(), entry.getValue()); - } else { - userProperties.put(entry.getKey(), entry.getValue()); - } - } - - systemProperties.put(Constants.PROPERTY_MESSAGE_DESTINATION, eventMeshMessage.getTopic()); - omsMsg.setSystemProperties(systemProperties); - omsMsg.setUserProperties(userProperties); - return omsMsg; - } +// public static Message decodeMessage(EventMeshMessage eventMeshMessage) { +// Message omsMsg = new Message(); +// omsMsg.setBody(eventMeshMessage.getBody().getBytes()); +// omsMsg.setTopic(eventMeshMessage.getTopic()); +// Properties systemProperties = new Properties(); +// Properties userProperties = new Properties(); +// +// final Set> entries = eventMeshMessage.getProperties().entrySet(); +// +// for (final Map.Entry entry : entries) { +// if (isOMSHeader(entry.getKey())) { +// systemProperties.put(entry.getKey(), entry.getValue()); +// } else { +// userProperties.put(entry.getKey(), entry.getValue()); +// } +// } +// +// systemProperties.put(Constants.PROPERTY_MESSAGE_DESTINATION, eventMeshMessage.getTopic()); +// omsMsg.setSystemProperties(systemProperties); +// omsMsg.setUserProperties(userProperties); +// return omsMsg; +// } // public static AccessMessage encodeMessage(org.apache.rocketmq.common.message.Message msg) throws Exception { // AccessMessage accessMessage = new AccessMessage(); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/OMSUtil.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/OMSUtil.java deleted file mode 100644 index b8f2d1e8cb..0000000000 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/OMSUtil.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Licensed to Apache Software Foundation (ASF) under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Apache Software Foundation (ASF) licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.eventmesh.runtime.util; - -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import io.cloudevents.CloudEvent; -import io.openmessaging.api.Message; -import io.openmessaging.api.OMSBuiltinKeys; - -public class OMSUtil { - - public static boolean isOMSHeader(String value) { - for (Field field : OMSBuiltinKeys.class.getDeclaredFields()) { - try { - if (field.get(OMSBuiltinKeys.class).equals(value)) { - return true; - } - } catch (IllegalAccessException e) { - return false; - } - } - return false; - } - -// public static Properties convertKeyValue2Prop(KeyValue keyValue){ -// Properties properties = new Properties(); -// for (String key : keyValue.keySet()){ -// properties.put(key, keyValue.getString(key)); -// } -// return properties; -// } - - @SuppressWarnings("unchecked") - public static Map combineProp(Properties p1, Properties p2) { - Properties properties = new Properties(); - properties.putAll(p1); - properties.putAll(p2); - - return new HashMap<>((Map) properties); - } - - public static Map getEventProp(CloudEvent event) { - Set extensionSet = event.getExtensionNames(); - Map prop = new HashMap<>(); - for (String extensionKey : extensionSet) { - prop.put(extensionKey, event.getExtension(extensionKey).toString()); - } - return prop; - } - -} From 8c098e603ad82e78978a0fbd029b035ae68689a7 Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Wed, 24 Nov 2021 17:56:31 +0800 Subject: [PATCH 03/13] fix sdk error --- .../common/protocol/tcp/UserAgent.java | 68 +++++++++++++++++++ eventmesh-examples/build.gradle | 4 ++ 2 files changed, 72 insertions(+) diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java index 701cd6260e..6ad7a32072 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java @@ -20,6 +20,8 @@ import lombok.Builder; import lombok.Data; +import java.util.Objects; + @Data @Builder public class UserAgent { @@ -40,4 +42,70 @@ public class UserAgent { @Builder.Default private int unack = 0; + public UserAgent() { + } + + public UserAgent(String env, String subsystem, String path, int pid, String host, int port, String version, + String username, String password, String idc, String producerGroup, String consumerGroup, + String purpose, int unack) { + this.env = env; + this.subsystem = subsystem; + this.path = path; + this.pid = pid; + this.host = host; + this.port = port; + this.version = version; + this.username = username; + this.password = password; + this.idc = idc; + this.producerGroup = producerGroup; + this.consumerGroup = consumerGroup; + this.purpose = purpose; + this.unack = unack; + } + + @Override + public String toString() { + return String.format( + "UserAgent{env='%s'subsystem='%s', path='%s', pid=%d, host='%s', port=%d, version='%s', idc='%s', purpose='%s', unack='%d'}", + env, subsystem, path, pid, host, port, version, idc, purpose, unack); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserAgent userAgent = (UserAgent) o; + + if (pid != userAgent.pid) return false; + if (port != userAgent.port) return false; + if (unack != userAgent.unack) return false; + if (!Objects.equals(subsystem, userAgent.subsystem)) return false; + if (!Objects.equals(path, userAgent.path)) return false; + if (!Objects.equals(host, userAgent.host)) return false; + if (!Objects.equals(purpose, userAgent.purpose)) return false; + if (!Objects.equals(version, userAgent.version)) return false; + if (!Objects.equals(username, userAgent.username)) return false; + if (!Objects.equals(password, userAgent.password)) return false; + if (!Objects.equals(env, userAgent.env)) return false; + return Objects.equals(idc, userAgent.idc); + } + + @Override + public int hashCode() { + int result = subsystem != null ? subsystem.hashCode() : 0; + result = 31 * result + (path != null ? path.hashCode() : 0); + result = 31 * result + pid; + result = 31 * result + (host != null ? host.hashCode() : 0); + result = 31 * result + (purpose != null ? purpose.hashCode() : 0); + result = 31 * result + port; + result = 31 * result + (version != null ? version.hashCode() : 0); + result = 31 * result + (username != null ? username.hashCode() : 0); + result = 31 * result + (password != null ? password.hashCode() : 0); + result = 31 * result + (idc != null ? idc.hashCode() : 0); + result = 31 * result + (env != null ? env.hashCode() : 0); + result = 31 * result + unack; + return result; + } } diff --git a/eventmesh-examples/build.gradle b/eventmesh-examples/build.gradle index b7b1f654a3..3cb42bce3e 100644 --- a/eventmesh-examples/build.gradle +++ b/eventmesh-examples/build.gradle @@ -15,6 +15,10 @@ * limitations under the License. */ +configurations { + implementation.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' +} + dependencies { implementation project(":eventmesh-sdk-java") implementation project(":eventmesh-common") From 9f6e15593a1811cb37d48519bae2200016c208a4 Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Thu, 25 Nov 2021 19:44:33 +0800 Subject: [PATCH 04/13] 1.remove the openmessage from connector-api 2.fix the standalone connector --- docs/cn/features/spi.md | 2 +- docs/en/features/spi.md | 2 +- .../eventmesh-connector-api/build.gradle | 2 +- .../org/apache/eventmesh/api/RRCallback.java | 56 +- .../api/consumer/MeshMQPushConsumer.java | 84 ++- .../api/producer/MeshMQProducer.java | 90 ++-- .../eventmesh/api/producer/Producer.java | 3 +- .../rocketmq/MessagingAccessPointImpl.java | 180 +++---- .../rocketmq/config/ClientConfig.java | 3 +- .../rocketmq/consumer/PushConsumerImpl.java | 15 +- .../consumer/RocketMQConsumerImpl.java | 16 - .../rocketmq/producer/ProducerImpl.java | 10 +- .../producer/RocketMQProducerImpl.java | 13 +- .../rocketmq/promise/DefaultPromise.java | 227 -------- .../connector/rocketmq/utils/OMSUtil.java | 490 +++++++++--------- ...rg.apache.eventmesh.api.consumer.Consumer} | 0 ...rg.apache.eventmesh.api.producer.Producer} | 0 ...ache.io.openmessaging.MessagingAccessPoint | 20 - .../standalone/broker/StandaloneBroker.java | 1 - .../broker/model/MessageEntity.java | 1 - .../producer/StandaloneProducer.java | 28 +- .../producer/StandaloneProducerAdaptor.java | 11 +- eventmesh-examples/build.gradle | 1 + .../tcp/common/EventMeshTestUtils.java | 9 +- .../org/apache/eventmesh/runtime/acl/Acl.java | 2 +- .../core/plugin/MQProducerWrapper.java | 1 - .../cloudevent/OMSMessageFactory.java | 99 ---- .../impl/OMSBinaryMessageReader.java | 90 ---- .../protocol/cloudevent/impl/OMSHeaders.java | 44 -- .../cloudevent/impl/OMSMessageWriter.java | 100 ---- .../http/producer/EventMeshProducer.java | 1 - .../client/session/send/SessionSender.java | 1 - .../protocol/tcp/client/task/HelloTask.java | 2 +- .../eventmesh/runtime/util/EventMeshUtil.java | 73 ++- .../EventMeshMessageTCPClient.java | 2 +- .../EventMeshMessageTCPPubClient.java | 2 +- .../EventMeshMessageTCPSubClient.java | 2 +- 37 files changed, 527 insertions(+), 1156 deletions(-) delete mode 100644 eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/DefaultPromise.java rename eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/{org.apache.eventmesh.api.consumer.MeshMQPushConsumer => org.apache.eventmesh.api.consumer.Consumer} (100%) rename eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/{org.apache.eventmesh.api.producer.MeshMQProducer => org.apache.eventmesh.api.producer.Producer} (100%) delete mode 100644 eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.MessagingAccessPoint delete mode 100644 eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/OMSMessageFactory.java delete mode 100644 eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSBinaryMessageReader.java delete mode 100644 eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSHeaders.java delete mode 100644 eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSMessageWriter.java diff --git a/docs/cn/features/spi.md b/docs/cn/features/spi.md index b990d5956e..7499e655b1 100644 --- a/docs/cn/features/spi.md +++ b/docs/cn/features/spi.md @@ -101,7 +101,7 @@ public class RocketMQProducerImpl implements MeshMQProducer { ``` 同时,还需要在eventmesh-connector-rocketmq模块中resource/META-INF/eventmesh目录下创建文件名为SPI接口全限定名的文件 -org.apache.eventmesh.api.producer.MeshMQProducer +org.apache.eventmesh.api.producer.Producer 文件内容为扩展实例名和对应的实例全类名 diff --git a/docs/en/features/spi.md b/docs/en/features/spi.md index 1c26d17ed5..af1d62725e 100644 --- a/docs/en/features/spi.md +++ b/docs/en/features/spi.md @@ -105,7 +105,7 @@ public class RocketMQProducerImpl implements MeshMQProducer { At the same time, we need to create a file with the full qualified name of the SPI interface under the resource/META-INF/eventmesh directory in the eventmesh-connector-rocketmq module. -org.apache.eventmesh.api.producer.MeshMQProducer +org.apache.eventmesh.api.producer.Producer The content of the file is the extension instance name and the corresponding instance full class name diff --git a/eventmesh-connector-plugin/eventmesh-connector-api/build.gradle b/eventmesh-connector-plugin/eventmesh-connector-api/build.gradle index 81d1568ec0..3c2c9ede9a 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-api/build.gradle +++ b/eventmesh-connector-plugin/eventmesh-connector-api/build.gradle @@ -19,7 +19,7 @@ dependencies { implementation project(":eventmesh-spi") implementation project(":eventmesh-common") api 'io.cloudevents:cloudevents-core' - api 'io.openmessaging:openmessaging-api' +// api 'io.openmessaging:openmessaging-api' api 'io.dropwizard.metrics:metrics-core' api "io.dropwizard.metrics:metrics-healthchecks" api "io.dropwizard.metrics:metrics-annotation" diff --git a/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/RRCallback.java b/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/RRCallback.java index 788f9f3906..eba4acdbaa 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/RRCallback.java +++ b/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/RRCallback.java @@ -1,28 +1,28 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.api; - -import io.openmessaging.api.Message; - -public interface RRCallback { - - public void onSuccess(Message msg); - - public void onException(Throwable e); - -} +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +// +//package org.apache.eventmesh.api; +// +//import io.openmessaging.api.Message; +// +//public interface RRCallback { +// +// public void onSuccess(Message msg); +// +// public void onException(Throwable e); +// +//} diff --git a/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/consumer/MeshMQPushConsumer.java b/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/consumer/MeshMQPushConsumer.java index dcf558b685..8747cd9945 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/consumer/MeshMQPushConsumer.java +++ b/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/consumer/MeshMQPushConsumer.java @@ -1,44 +1,40 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.api.consumer; - -import java.util.List; -import java.util.Properties; - -import io.openmessaging.api.AsyncMessageListener; -import io.openmessaging.api.Consumer; -import io.openmessaging.api.Message; - -import org.apache.eventmesh.api.AbstractContext; -import org.apache.eventmesh.spi.EventMeshExtensionType; -import org.apache.eventmesh.spi.EventMeshSPI; - -@EventMeshSPI(isSingleton = false, eventMeshExtensionType = EventMeshExtensionType.CONNECTOR) -public interface MeshMQPushConsumer extends Consumer { - - void init(Properties keyValue) throws Exception; - - void updateOffset(List msgs, AbstractContext context); - -// void registerMessageListener(MessageListenerConcurrently messageListenerConcurrently); - - void subscribe(String topic, final AsyncMessageListener listener) throws Exception; - - @Override - void unsubscribe(String topic); -} +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +// +//package org.apache.eventmesh.api.consumer; +// +//import java.util.List; +//import java.util.Properties; +// +//import org.apache.eventmesh.api.AbstractContext; +//import org.apache.eventmesh.spi.EventMeshExtensionType; +//import org.apache.eventmesh.spi.EventMeshSPI; +// +//@EventMeshSPI(isSingleton = false, eventMeshExtensionType = EventMeshExtensionType.CONNECTOR) +//public interface MeshMQPushConsumer extends Consumer { +// +// void init(Properties keyValue) throws Exception; +// +// void updateOffset(List msgs, AbstractContext context); +// +//// void registerMessageListener(MessageListenerConcurrently messageListenerConcurrently); +// +// void subscribe(String topic, final AsyncMessageListener listener) throws Exception; +// +// @Override +// void unsubscribe(String topic); +//} diff --git a/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/MeshMQProducer.java b/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/MeshMQProducer.java index d87be7d842..e44bd50a8e 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/MeshMQProducer.java +++ b/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/MeshMQProducer.java @@ -1,45 +1,45 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.api.producer; - -import java.util.Properties; - -import io.openmessaging.api.Message; -import io.openmessaging.api.Producer; -import io.openmessaging.api.SendCallback; - -import org.apache.eventmesh.api.RRCallback; -import org.apache.eventmesh.spi.EventMeshExtensionType; -import org.apache.eventmesh.spi.EventMeshSPI; - -@EventMeshSPI(isSingleton = false, eventMeshExtensionType = EventMeshExtensionType.CONNECTOR) -public interface MeshMQProducer extends Producer { - - void init(Properties properties) throws Exception; - - void send(Message message, SendCallback sendCallback) throws Exception; - - void request(Message message, RRCallback rrCallback, long timeout) throws Exception; - - boolean reply(final Message message, final SendCallback sendCallback) throws Exception; - - void checkTopicExist(String topic) throws Exception; - - void setExtFields(); - -} +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +// +//package org.apache.eventmesh.api.producer; +// +//import java.util.Properties; +// +//import io.openmessaging.api.Message; +//import io.openmessaging.api.Producer; +//import io.openmessaging.api.SendCallback; +// +//import org.apache.eventmesh.api.RRCallback; +//import org.apache.eventmesh.spi.EventMeshExtensionType; +//import org.apache.eventmesh.spi.EventMeshSPI; +// +//@EventMeshSPI(isSingleton = false, eventMeshExtensionType = EventMeshExtensionType.CONNECTOR) +//public interface MeshMQProducer extends Producer { +// +// void init(Properties properties) throws Exception; +// +// void send(Message message, SendCallback sendCallback) throws Exception; +// +// void request(Message message, RRCallback rrCallback, long timeout) throws Exception; +// +// boolean reply(final Message message, final SendCallback sendCallback) throws Exception; +// +// void checkTopicExist(String topic) throws Exception; +// +// void setExtFields(); +// +//} diff --git a/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/Producer.java b/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/Producer.java index 7a9e0320e5..5f14582eac 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/Producer.java +++ b/eventmesh-connector-plugin/eventmesh-connector-api/src/main/java/org/apache/eventmesh/api/producer/Producer.java @@ -18,7 +18,6 @@ package org.apache.eventmesh.api.producer; import org.apache.eventmesh.api.LifeCycle; -import org.apache.eventmesh.api.RRCallback; import org.apache.eventmesh.api.RequestReplyCallback; import org.apache.eventmesh.api.SendCallback; import org.apache.eventmesh.api.SendResult; @@ -45,7 +44,7 @@ public interface Producer extends LifeCycle { void sendAsync(final CloudEvent cloudEvent, final SendCallback sendCallback); - void request(CloudEvent cloudEvent, RRCallback rrCallback, long timeout) throws Exception; +// void request(CloudEvent cloudEvent, RRCallback rrCallback, long timeout) throws Exception; void request(CloudEvent cloudEvent, RequestReplyCallback rrCallback, long timeout) throws Exception; diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java index 8770b7d484..06999487d3 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java @@ -1,90 +1,90 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.connector.rocketmq; - -import java.util.Properties; - -import io.openmessaging.api.Consumer; -import io.openmessaging.api.MessagingAccessPoint; -import io.openmessaging.api.Producer; -import io.openmessaging.api.PullConsumer; -import io.openmessaging.api.batch.BatchConsumer; -import io.openmessaging.api.order.OrderConsumer; -import io.openmessaging.api.order.OrderProducer; -import io.openmessaging.api.transaction.LocalTransactionChecker; -import io.openmessaging.api.transaction.TransactionProducer; - -public class MessagingAccessPointImpl implements MessagingAccessPoint { - - private Properties accessPointProperties; - - public MessagingAccessPointImpl(final Properties accessPointProperties) { - this.accessPointProperties = accessPointProperties; - } - - @Override - public String version() { - return null; - } - - @Override - public Properties attributes() { - return accessPointProperties; - } - - @Override - public Producer createProducer(Properties properties) { - return null; - } - - @Override - public OrderProducer createOrderProducer(Properties properties) { - return null; - } - - @Override - public TransactionProducer createTransactionProducer(Properties properties, LocalTransactionChecker checker) { - return null; - } - - @Override - public TransactionProducer createTransactionProducer(Properties properties) { - return null; - } - - @Override - public Consumer createConsumer(Properties properties) { - return null; - } - - @Override - public PullConsumer createPullConsumer(Properties properties) { - return null; - } - - @Override - public BatchConsumer createBatchConsumer(Properties properties) { - return null; - } - - @Override - public OrderConsumer createOrderedConsumer(Properties properties) { - return null; - } - -} +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +// +//package org.apache.eventmesh.connector.rocketmq; +// +//import java.util.Properties; +// +//import io.openmessaging.api.Consumer; +//import io.openmessaging.api.MessagingAccessPoint; +//import io.openmessaging.api.Producer; +//import io.openmessaging.api.PullConsumer; +//import io.openmessaging.api.batch.BatchConsumer; +//import io.openmessaging.api.order.OrderConsumer; +//import io.openmessaging.api.order.OrderProducer; +//import io.openmessaging.api.transaction.LocalTransactionChecker; +//import io.openmessaging.api.transaction.TransactionProducer; +// +//public class MessagingAccessPointImpl implements MessagingAccessPoint { +// +// private Properties accessPointProperties; +// +// public MessagingAccessPointImpl(final Properties accessPointProperties) { +// this.accessPointProperties = accessPointProperties; +// } +// +// @Override +// public String version() { +// return null; +// } +// +// @Override +// public Properties attributes() { +// return accessPointProperties; +// } +// +// @Override +// public Producer createProducer(Properties properties) { +// return null; +// } +// +// @Override +// public OrderProducer createOrderProducer(Properties properties) { +// return null; +// } +// +// @Override +// public TransactionProducer createTransactionProducer(Properties properties, LocalTransactionChecker checker) { +// return null; +// } +// +// @Override +// public TransactionProducer createTransactionProducer(Properties properties) { +// return null; +// } +// +// @Override +// public Consumer createConsumer(Properties properties) { +// return null; +// } +// +// @Override +// public PullConsumer createPullConsumer(Properties properties) { +// return null; +// } +// +// @Override +// public BatchConsumer createBatchConsumer(Properties properties) { +// return null; +// } +// +// @Override +// public OrderConsumer createOrderedConsumer(Properties properties) { +// return null; +// } +// +//} diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfig.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfig.java index aad0b49a53..f03a38a4a9 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfig.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/config/ClientConfig.java @@ -17,11 +17,10 @@ package org.apache.eventmesh.connector.rocketmq.config; -import io.openmessaging.api.OMSBuiltinKeys; import org.apache.eventmesh.connector.rocketmq.domain.NonStandardKeys; -public class ClientConfig implements OMSBuiltinKeys, NonStandardKeys { +public class ClientConfig implements NonStandardKeys { private String driverImpl; private String accessPoints; private String namespace; diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java index 7258131c23..dbc07030ea 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java @@ -50,7 +50,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import io.cloudevents.CloudEvent; -import io.openmessaging.api.exception.OMSRuntimeException; public class PushConsumerImpl { private final DefaultMQPushConsumer rocketmqPushConsumer; @@ -105,7 +104,7 @@ public void start() { try { this.rocketmqPushConsumer.start(); } catch (Exception e) { - throw new OMSRuntimeException(e.getMessage()); + throw new ConnectorRuntimeException(e.getMessage()); } } } @@ -137,8 +136,7 @@ public void subscribe(String topic, String subExpression, EventListener listener try { this.rocketmqPushConsumer.subscribe(topic, subExpression); } catch (MQClientException e) { - throw new OMSRuntimeException(-1, - String.format("RocketMQ push consumer can't attach to %s.", topic)); + throw new ConnectorRuntimeException(String.format("RocketMQ push consumer can't attach to %s.", topic)); } } @@ -148,8 +146,7 @@ public void unsubscribe(String topic) { try { this.rocketmqPushConsumer.unsubscribe(topic); } catch (Exception e) { - throw new OMSRuntimeException(-1, - String.format("RocketMQ push consumer fails to unsubscribe topic: %s", topic)); + throw new ConnectorRuntimeException(String.format("RocketMQ push consumer fails to unsubscribe topic: %s", topic)); } } @@ -186,8 +183,7 @@ public EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, EventListener listener = PushConsumerImpl.this.subscribeTable.get(msg.getTopic()); if (listener == null) { - throw new OMSRuntimeException(-1, - String.format("The topic/queue %s isn't attached to this consumer", + throw new ConnectorRuntimeException(String.format("The topic/queue %s isn't attached to this consumer", msg.getTopic())); } @@ -245,8 +241,7 @@ public EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, EventListener listener = PushConsumerImpl.this.subscribeTable.get(msg.getTopic()); if (listener == null) { - throw new OMSRuntimeException(-1, - String.format("The topic/queue %s isn't attached to this consumer", + throw new ConnectorRuntimeException(String.format("The topic/queue %s isn't attached to this consumer", msg.getTopic())); } diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java index 9626e2c0d4..1b3fe482ce 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java @@ -20,22 +20,13 @@ import org.apache.eventmesh.api.AbstractContext; import org.apache.eventmesh.api.EventListener; import org.apache.eventmesh.api.consumer.Consumer; -import org.apache.eventmesh.api.consumer.MeshMQPushConsumer; -import org.apache.eventmesh.connector.rocketmq.MessagingAccessPointImpl; import org.apache.eventmesh.connector.rocketmq.common.Constants; import org.apache.eventmesh.connector.rocketmq.common.EventMeshConstants; import org.apache.eventmesh.connector.rocketmq.config.ClientConfiguration; import org.apache.eventmesh.connector.rocketmq.config.ConfigurationWrapper; -import org.apache.eventmesh.connector.rocketmq.patch.EventMeshConsumeConcurrentlyContext; -import org.apache.eventmesh.connector.rocketmq.utils.OMSUtil; - -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService; -import org.apache.rocketmq.client.impl.consumer.ConsumeMessageService; -import org.apache.rocketmq.common.message.MessageExt; import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; import java.io.File; -import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -43,13 +34,6 @@ import org.slf4j.LoggerFactory; import io.cloudevents.CloudEvent; -import io.openmessaging.api.AsyncGenericMessageListener; -import io.openmessaging.api.AsyncMessageListener; -import io.openmessaging.api.GenericMessageListener; -import io.openmessaging.api.Message; -import io.openmessaging.api.MessageListener; -import io.openmessaging.api.MessageSelector; -import io.openmessaging.api.MessagingAccessPoint; public class RocketMQConsumerImpl implements Consumer { diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/ProducerImpl.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/ProducerImpl.java index ec04b58bed..7831639ad6 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/ProducerImpl.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/ProducerImpl.java @@ -17,7 +17,7 @@ package org.apache.eventmesh.connector.rocketmq.producer; -import org.apache.eventmesh.api.RRCallback; +import org.apache.eventmesh.api.RequestReplyCallback; import org.apache.eventmesh.api.SendCallback; import org.apache.eventmesh.api.SendResult; import org.apache.eventmesh.api.exception.ConnectorRuntimeException; @@ -117,7 +117,7 @@ public void sendAsync(CloudEvent cloudEvent, SendCallback sendCallback) { } } - public void request(CloudEvent cloudEvent, RRCallback rrCallback, long timeout) + public void request(CloudEvent cloudEvent, RequestReplyCallback rrCallback, long timeout) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { this.checkProducerServiceState(this.rocketmqProducer.getDefaultMQProducerImpl()); @@ -142,12 +142,12 @@ public boolean reply(final CloudEvent cloudEvent, final SendCallback sendCallbac } - private RequestCallback rrCallbackConvert(final Message message, final RRCallback rrCallback) { + private RequestCallback rrCallbackConvert(final Message message, final RequestReplyCallback rrCallback) { return new RequestCallback() { @Override public void onSuccess(org.apache.rocketmq.common.message.Message message) { - io.openmessaging.api.Message openMessage = OMSUtil.msgConvert((MessageExt) message); - rrCallback.onSuccess(openMessage); + CloudEvent event = RocketMQMessageFactory.createReader(message).toEvent(); + rrCallback.onSuccess(event); } @Override diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java index 54d06a0bf9..8ab3d969e4 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java @@ -17,12 +17,10 @@ package org.apache.eventmesh.connector.rocketmq.producer; -import org.apache.eventmesh.api.RRCallback; import org.apache.eventmesh.api.RequestReplyCallback; import org.apache.eventmesh.api.SendCallback; import org.apache.eventmesh.api.SendResult; import org.apache.eventmesh.api.producer.Producer; -import org.apache.eventmesh.connector.rocketmq.MessagingAccessPointImpl; import org.apache.eventmesh.connector.rocketmq.common.EventMeshConstants; import org.apache.eventmesh.connector.rocketmq.config.ClientConfiguration; import org.apache.eventmesh.connector.rocketmq.config.ConfigurationWrapper; @@ -38,7 +36,6 @@ import org.slf4j.LoggerFactory; import io.cloudevents.CloudEvent; -import io.openmessaging.api.MessagingAccessPoint; public class RocketMQProducerImpl implements Producer { @@ -94,15 +91,15 @@ public void publish(CloudEvent message, SendCallback sendCallback) throws Except } @Override - public void request(CloudEvent message, RRCallback rrCallback, long timeout) + public void request(CloudEvent message, RequestReplyCallback rrCallback, long timeout) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { producer.request(message, rrCallback, timeout); } - @Override - public void request(CloudEvent cloudEvent, RequestReplyCallback rrCallback, long timeout) throws Exception { - - } +// @Override +// public void request(CloudEvent cloudEvent, RequestReplyCallback rrCallback, long timeout) throws Exception { +// +// } @Override public boolean reply(final CloudEvent message, final SendCallback sendCallback) throws Exception { diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/DefaultPromise.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/DefaultPromise.java deleted file mode 100644 index cc30eb5ab0..0000000000 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/promise/DefaultPromise.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.connector.rocketmq.promise; - -import java.util.ArrayList; -import java.util.List; - -import io.openmessaging.api.Future; -import io.openmessaging.api.FutureListener; -import io.openmessaging.api.exception.OMSRuntimeException; - -import org.apache.rocketmq.logging.InternalLogger; -import org.apache.rocketmq.logging.InternalLoggerFactory; - -public class DefaultPromise implements Future { - private static final InternalLogger LOG = InternalLoggerFactory.getLogger(DefaultPromise.class); - private final Object lock = new Object(); - private volatile FutureState state = FutureState.DOING; - private V result = null; - private long timeout; - private long createTime; - private Throwable exception = null; - private List> promiseListenerList; - - public DefaultPromise() { - createTime = System.currentTimeMillis(); - promiseListenerList = new ArrayList<>(); - timeout = 5000; - } - - @Override - public boolean cancel(final boolean mayInterruptIfRunning) { - return false; - } - - @Override - public boolean isCancelled() { - return state.isCancelledState(); - } - - @Override - public boolean isDone() { - return state.isDoneState(); - } - - @Override - public V get() { - return result; - } - - @Override - public V get(final long timeout) { - synchronized (lock) { - if (!isDoing()) { - return getValueOrThrowable(); - } - - if (timeout <= 0) { - try { - lock.wait(); - } catch (Exception e) { - cancel(e); - } - return getValueOrThrowable(); - } else { - long waitTime = timeout - (System.currentTimeMillis() - createTime); - if (waitTime > 0) { - for (; ; ) { - try { - lock.wait(waitTime); - } catch (InterruptedException e) { - LOG.error("promise get value interrupted,excepiton:{}", e.getMessage()); - } - - if (!isDoing()) { - break; - } else { - waitTime = timeout - (System.currentTimeMillis() - createTime); - if (waitTime <= 0) { - break; - } - } - } - } - - if (isDoing()) { - timeoutSoCancel(); - } - } - return getValueOrThrowable(); - } - } - - public boolean set(final V value) { - if (value == null) { - return false; - } - this.result = value; - return done(); - } - - public boolean setFailure(final Throwable cause) { - if (cause == null) { - return false; - } - this.exception = cause; - return done(); - } - - @Override - public void addListener(final FutureListener listener) { - if (listener == null) { - throw new NullPointerException("FutureListener is null"); - } - - boolean notifyNow = false; - synchronized (lock) { - if (!isDoing()) { - notifyNow = true; - } else { - if (promiseListenerList == null) { - promiseListenerList = new ArrayList<>(); - } - promiseListenerList.add(listener); - } - } - - if (notifyNow) { - notifyListener(listener); - } - } - - @Override - public Throwable getThrowable() { - return exception; - } - - private void notifyListeners() { - if (promiseListenerList != null) { - for (FutureListener listener : promiseListenerList) { - notifyListener(listener); - } - } - } - - private boolean isSuccess() { - return isDone() && (exception == null); - } - - private void timeoutSoCancel() { - synchronized (lock) { - if (!isDoing()) { - return; - } - state = FutureState.CANCELLED; - exception = new RuntimeException("Get request result is timeout or interrupted"); - lock.notifyAll(); - } - notifyListeners(); - } - - private V getValueOrThrowable() { - if (exception != null) { - Throwable e = exception.getCause() != null ? exception.getCause() : exception; - throw new OMSRuntimeException("-1", e); - } - notifyListeners(); - return result; - } - - private boolean isDoing() { - return state.isDoingState(); - } - - private boolean done() { - synchronized (lock) { - if (!isDoing()) { - return false; - } - - state = FutureState.DONE; - lock.notifyAll(); - } - - notifyListeners(); - return true; - } - - private void notifyListener(final FutureListener listener) { - try { - listener.operationComplete(this); - } catch (Throwable t) { - LOG.error("notifyListener {} Error:{}", listener.getClass().getSimpleName(), t); - } - } - - private boolean cancel(Exception e) { - synchronized (lock) { - if (!isDoing()) { - return false; - } - - state = FutureState.CANCELLED; - exception = e; - lock.notifyAll(); - } - - notifyListeners(); - return true; - } -} - diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/OMSUtil.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/OMSUtil.java index 00bcb85f2a..d6a1ae965a 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/OMSUtil.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/OMSUtil.java @@ -24,10 +24,6 @@ import java.util.Properties; import java.util.Set; -import io.openmessaging.api.Message; -import io.openmessaging.api.OMSBuiltinKeys; -import io.openmessaging.api.SendResult; -import io.openmessaging.api.exception.OMSRuntimeException; import org.apache.eventmesh.common.Constants; import org.apache.rocketmq.common.UtilAll; @@ -45,252 +41,252 @@ public static String buildInstanceName() { return Integer.toString(UtilAll.getPid()) + "%OpenMessaging" + "%" + System.nanoTime(); } - public static org.apache.rocketmq.common.message.Message msgConvert(Message omsMessage) { - org.apache.rocketmq.common.message.Message rmqMessage = new org.apache.rocketmq.common.message.Message(); - if (omsMessage == null) { - throw new OMSRuntimeException("'message' is null"); - } else { - if (omsMessage.getTopic() != null) { - rmqMessage.setTopic(omsMessage.getTopic()); - } - if (omsMessage.getKey() != null) { - rmqMessage.setKeys(omsMessage.getKey()); - } - if (omsMessage.getTag() != null) { - rmqMessage.setTags(omsMessage.getTag()); - } - if (omsMessage.getStartDeliverTime() > 0L) { - rmqMessage.putUserProperty("TIMER_DELIVER_MS", String.valueOf(omsMessage.getStartDeliverTime())); - } - - if (omsMessage.getBody() != null) { - rmqMessage.setBody(omsMessage.getBody()); - } - - if (omsMessage.getShardingKey() != null && !omsMessage.getShardingKey().isEmpty()) { - rmqMessage.putUserProperty("__SHARDINGKEY", omsMessage.getShardingKey()); - } - } - Properties systemProperties = omsMessage.getSystemProperties(); - Properties userProperties = omsMessage.getUserProperties(); - - //All destinations in RocketMQ use Topic -// rmqMessage.setTopic(systemProperties.getProperty(BuiltinKeys.DESTINATION)); - -// if (sysHeaders.containsKey(BuiltinKeys.START_TIME)) { -// long deliverTime = sysHeaders.getLong(BuiltinKeys.START_TIME, 0); -// if (deliverTime > 0) { -// rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); +// public static org.apache.rocketmq.common.message.Message msgConvert(Message omsMessage) { +// org.apache.rocketmq.common.message.Message rmqMessage = new org.apache.rocketmq.common.message.Message(); +// if (omsMessage == null) { +// throw new OMSRuntimeException("'message' is null"); +// } else { +// if (omsMessage.getTopic() != null) { +// rmqMessage.setTopic(omsMessage.getTopic()); +// } +// if (omsMessage.getKey() != null) { +// rmqMessage.setKeys(omsMessage.getKey()); +// } +// if (omsMessage.getTag() != null) { +// rmqMessage.setTags(omsMessage.getTag()); +// } +// if (omsMessage.getStartDeliverTime() > 0L) { +// rmqMessage.putUserProperty("TIMER_DELIVER_MS", String.valueOf(omsMessage.getStartDeliverTime())); +// } +// +// if (omsMessage.getBody() != null) { +// rmqMessage.setBody(omsMessage.getBody()); +// } +// +// if (omsMessage.getShardingKey() != null && !omsMessage.getShardingKey().isEmpty()) { +// rmqMessage.putUserProperty("__SHARDINGKEY", omsMessage.getShardingKey()); // } // } - - for (String key : userProperties.stringPropertyNames()) { - MessageAccessor.putProperty(rmqMessage, key, userProperties.getProperty(key)); - } - - //System headers has a high priority - for (String key : systemProperties.stringPropertyNames()) { - MessageAccessor.putProperty(rmqMessage, key, systemProperties.getProperty(key)); - } - - return rmqMessage; - } - - public static Message msgConvert(MessageExt rmqMsg) { - Message message = new Message(); - if (rmqMsg.getTopic() != null) { - message.setTopic(rmqMsg.getTopic()); - } - - if (rmqMsg.getKeys() != null) { - message.setKey(rmqMsg.getKeys()); - } - - if (rmqMsg.getTags() != null) { - message.setTag(rmqMsg.getTags()); - } - - if (rmqMsg.getBody() != null) { - message.setBody(rmqMsg.getBody()); - } - - if (rmqMsg.getUserProperty("TIMER_DELIVER_MS") != null) { - long ms = Long.parseLong(rmqMsg.getUserProperty("TIMER_DELIVER_MS")); - rmqMsg.getProperties().remove("TIMER_DELIVER_MS"); - message.setStartDeliverTime(ms); - } - - Properties systemProperties = new Properties(); - Properties userProperties = new Properties(); - - - final Set> entries = rmqMsg.getProperties().entrySet(); - - for (final Map.Entry entry : entries) { - if (isOMSHeader(entry.getKey())) { - //sysHeader - systemProperties.put(entry.getKey(), entry.getValue()); - } else { - //userHeader - userProperties.put(entry.getKey(), entry.getValue()); - } - } - - if (rmqMsg.getMsgId() != null){ - systemProperties.put(Constants.PROPERTY_MESSAGE_MESSAGE_ID, rmqMsg.getMsgId()); - } - - if (rmqMsg.getTopic() != null){ - systemProperties.put(Constants.PROPERTY_MESSAGE_DESTINATION, rmqMsg.getTopic()); - } - -// omsMsg.putSysHeaders(BuiltinKeys.SEARCH_KEYS, rmqMsg.getKeys()); - systemProperties.put(Constants.PROPERTY_MESSAGE_BORN_HOST, String.valueOf(rmqMsg.getBornHost())); - systemProperties.put(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, rmqMsg.getBornTimestamp()); - systemProperties.put(Constants.PROPERTY_MESSAGE_STORE_HOST, String.valueOf(rmqMsg.getStoreHost())); - systemProperties.put("STORE_TIMESTAMP", rmqMsg.getStoreTimestamp()); - - //use in manual ack - userProperties.put(Constants.PROPERTY_MESSAGE_QUEUE_ID, rmqMsg.getQueueId()); - userProperties.put(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET, rmqMsg.getQueueOffset()); - - message.setSystemProperties(systemProperties); - message.setUserProperties(userProperties); - - return message; - } - - public static org.apache.rocketmq.common.message.MessageExt msgConvertExt(Message omsMessage) { - - org.apache.rocketmq.common.message.MessageExt rmqMessageExt = new org.apache.rocketmq.common.message.MessageExt(); - try { - if (omsMessage.getKey() != null) { - rmqMessageExt.setKeys(omsMessage.getKey()); - } - if (omsMessage.getTag() != null) { - rmqMessageExt.setTags(omsMessage.getTag()); - } - if (omsMessage.getStartDeliverTime() > 0L) { - rmqMessageExt.putUserProperty("TIMER_DELIVER_MS", String.valueOf(omsMessage.getStartDeliverTime())); - } - - if (omsMessage.getBody() != null) { - rmqMessageExt.setBody(omsMessage.getBody()); - } - - if (omsMessage.getShardingKey() != null && !omsMessage.getShardingKey().isEmpty()) { - rmqMessageExt.putUserProperty("__SHARDINGKEY", omsMessage.getShardingKey()); - } - - Properties systemProperties = omsMessage.getSystemProperties(); - Properties userProperties = omsMessage.getUserProperties(); - - //All destinations in RocketMQ use Topic - rmqMessageExt.setTopic(omsMessage.getTopic()); - - int queueId = (int) userProperties.get(Constants.PROPERTY_MESSAGE_QUEUE_ID); - long queueOffset = (long) userProperties.get(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET); - //use in manual ack - rmqMessageExt.setQueueId(queueId); - rmqMessageExt.setQueueOffset(queueOffset); - - for (String key : userProperties.stringPropertyNames()) { - MessageAccessor.putProperty(rmqMessageExt, key, userProperties.getProperty(key)); - } - - //System headers has a high priority - for (String key : systemProperties.stringPropertyNames()) { - MessageAccessor.putProperty(rmqMessageExt, key, systemProperties.getProperty(key)); - } - - } catch (Exception e) { - e.printStackTrace(); - } - return rmqMessageExt; - - } - - public static boolean isOMSHeader(String value) { - for (Field field : OMSBuiltinKeys.class.getDeclaredFields()) { - try { - if (field.get(OMSBuiltinKeys.class).equals(value)) { - return true; - } - } catch (IllegalAccessException e) { - return false; - } - } - return false; - } - - /** - * Convert a RocketMQ SEND_OK SendResult instance to a OMS SendResult. - * - * @param rmqResult RocketMQ result - * @return send result - */ - public static SendResult sendResultConvert(org.apache.rocketmq.client.producer.SendResult rmqResult) { - SendResult sendResult = new SendResult(); - sendResult.setTopic(rmqResult.getMessageQueue().getTopic()); - sendResult.setMessageId(rmqResult.getMsgId()); - return sendResult; - } - -// public static KeyValue buildKeyValue(KeyValue... keyValues) { -// KeyValue keyValue = OMS.newKeyValue(); -// for (KeyValue properties : keyValues) { -// for (String key : properties.keySet()) { -// keyValue.put(key, properties.getString(key)); +// Properties systemProperties = omsMessage.getSystemProperties(); +// Properties userProperties = omsMessage.getUserProperties(); +// +// //All destinations in RocketMQ use Topic +//// rmqMessage.setTopic(systemProperties.getProperty(BuiltinKeys.DESTINATION)); +// +//// if (sysHeaders.containsKey(BuiltinKeys.START_TIME)) { +//// long deliverTime = sysHeaders.getLong(BuiltinKeys.START_TIME, 0); +//// if (deliverTime > 0) { +//// rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); +//// } +//// } +// +// for (String key : userProperties.stringPropertyNames()) { +// MessageAccessor.putProperty(rmqMessage, key, userProperties.getProperty(key)); +// } +// +// //System headers has a high priority +// for (String key : systemProperties.stringPropertyNames()) { +// MessageAccessor.putProperty(rmqMessage, key, systemProperties.getProperty(key)); +// } +// +// return rmqMessage; +// } +// +// public static Message msgConvert(MessageExt rmqMsg) { +// Message message = new Message(); +// if (rmqMsg.getTopic() != null) { +// message.setTopic(rmqMsg.getTopic()); +// } +// +// if (rmqMsg.getKeys() != null) { +// message.setKey(rmqMsg.getKeys()); +// } +// +// if (rmqMsg.getTags() != null) { +// message.setTag(rmqMsg.getTags()); +// } +// +// if (rmqMsg.getBody() != null) { +// message.setBody(rmqMsg.getBody()); +// } +// +// if (rmqMsg.getUserProperty("TIMER_DELIVER_MS") != null) { +// long ms = Long.parseLong(rmqMsg.getUserProperty("TIMER_DELIVER_MS")); +// rmqMsg.getProperties().remove("TIMER_DELIVER_MS"); +// message.setStartDeliverTime(ms); +// } +// +// Properties systemProperties = new Properties(); +// Properties userProperties = new Properties(); +// +// +// final Set> entries = rmqMsg.getProperties().entrySet(); +// +// for (final Map.Entry entry : entries) { +// if (isOMSHeader(entry.getKey())) { +// //sysHeader +// systemProperties.put(entry.getKey(), entry.getValue()); +// } else { +// //userHeader +// userProperties.put(entry.getKey(), entry.getValue()); // } // } -// return keyValue; +// +// if (rmqMsg.getMsgId() != null){ +// systemProperties.put(Constants.PROPERTY_MESSAGE_MESSAGE_ID, rmqMsg.getMsgId()); +// } +// +// if (rmqMsg.getTopic() != null){ +// systemProperties.put(Constants.PROPERTY_MESSAGE_DESTINATION, rmqMsg.getTopic()); +// } +// +//// omsMsg.putSysHeaders(BuiltinKeys.SEARCH_KEYS, rmqMsg.getKeys()); +// systemProperties.put(Constants.PROPERTY_MESSAGE_BORN_HOST, String.valueOf(rmqMsg.getBornHost())); +// systemProperties.put(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, rmqMsg.getBornTimestamp()); +// systemProperties.put(Constants.PROPERTY_MESSAGE_STORE_HOST, String.valueOf(rmqMsg.getStoreHost())); +// systemProperties.put("STORE_TIMESTAMP", rmqMsg.getStoreTimestamp()); +// +// //use in manual ack +// userProperties.put(Constants.PROPERTY_MESSAGE_QUEUE_ID, rmqMsg.getQueueId()); +// userProperties.put(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET, rmqMsg.getQueueOffset()); +// +// message.setSystemProperties(systemProperties); +// message.setUserProperties(userProperties); +// +// return message; +// } +// +// public static org.apache.rocketmq.common.message.MessageExt msgConvertExt(Message omsMessage) { +// +// org.apache.rocketmq.common.message.MessageExt rmqMessageExt = new org.apache.rocketmq.common.message.MessageExt(); +// try { +// if (omsMessage.getKey() != null) { +// rmqMessageExt.setKeys(omsMessage.getKey()); +// } +// if (omsMessage.getTag() != null) { +// rmqMessageExt.setTags(omsMessage.getTag()); +// } +// if (omsMessage.getStartDeliverTime() > 0L) { +// rmqMessageExt.putUserProperty("TIMER_DELIVER_MS", String.valueOf(omsMessage.getStartDeliverTime())); +// } +// +// if (omsMessage.getBody() != null) { +// rmqMessageExt.setBody(omsMessage.getBody()); +// } +// +// if (omsMessage.getShardingKey() != null && !omsMessage.getShardingKey().isEmpty()) { +// rmqMessageExt.putUserProperty("__SHARDINGKEY", omsMessage.getShardingKey()); +// } +// +// Properties systemProperties = omsMessage.getSystemProperties(); +// Properties userProperties = omsMessage.getUserProperties(); +// +// //All destinations in RocketMQ use Topic +// rmqMessageExt.setTopic(omsMessage.getTopic()); +// +// int queueId = (int) userProperties.get(Constants.PROPERTY_MESSAGE_QUEUE_ID); +// long queueOffset = (long) userProperties.get(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET); +// //use in manual ack +// rmqMessageExt.setQueueId(queueId); +// rmqMessageExt.setQueueOffset(queueOffset); +// +// for (String key : userProperties.stringPropertyNames()) { +// MessageAccessor.putProperty(rmqMessageExt, key, userProperties.getProperty(key)); +// } +// +// //System headers has a high priority +// for (String key : systemProperties.stringPropertyNames()) { +// MessageAccessor.putProperty(rmqMessageExt, key, systemProperties.getProperty(key)); +// } +// +// } catch (Exception e) { +// e.printStackTrace(); +// } +// return rmqMessageExt; +// +// } +// +// public static boolean isOMSHeader(String value) { +// for (Field field : OMSBuiltinKeys.class.getDeclaredFields()) { +// try { +// if (field.get(OMSBuiltinKeys.class).equals(value)) { +// return true; +// } +// } catch (IllegalAccessException e) { +// return false; +// } +// } +// return false; +// } +// +// /** +// * Convert a RocketMQ SEND_OK SendResult instance to a OMS SendResult. +// * +// * @param rmqResult RocketMQ result +// * @return send result +// */ +// public static SendResult sendResultConvert(org.apache.rocketmq.client.producer.SendResult rmqResult) { +// SendResult sendResult = new SendResult(); +// sendResult.setTopic(rmqResult.getMessageQueue().getTopic()); +// sendResult.setMessageId(rmqResult.getMsgId()); +// return sendResult; +// } +// +//// public static KeyValue buildKeyValue(KeyValue... keyValues) { +//// KeyValue keyValue = OMS.newKeyValue(); +//// for (KeyValue properties : keyValues) { +//// for (String key : properties.keySet()) { +//// keyValue.put(key, properties.getString(key)); +//// } +//// } +//// return keyValue; +//// } +// +// /** +// * Returns an iterator that cycles indefinitely over the elements of {@code Iterable}. +// * +// * @param Target type +// * @return Iterator +// */ +// public static Iterator cycle(final Iterable iterable) { +// return new Iterator() { +// Iterator iterator = new Iterator() { +// @Override +// public synchronized boolean hasNext() { +// return false; +// } +// +// @Override +// public synchronized T next() { +// throw new NoSuchElementException(); +// } +// +// @Override +// public synchronized void remove() { +// //Ignore +// } +// }; +// +// @Override +// public synchronized boolean hasNext() { +// return iterator.hasNext() || iterable.iterator().hasNext(); +// } +// +// @Override +// public synchronized T next() { +// if (!iterator.hasNext()) { +// iterator = iterable.iterator(); +// if (!iterator.hasNext()) { +// throw new NoSuchElementException(); +// } +// } +// return iterator.next(); +// } +// +// @Override +// public synchronized void remove() { +// iterator.remove(); +// } +// }; // } - - /** - * Returns an iterator that cycles indefinitely over the elements of {@code Iterable}. - * - * @param Target type - * @return Iterator - */ - public static Iterator cycle(final Iterable iterable) { - return new Iterator() { - Iterator iterator = new Iterator() { - @Override - public synchronized boolean hasNext() { - return false; - } - - @Override - public synchronized T next() { - throw new NoSuchElementException(); - } - - @Override - public synchronized void remove() { - //Ignore - } - }; - - @Override - public synchronized boolean hasNext() { - return iterator.hasNext() || iterable.iterator().hasNext(); - } - - @Override - public synchronized T next() { - if (!iterator.hasNext()) { - iterator = iterable.iterator(); - if (!iterator.hasNext()) { - throw new NoSuchElementException(); - } - } - return iterator.next(); - } - - @Override - public synchronized void remove() { - iterator.remove(); - } - }; - } } diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.api.consumer.MeshMQPushConsumer b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.api.consumer.Consumer similarity index 100% rename from eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.api.consumer.MeshMQPushConsumer rename to eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.api.consumer.Consumer diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.api.producer.MeshMQProducer b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.api.producer.Producer similarity index 100% rename from eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.api.producer.MeshMQProducer rename to eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.api.producer.Producer diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.MessagingAccessPoint b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.MessagingAccessPoint deleted file mode 100644 index e326c919c7..0000000000 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/test/resources/META-INF/services/org.apache.io.openmessaging.MessagingAccessPoint +++ /dev/null @@ -1,20 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -org.apache.eventmesh.connector.rocketmq.MessagingAccessPointImpl \ No newline at end of file diff --git a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/StandaloneBroker.java b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/StandaloneBroker.java index 72a9b58e65..99135aa7e8 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/StandaloneBroker.java +++ b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/StandaloneBroker.java @@ -18,7 +18,6 @@ package org.apache.eventmesh.connector.standalone.broker; import io.cloudevents.CloudEvent; -import io.openmessaging.api.Message; import org.apache.commons.lang3.tuple.Pair; import org.apache.eventmesh.connector.standalone.broker.model.MessageEntity; import org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata; diff --git a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/model/MessageEntity.java b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/model/MessageEntity.java index 968053aba9..dd7468f03b 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/model/MessageEntity.java +++ b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/model/MessageEntity.java @@ -18,7 +18,6 @@ package org.apache.eventmesh.connector.standalone.broker.model; import io.cloudevents.CloudEvent; -import io.openmessaging.api.Message; import java.io.Serializable; diff --git a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/producer/StandaloneProducer.java b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/producer/StandaloneProducer.java index 57c8a2a749..b2a6122d91 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/producer/StandaloneProducer.java +++ b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/producer/StandaloneProducer.java @@ -17,7 +17,6 @@ package org.apache.eventmesh.connector.standalone.producer; -import org.apache.eventmesh.api.RRCallback; import org.apache.eventmesh.api.RequestReplyCallback; import org.apache.eventmesh.api.SendCallback; import org.apache.eventmesh.api.SendResult; @@ -37,7 +36,7 @@ import io.cloudevents.CloudEvent; -public class StandaloneProducer implements Producer { +public class StandaloneProducer { private Logger logger = LoggerFactory.getLogger(StandaloneProducer.class); @@ -50,32 +49,26 @@ public StandaloneProducer(Properties properties) { this.isStarted = new AtomicBoolean(false); } - @Override public boolean isStarted() { return isStarted.get(); } - @Override public boolean isClosed() { return !isStarted.get(); } - @Override public void start() { isStarted.compareAndSet(false, true); } - @Override public void shutdown() { isStarted.compareAndSet(true, false); } - @Override - public void init(Properties properties) throws Exception { - + public StandaloneProducer init(Properties properties) throws Exception { + return new StandaloneProducer(properties); } - @Override public SendResult publish(CloudEvent cloudEvent) { Preconditions.checkNotNull(cloudEvent); try { @@ -91,7 +84,6 @@ public SendResult publish(CloudEvent cloudEvent) { } } - @Override public void publish(CloudEvent cloudEvent, SendCallback sendCallback) throws Exception { Preconditions.checkNotNull(cloudEvent); Preconditions.checkNotNull(sendCallback); @@ -108,12 +100,10 @@ public void publish(CloudEvent cloudEvent, SendCallback sendCallback) throws Exc } } - @Override public void sendOneway(CloudEvent cloudEvent) { publish(cloudEvent); } - @Override public void sendAsync(CloudEvent cloudEvent, SendCallback sendCallback) { Preconditions.checkNotNull(cloudEvent); Preconditions.checkNotNull(sendCallback); @@ -130,22 +120,19 @@ public void sendAsync(CloudEvent cloudEvent, SendCallback sendCallback) { } } - @Override - public void request(CloudEvent cloudEvent, RRCallback rrCallback, long timeout) throws Exception { - throw new ConnectorRuntimeException("Request is not supported"); - } +// @Override +// public void request(CloudEvent cloudEvent, RequestReplyCallback rrCallback, long timeout) throws Exception { +// throw new ConnectorRuntimeException("Request is not supported"); +// } - @Override public void request(CloudEvent cloudEvent, RequestReplyCallback rrCallback, long timeout) throws Exception { throw new ConnectorRuntimeException("Request is not supported"); } - @Override public boolean reply(CloudEvent cloudEvent, SendCallback sendCallback) throws Exception { throw new ConnectorRuntimeException("Reply is not supported"); } - @Override public void checkTopicExist(String topic) throws Exception { boolean exist = standaloneBroker.checkTopicExist(topic); if (!exist) { @@ -153,7 +140,6 @@ public void checkTopicExist(String topic) throws Exception { } } - @Override public void setExtFields() { } diff --git a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/producer/StandaloneProducerAdaptor.java b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/producer/StandaloneProducerAdaptor.java index d60968cafc..8cff7e4f82 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/producer/StandaloneProducerAdaptor.java +++ b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/producer/StandaloneProducerAdaptor.java @@ -17,7 +17,6 @@ package org.apache.eventmesh.connector.standalone.producer; -import org.apache.eventmesh.api.RRCallback; import org.apache.eventmesh.api.RequestReplyCallback; import org.apache.eventmesh.api.SendCallback; import org.apache.eventmesh.api.SendResult; @@ -61,7 +60,7 @@ public void shutdown() { @Override public void init(Properties properties) throws Exception { - standaloneProducer.init(properties); + standaloneProducer = new StandaloneProducer(properties); } @Override @@ -84,10 +83,10 @@ public void sendAsync(CloudEvent cloudEvent, SendCallback sendCallback) { standaloneProducer.sendAsync(cloudEvent, sendCallback); } - @Override - public void request(CloudEvent cloudEvent, RRCallback rrCallback, long timeout) throws Exception { - standaloneProducer.request(cloudEvent, rrCallback, timeout); - } +// @Override +// public void request(CloudEvent cloudEvent, RRCallback rrCallback, long timeout) throws Exception { +// standaloneProducer.request(cloudEvent, rrCallback, timeout); +// } @Override public void request(CloudEvent cloudEvent, RequestReplyCallback rrCallback, long timeout) throws Exception { diff --git a/eventmesh-examples/build.gradle b/eventmesh-examples/build.gradle index 3cb42bce3e..6e57ad6722 100644 --- a/eventmesh-examples/build.gradle +++ b/eventmesh-examples/build.gradle @@ -26,6 +26,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'io.netty:netty-all' implementation "io.cloudevents:cloudevents-core" + implementation "io.openmessaging:openmessaging-api" compileOnly 'org.projectlombok:lombok:1.18.22' annotationProcessor 'org.projectlombok:lombok:1.18.22' diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java index bd7f7cbcaa..d6f30e8502 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java @@ -22,6 +22,7 @@ import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientBroadCast; import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientUniCast; +import org.apache.eventmesh.client.tcp.common.MessageUtils; import org.apache.eventmesh.common.protocol.tcp.Command; import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; import org.apache.eventmesh.common.protocol.tcp.Header; @@ -33,8 +34,9 @@ public class EventMeshTestUtils { private static final int seqLength = 10; + // generate pub-client public static UserAgent generateClient1() { - return UserAgent.builder() + UserAgent agent = UserAgent.builder() .env("test") .host("127.0.0.1") .password(generateRandomString(8)) @@ -48,10 +50,12 @@ public static UserAgent generateClient1() { .version("2.0.11") .idc("FT") .build(); + return MessageUtils.generatePubClient(agent); } + // generate sub-client public static UserAgent generateClient2() { - return UserAgent.builder() + UserAgent agent = UserAgent.builder() .env("test") .host("127.0.0.1") .password(generateRandomString(8)) @@ -65,6 +69,7 @@ public static UserAgent generateClient2() { .version("2.0.11") .idc("FT") .build(); + return MessageUtils.generateSubClient(agent); } public static Package syncRR() { diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/acl/Acl.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/acl/Acl.java index cb773f4eb4..bf762ebd92 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/acl/Acl.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/acl/Acl.java @@ -20,7 +20,7 @@ import org.apache.eventmesh.api.acl.AclPropertyKeys; import org.apache.eventmesh.api.acl.AclService; import org.apache.eventmesh.api.exception.AclException; -import org.apache.eventmesh.api.producer.MeshMQProducer; +//import org.apache.eventmesh.api.producer.MeshMQProducer; import org.apache.eventmesh.common.protocol.tcp.UserAgent; import org.apache.eventmesh.spi.EventMeshExtensionFactory; import org.slf4j.Logger; diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQProducerWrapper.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQProducerWrapper.java index 58b3151578..f781071126 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQProducerWrapper.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/plugin/MQProducerWrapper.java @@ -19,7 +19,6 @@ import java.util.Properties; -import org.apache.eventmesh.api.RRCallback; import org.apache.eventmesh.api.RequestReplyCallback; import org.apache.eventmesh.api.SendCallback; import org.apache.eventmesh.api.factory.ConnectorPluginFactory; diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/OMSMessageFactory.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/OMSMessageFactory.java deleted file mode 100644 index fcb2026fb2..0000000000 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/OMSMessageFactory.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.runtime.core.protocol.cloudevent; - -import io.cloudevents.core.message.MessageReader; -import io.cloudevents.core.message.MessageWriter; -import io.cloudevents.core.message.impl.GenericStructuredMessageReader; -import io.cloudevents.core.message.impl.MessageUtils; -import io.cloudevents.lang.Nullable; -import io.cloudevents.rw.CloudEventRWException; -import io.cloudevents.rw.CloudEventWriter; -import io.openmessaging.api.Message; -import org.apache.eventmesh.runtime.core.protocol.cloudevent.impl.OMSBinaryMessageReader; -import org.apache.eventmesh.runtime.core.protocol.cloudevent.impl.OMSHeaders; -import org.apache.eventmesh.runtime.core.protocol.cloudevent.impl.OMSMessageWriter; - -import javax.annotation.ParametersAreNonnullByDefault; -import java.util.Properties; - -/** - * This class provides a collection of methods to create {@link io.cloudevents.core.message.MessageReader} - * and {@link io.cloudevents.core.message.MessageWriter} - * manually serialize/deserialize {@link io.cloudevents.CloudEvent} messages. - */ -@ParametersAreNonnullByDefault -public final class OMSMessageFactory { - - private OMSMessageFactory() { - // prevent instantiation - } - - /** - * create reader by message - * @param message - * @return - * @throws CloudEventRWException - */ - public static MessageReader createReader(final Message message) throws CloudEventRWException { - return createReader(message.getUserProperties(), message.getBody()); - } - - - public static MessageReader createReader(final Properties props, @Nullable final byte[] body) throws CloudEventRWException { - - return MessageUtils.parseStructuredOrBinaryMessage( - () -> props.getOrDefault(OMSHeaders.CONTENT_TYPE,"").toString(), - format -> new GenericStructuredMessageReader(format, body), - () -> props.getOrDefault(OMSHeaders.SPEC_VERSION,"").toString(), - sv -> new OMSBinaryMessageReader(sv, props, body) - ); - } - - - /** - * create writer by topic - * @param topic - * @return - */ - public static MessageWriter, Message> createWriter(String topic) { - return new OMSMessageWriter<>(topic); - } - - /** - * create writer by topic,keys - * @param topic - * @param keys - * @return - */ - public static MessageWriter, Message> createWriter(String topic, String keys) { - return new OMSMessageWriter<>(topic, keys); - } - - /** - * create writer by topic,keys,tags - * @param topic - * @param keys - * @param tags - * @return - */ - public static MessageWriter, Message> createWriter(String topic, String keys, String tags) { - return new OMSMessageWriter<>(topic, keys, tags); - } - -} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSBinaryMessageReader.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSBinaryMessageReader.java deleted file mode 100644 index ae035c7be8..0000000000 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSBinaryMessageReader.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.runtime.core.protocol.cloudevent.impl; - -import io.cloudevents.SpecVersion; -import io.cloudevents.core.data.BytesCloudEventData; -import io.cloudevents.core.message.impl.BaseGenericBinaryMessageReaderImpl; - -import java.util.Objects; -import java.util.Properties; -import java.util.function.BiConsumer; - -/** - * binary message reader - */ -public class OMSBinaryMessageReader extends BaseGenericBinaryMessageReaderImpl { - - private final Properties headers; - - public OMSBinaryMessageReader(SpecVersion version, Properties headers, byte[] payload) { - super(version, payload != null && payload.length > 0 ? BytesCloudEventData.wrap(payload) : null); - - Objects.requireNonNull(headers); - this.headers = headers; - } - - /** - * whether header key is content type - * @param key - * @return - */ - @Override - protected boolean isContentTypeHeader(String key) { - return key.equals(OMSHeaders.CONTENT_TYPE); - } - - /** - * whether message header is cloudEvent header - * @param key - * @return - */ - @Override - protected boolean isCloudEventsHeader(String key) { - return key.length() > 3 && key.substring(0, OMSHeaders.CE_PREFIX.length()).startsWith(OMSHeaders.CE_PREFIX); - } - - /** - * parse message header to cloudEvent attribute - * @param key - * @return - */ - @Override - protected String toCloudEventsKey(String key) { - return key.substring(OMSHeaders.CE_PREFIX.length()).toLowerCase(); - } - - /** - * - * @param fn - */ - @Override - protected void forEachHeader(BiConsumer fn) { - this.headers.forEach((k, v) -> { - if (k != null && v != null) { - fn.accept(k.toString(), v.toString()); - } - - }); - } - - @Override - protected String toCloudEventsValue(String value) { - return value; - } -} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSHeaders.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSHeaders.java deleted file mode 100644 index 7747175165..0000000000 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSHeaders.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.eventmesh.runtime.core.protocol.cloudevent.impl; - -import io.cloudevents.core.message.impl.MessageUtils; -import io.cloudevents.core.v1.CloudEventV1; - -import java.util.Map; - -/** - * Define the value of CE attribute in the header of ons - */ -public class OMSHeaders { - - /** - * CE prefix - */ - public static final String CE_PREFIX = "ce_"; - - /** - * Prefix each value - */ - protected static final Map ATTRIBUTES_TO_HEADERS = MessageUtils.generateAttributesToHeadersMapping(v -> CE_PREFIX + v); - - public static final String CONTENT_TYPE = ATTRIBUTES_TO_HEADERS.get(CloudEventV1.DATACONTENTTYPE); - - public static final String SPEC_VERSION = ATTRIBUTES_TO_HEADERS.get(CloudEventV1.SPECVERSION); - -} - diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSMessageWriter.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSMessageWriter.java deleted file mode 100644 index 0d74331a06..0000000000 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/cloudevent/impl/OMSMessageWriter.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.runtime.core.protocol.cloudevent.impl; - -import io.cloudevents.CloudEventData; -import io.cloudevents.SpecVersion; -import io.cloudevents.core.format.EventFormat; -import io.cloudevents.core.message.MessageWriter; -import io.cloudevents.rw.CloudEventContextWriter; -import io.cloudevents.rw.CloudEventRWException; -import io.cloudevents.rw.CloudEventWriter; -import io.openmessaging.api.Message; -import org.apache.commons.lang3.StringUtils; - -/** - * write ce to ons - * @param - */ -public final class OMSMessageWriter implements MessageWriter, Message>, CloudEventWriter { - - private Message message; - - - public OMSMessageWriter(String topic) { - message = new Message(); - message.setTopic(topic); - } - - public OMSMessageWriter(String topic, String key) { - message = new Message(); - message.setTopic(topic); - if (key != null && key.length() > 0) { - message.setKey(key); - } - } - - public OMSMessageWriter(String topic, String key, String tag) { - message = new Message(); - message.setTopic(topic); - if (StringUtils.isNotEmpty(tag)) { - message.setTag(tag); - } - - if (StringUtils.isNotEmpty(key)) { - message.setKey(key); - } - } - - - @Override - public CloudEventContextWriter withContextAttribute(String name, String value) throws CloudEventRWException { - - String propName = OMSHeaders.ATTRIBUTES_TO_HEADERS.get(name); - if (propName == null) { - propName = OMSHeaders.CE_PREFIX + name; - } - message.putUserProperties(propName, value); - return this; - } - - @Override - public OMSMessageWriter create(final SpecVersion version) { - message.putUserProperties(OMSHeaders.SPEC_VERSION, version.toString()); - return this; - } - - @Override - public Message setEvent(final EventFormat format, final byte[] value) throws CloudEventRWException { - message.putUserProperties(OMSHeaders.CONTENT_TYPE, format.serializedContentType()); - message.setBody(value); - return message; - } - - @Override - public Message end(final CloudEventData data) throws CloudEventRWException { - message.setBody(data.toBytes()); - return message; - } - - @Override - public Message end() { - message.setBody(null); - return message; - } -} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/EventMeshProducer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/EventMeshProducer.java index 29cf4c81c5..34c8d7d5d2 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/EventMeshProducer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/producer/EventMeshProducer.java @@ -20,7 +20,6 @@ import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; -import org.apache.eventmesh.api.RRCallback; import org.apache.eventmesh.api.RequestReplyCallback; import org.apache.eventmesh.api.SendCallback; import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java index 4fbea05a01..e5db1ab7e3 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java @@ -20,7 +20,6 @@ import io.cloudevents.core.builder.CloudEventBuilder; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.eventmesh.api.RRCallback; import org.apache.eventmesh.api.RequestReplyCallback; import org.apache.eventmesh.api.SendCallback; import org.apache.eventmesh.common.protocol.tcp.Command; diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HelloTask.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HelloTask.java index df4a6062b4..10e8f1ad41 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HelloTask.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/task/HelloTask.java @@ -55,7 +55,7 @@ public void run() { UserAgent user = (UserAgent) pkg.getBody(); try { //do acl check in connect - if(eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerSecurityEnable){ + if (eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerSecurityEnable) { String remoteAddr = RemotingHelper.parseChannelRemoteAddr(ctx.channel()); Acl.doAclCheckInTcpConnect(remoteAddr, user, HELLO_REQUEST.value()); } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java index 035e9d4c97..22400f7c3d 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/EventMeshUtil.java @@ -38,7 +38,6 @@ import com.fasterxml.jackson.databind.SerializationFeature; import io.cloudevents.CloudEvent; -import io.openmessaging.api.Message; import org.apache.commons.lang3.StringUtils; import org.apache.eventmesh.common.Constants; @@ -200,42 +199,42 @@ public static Map getEventProp(CloudEvent event) { // return accessMessage; // } - public static EventMeshMessage encodeMessage(Message omsMessage) throws Exception { - - EventMeshMessage eventMeshMessage = new EventMeshMessage(); - eventMeshMessage.setBody(new String(omsMessage.getBody(), StandardCharsets.UTF_8)); - - Properties sysHeaders = omsMessage.getSystemProperties(); - Properties userHeaders = omsMessage.getUserProperties(); - - //All destinations in RocketMQ use Topic - eventMeshMessage.setTopic(sysHeaders.getProperty(Constants.PROPERTY_MESSAGE_DESTINATION)); - - if (sysHeaders.containsKey("START_TIME")) { - long deliverTime; - if (StringUtils.isBlank(sysHeaders.getProperty("START_TIME"))) { - deliverTime = 0; - } else { - deliverTime = Long.parseLong(sysHeaders.getProperty("START_TIME")); - } - - if (deliverTime > 0) { -// rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); - eventMeshMessage.getProperties().put("START_TIME", String.valueOf(deliverTime)); - } - } - - for (String key : userHeaders.stringPropertyNames()) { - eventMeshMessage.getProperties().put(key, userHeaders.getProperty(key)); - } - - //System headers has a high priority - for (String key : sysHeaders.stringPropertyNames()) { - eventMeshMessage.getProperties().put(key, sysHeaders.getProperty(key)); - } - - return eventMeshMessage; - } +// public static EventMeshMessage encodeMessage(Message omsMessage) throws Exception { +// +// EventMeshMessage eventMeshMessage = new EventMeshMessage(); +// eventMeshMessage.setBody(new String(omsMessage.getBody(), StandardCharsets.UTF_8)); +// +// Properties sysHeaders = omsMessage.getSystemProperties(); +// Properties userHeaders = omsMessage.getUserProperties(); +// +// //All destinations in RocketMQ use Topic +// eventMeshMessage.setTopic(sysHeaders.getProperty(Constants.PROPERTY_MESSAGE_DESTINATION)); +// +// if (sysHeaders.containsKey("START_TIME")) { +// long deliverTime; +// if (StringUtils.isBlank(sysHeaders.getProperty("START_TIME"))) { +// deliverTime = 0; +// } else { +// deliverTime = Long.parseLong(sysHeaders.getProperty("START_TIME")); +// } +// +// if (deliverTime > 0) { +//// rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); +// eventMeshMessage.getProperties().put("START_TIME", String.valueOf(deliverTime)); +// } +// } +// +// for (String key : userHeaders.stringPropertyNames()) { +// eventMeshMessage.getProperties().put(key, userHeaders.getProperty(key)); +// } +// +// //System headers has a high priority +// for (String key : sysHeaders.stringPropertyNames()) { +// eventMeshMessage.getProperties().put(key, sysHeaders.getProperty(key)); +// } +// +// return eventMeshMessage; +// } public static String getLocalAddr() { //priority of networkInterface when generating client ip diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPClient.java index ceaa4ce655..8ed1ea85b8 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPClient.java @@ -41,8 +41,8 @@ public EventMeshMessageTCPClient(EventMeshTCPClientConfig eventMeshTcpClientConf @Override public void init() throws EventMeshException { - eventMeshMessageTCPPubClient.init(); eventMeshMessageTCPSubClient.init(); + eventMeshMessageTCPPubClient.init(); } @Override diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java index 794cea66e5..a3dd6a41bd 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java @@ -56,7 +56,7 @@ class EventMeshMessageTCPPubClient extends TcpClient implements EventMeshTCPPubC public EventMeshMessageTCPPubClient(EventMeshTCPClientConfig eventMeshTcpClientConfig) { super(eventMeshTcpClientConfig); - this.userAgent = eventMeshTcpClientConfig.getUserAgent(); + this.userAgent = MessageUtils.generatePubClient(eventMeshTcpClientConfig.getUserAgent()); } @Override diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java index 7939066e92..add12a5d85 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java @@ -55,7 +55,7 @@ class EventMeshMessageTCPSubClient extends TcpClient implements EventMeshTCPSubC public EventMeshMessageTCPSubClient(EventMeshTCPClientConfig eventMeshTcpClientConfig) { super(eventMeshTcpClientConfig); - this.userAgent = eventMeshTcpClientConfig.getUserAgent(); + this.userAgent = MessageUtils.generateSubClient(eventMeshTcpClientConfig.getUserAgent()); } @Override From a27ad7e2eb56624e1d12b5bb2846d3897fd3dd73 Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Fri, 26 Nov 2021 19:00:57 +0800 Subject: [PATCH 05/13] 1.fix the standalone connector 2.fix the sdk --- .../org/apache/eventmesh/common/Constants.java | 6 +++--- .../consumer/StandaloneConsumerAdaptor.java | 16 ++++++++-------- .../demo/pub/eventmeshmessage/AsyncPublish.java | 1 - .../sub/eventmeshmessage/AsyncSubscribe.java | 12 ++++++++++-- ...apache.eventmesh.protocol.api.ProtocolAdaptor | 0 .../impl/cloudevent/CloudEventTCPPubClient.java | 5 +++-- .../EventMeshMessageTCPPubClient.java | 15 ++++++++------- .../EventMeshMessageTCPSubClient.java | 8 ++++---- 8 files changed, 36 insertions(+), 27 deletions(-) rename eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/resources/{META-INF.eventmesh => META-INF/eventmesh}/org.apache.eventmesh.protocol.api.ProtocolAdaptor (100%) diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java index f5fa2cd698..20c1b4e742 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java @@ -29,11 +29,11 @@ public class Constants { public static final String HTTPS_PROTOCOL_PREFIX = "https://"; - public static final String PROTOCOL_TYPE = "protocol_type"; + public static final String PROTOCOL_TYPE = "protocoltype"; - public static final String PROTOCOL_VERSION = "protocol_version"; + public static final String PROTOCOL_VERSION = "protocolversion"; - public static final String PROTOCOL_DESC = "protocol_desc"; + public static final String PROTOCOL_DESC = "protocoldesc"; public static final int DEFAULT_HTTP_TIME_OUT = 3000; diff --git a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/consumer/StandaloneConsumerAdaptor.java b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/consumer/StandaloneConsumerAdaptor.java index 56518a75d2..716b4a21d3 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/consumer/StandaloneConsumerAdaptor.java +++ b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/consumer/StandaloneConsumerAdaptor.java @@ -40,42 +40,42 @@ public StandaloneConsumerAdaptor() { @Override public boolean isStarted() { - return false; + return consumer.isStarted(); } @Override public boolean isClosed() { - return false; + return consumer.isClosed(); } @Override public void start() { - + consumer.start(); } @Override public void shutdown() { - + consumer.shutdown(); } @Override public void init(Properties keyValue) throws Exception { - + consumer = new StandaloneConsumer(keyValue); } @Override public void updateOffset(List cloudEvents, AbstractContext context) { - + consumer.updateOffset(cloudEvents, context); } @Override public void subscribe(String topic, EventListener listener) throws Exception { - + consumer.subscribe(topic, listener); } @Override public void unsubscribe(String topic) { - + consumer.unsubscribe(topic); } // @Override diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/eventmeshmessage/AsyncPublish.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/eventmeshmessage/AsyncPublish.java index a01bb250b4..bc393523de 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/eventmeshmessage/AsyncPublish.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/eventmeshmessage/AsyncPublish.java @@ -63,7 +63,6 @@ public static void main(String[] agrs) throws Exception { Thread.sleep(1000); } - client.listen(); Thread.sleep(2000); } catch (Exception e) { logger.warn("AsyncPublish failed", e); diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/eventmeshmessage/AsyncSubscribe.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/eventmeshmessage/AsyncSubscribe.java index f27dbd1b75..9df4548932 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/eventmeshmessage/AsyncSubscribe.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/eventmeshmessage/AsyncSubscribe.java @@ -21,6 +21,7 @@ import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; import org.apache.eventmesh.client.tcp.conf.EventMeshTCPClientConfig; import org.apache.eventmesh.client.tcp.impl.EventMeshTCPClientFactory; +import org.apache.eventmesh.client.tcp.impl.eventmeshmessage.EventMeshMessageTCPClient; import org.apache.eventmesh.common.protocol.SubscriptionMode; import org.apache.eventmesh.common.protocol.SubscriptionType; import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; @@ -39,6 +40,8 @@ public class AsyncSubscribe implements ReceiveMsgHook { public static AsyncSubscribe handler = new AsyncSubscribe(); + private static EventMeshTCPClient client; + public static void main(String[] agrs) throws Exception { Properties properties = Utils.readPropertiesFile("application.properties"); final String eventMeshIp = properties.getProperty("eventmesh.ip"); @@ -49,8 +52,8 @@ public static void main(String[] agrs) throws Exception { .port(eventMeshTcpPort) .userAgent(userAgent) .build(); - try (EventMeshTCPClient client = EventMeshTCPClientFactory.createEventMeshTCPClient( - eventMeshTcpClientConfig, EventMeshMessage.class)) { + try { + client = EventMeshTCPClientFactory.createEventMeshTCPClient(eventMeshTcpClientConfig, EventMeshMessage.class); client.init(); client.heartbeat(); @@ -59,6 +62,11 @@ public static void main(String[] agrs) throws Exception { client.listen(); + //client.unsubscribe(); + + // release resource and close client + // client.close(); + } catch (Exception e) { log.warn("AsyncSubscribe failed", e); } diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/resources/META-INF.eventmesh/org.apache.eventmesh.protocol.api.ProtocolAdaptor b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.protocol.api.ProtocolAdaptor similarity index 100% rename from eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/resources/META-INF.eventmesh/org.apache.eventmesh.protocol.api.ProtocolAdaptor rename to eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.protocol.api.ProtocolAdaptor diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPPubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPPubClient.java index d4fbec7b36..ca5901a7ea 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPPubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPPubClient.java @@ -26,6 +26,7 @@ import org.apache.eventmesh.client.tcp.common.RequestContext; import org.apache.eventmesh.client.tcp.common.TcpClient; import org.apache.eventmesh.client.tcp.conf.EventMeshTCPClientConfig; +import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.common.exception.EventMeshException; import org.apache.eventmesh.common.protocol.tcp.Command; import org.apache.eventmesh.common.protocol.tcp.Package; @@ -128,7 +129,7 @@ public Package publish(CloudEvent cloudEvent, long timeout) throws EventMeshExce Package msg = MessageUtils.buildPackage(cloudEvent, Command.ASYNC_MESSAGE_TO_SERVER); log.info("SimplePubClientImpl cloud event|{}|publish|send|type={}|protocol={}|msg={}", clientNo, msg.getHeader().getCommand(), - msg.getHeader().getProperty(PropertyConst.PROPERTY_MESSAGE_PROTOCOL), msg); + msg.getHeader().getProperty(Constants.PROTOCOL_TYPE), msg); return io(msg, timeout); } catch (Exception ex) { throw new EventMeshException("publish error", ex); @@ -141,7 +142,7 @@ public void broadcast(CloudEvent cloudEvent, long timeout) throws EventMeshExcep // todo: transform EventMeshMessage to Package Package msg = MessageUtils.buildPackage(cloudEvent, Command.BROADCAST_MESSAGE_TO_SERVER); log.info("{}|publish|send|type={}|protocol={}|msg={}", clientNo, msg.getHeader().getCommand(), - msg.getHeader().getProperty(PropertyConst.PROPERTY_MESSAGE_PROTOCOL), msg); + msg.getHeader().getProperty(Constants.PROTOCOL_TYPE), msg); super.send(msg); } catch (Exception ex) { throw new EventMeshException("Broadcast message error", ex); diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java index a3dd6a41bd..18f555fcaa 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java @@ -26,6 +26,7 @@ import org.apache.eventmesh.client.tcp.common.RequestContext; import org.apache.eventmesh.client.tcp.common.TcpClient; import org.apache.eventmesh.client.tcp.conf.EventMeshTCPClientConfig; +import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.common.exception.EventMeshException; import org.apache.eventmesh.common.protocol.tcp.Command; import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; @@ -72,8 +73,8 @@ public void init() throws EventMeshException { @Override public void heartbeat() throws EventMeshException { - if (task != null) { - synchronized (EventMeshMessageTCPPubClient.class) { +// if (task != null) { +// synchronized (EventMeshMessageTCPPubClient.class) { task = scheduler.scheduleAtFixedRate(() -> { try { if (!isActive()) { @@ -86,8 +87,8 @@ public void heartbeat() throws EventMeshException { } }, EventMeshCommon.HEARTBEAT, EventMeshCommon.HEARTBEAT, TimeUnit.MILLISECONDS); } - } - } +// } +// } @Override public void reconnect() throws EventMeshException { @@ -128,9 +129,9 @@ public Package publish(EventMeshMessage eventMeshMessage, long timeout) throws E try { // todo: transform EventMeshMessage to Package Package msg = MessageUtils.buildPackage(eventMeshMessage, Command.ASYNC_MESSAGE_TO_SERVER); - log.info("SimplePubClientImpl cloud event|{}|publish|send|type={}|protocol={}|msg={}", + log.info("SimplePubClientImpl em message|{}|publish|send|type={}|protocol={}|msg={}", clientNo, msg.getHeader().getCommand(), - msg.getHeader().getProperty(PropertyConst.PROPERTY_MESSAGE_PROTOCOL), msg); + msg.getHeader().getProperty(Constants.PROTOCOL_TYPE), msg); return io(msg, timeout); } catch (Exception ex) { throw new EventMeshException("publish error", ex); @@ -143,7 +144,7 @@ public void broadcast(EventMeshMessage eventMeshMessage, long timeout) throws Ev // todo: transform EventMeshMessage to Package Package msg = MessageUtils.buildPackage(eventMeshMessage, Command.BROADCAST_MESSAGE_TO_SERVER); log.info("{}|publish|send|type={}|protocol={}|msg={}", clientNo, msg.getHeader().getCommand(), - msg.getHeader().getProperty(PropertyConst.PROPERTY_MESSAGE_PROTOCOL), msg); + msg.getHeader().getProperty(Constants.PROTOCOL_TYPE), msg); super.send(msg); } catch (Exception ex) { throw new EventMeshException("Broadcast message error", ex); diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java index add12a5d85..f9da554efd 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java @@ -71,8 +71,8 @@ public void init() throws EventMeshException { @Override public void heartbeat() throws EventMeshException { - if (task == null) { - synchronized (EventMeshMessageTCPSubClient.class) { +// if (task == null) { +// synchronized (EventMeshMessageTCPSubClient.class) { task = scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { @@ -88,8 +88,8 @@ public void run() { } }, EventMeshCommon.HEARTBEAT, EventMeshCommon.HEARTBEAT, TimeUnit.MILLISECONDS); } - } - } +// } +// } @Override public void reconnect() throws EventMeshException { From 76ae04f520d81236a01c2b671f3a00254486ac41 Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Mon, 29 Nov 2021 19:37:21 +0800 Subject: [PATCH 06/13] 1.fix the standalone connector 2.fix eventmeshmessage protocol adaptor for tcp --- .../common/protocol/tcp/UserAgent.java | 2 +- .../standalone/broker/task/SubScribeTask.java | 7 ++- .../tcp/common/EventMeshTestUtils.java | 22 ++++----- .../tcp/TcpMessageProtocolResolver.java | 3 ++ .../runtime/constants/EventMeshConstants.java | 48 +++++++++---------- .../group/ClientSessionGroupMapping.java | 4 +- .../EventMeshMessageTCPSubClient.java | 30 +++++------- 7 files changed, 60 insertions(+), 56 deletions(-) diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java index 6ad7a32072..1d0c8d3fad 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/UserAgent.java @@ -67,7 +67,7 @@ public UserAgent(String env, String subsystem, String path, int pid, String host @Override public String toString() { return String.format( - "UserAgent{env='%s'subsystem='%s', path='%s', pid=%d, host='%s', port=%d, version='%s', idc='%s', purpose='%s', unack='%d'}", + "UserAgent{env='%s', subsystem='%s', path='%s', pid=%d, host='%s', port=%d, version='%s', idc='%s', purpose='%s', unack='%d'}", env, subsystem, path, pid, host, port, version, idc, purpose, unack); } diff --git a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/task/SubScribeTask.java b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/task/SubScribeTask.java index bfbf1671ee..f5058c111c 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/task/SubScribeTask.java +++ b/eventmesh-connector-plugin/eventmesh-connector-standalone/src/main/java/org/apache/eventmesh/connector/standalone/broker/task/SubScribeTask.java @@ -57,7 +57,12 @@ public void run() { if (offset == null) { CloudEvent message = standaloneBroker.getMessage(topicName); if (message != null) { - offset = new AtomicInteger((int) message.getExtension("offset")); + if (message.getExtension("offset") != null) { + offset = new AtomicInteger((int) message.getExtension("offset")); + } else { + offset = new AtomicInteger(0); + } + } } if (offset != null) { diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java index d6f30e8502..db031171b7 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java @@ -111,8 +111,8 @@ public static EventMeshMessage generateSyncRRMqMsg() { EventMeshMessage mqMsg = new EventMeshMessage(); mqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); mqMsg.getProperties().put("msgType", "persistent"); - mqMsg.getProperties().put("TTL", "300000"); - mqMsg.getProperties().put("KEYS", generateRandomString(16)); + mqMsg.getProperties().put("ttl", "300000"); + mqMsg.getProperties().put("keys", generateRandomString(16)); mqMsg.setBody("testSyncRR"); return mqMsg; } @@ -121,9 +121,9 @@ public static EventMeshMessage generateSyncRRMqMsg() { private static EventMeshMessage generateAsyncRRMqMsg() { EventMeshMessage mqMsg = new EventMeshMessage(); mqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); - mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); - mqMsg.getProperties().put("TTL", "300000"); - mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.getProperties().put("replyto", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("ttl", "300000"); + mqMsg.getProperties().put("propertymessagereplyto", "notnull"); mqMsg.setBody("testAsyncRR"); return mqMsg; } @@ -131,9 +131,9 @@ private static EventMeshMessage generateAsyncRRMqMsg() { public static EventMeshMessage generateAsyncEventMqMsg() { EventMeshMessage mqMsg = new EventMeshMessage(); mqMsg.setTopic(TOPIC_PRX_WQ2ClientUniCast); - mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); - mqMsg.getProperties().put("TTL", "30000"); - mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.getProperties().put("replyto", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("ttl", "30000"); + mqMsg.getProperties().put("propertymessagereplyto", "notnull"); mqMsg.setBody("testAsyncMessage"); return mqMsg; } @@ -141,9 +141,9 @@ public static EventMeshMessage generateAsyncEventMqMsg() { public static EventMeshMessage generateBroadcastMqMsg() { EventMeshMessage mqMsg = new EventMeshMessage(); mqMsg.setTopic(TOPIC_PRX_WQ2ClientBroadCast); - mqMsg.getProperties().put("REPLY_TO", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); - mqMsg.getProperties().put("TTL", "30000"); - mqMsg.getProperties().put("PROPERTY_MESSAGE_REPLY_TO", "notnull"); + mqMsg.getProperties().put("replyto", "10.36.0.109@ProducerGroup-producerPool-9-access#V1_4_0#CI"); + mqMsg.getProperties().put("ttl", "30000"); + mqMsg.getProperties().put("propertymessagereplyto", "notnull"); mqMsg.setBody("testAsyncMessage"); return mqMsg; } diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/tcp/TcpMessageProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/tcp/TcpMessageProtocolResolver.java index 623575941b..3f69237fe7 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/tcp/TcpMessageProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/tcp/TcpMessageProtocolResolver.java @@ -28,6 +28,7 @@ import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException; import org.apache.eventmesh.protocol.meshmessage.MeshMessageProtocolConstant; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -72,6 +73,8 @@ public static CloudEvent buildEvent(Header header, Object body) throws ProtocolH cloudEventBuilder = cloudEventBuilder .withId(header.getSeq()) + .withSource(URI.create("/")) + .withType("eventmeshmessage") .withSubject(topic) .withData(content.getBytes(StandardCharsets.UTF_8)); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java index 810b3f20ce..537aa9140a 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java @@ -35,19 +35,19 @@ public class EventMeshConstants { public static final String EVENTMESH_CONF_FILE = "eventmesh.properties"; - public static final String REQ_C2EVENTMESH_TIMESTAMP = "req_c2eventMesh_timestamp"; - public static final String REQ_EVENTMESH2MQ_TIMESTAMP = "req_eventMesh2mq_timestamp"; - public static final String REQ_MQ2EVENTMESH_TIMESTAMP = "req_mq2eventMesh_timestamp"; - public static final String REQ_EVENTMESH2C_TIMESTAMP = "req_eventMesh2c_timestamp"; - public static final String RSP_C2EVENTMESH_TIMESTAMP = "rsp_c2eventMesh_timestamp"; - public static final String RSP_EVENTMESH2MQ_TIMESTAMP = "rsp_eventMesh2mq_timestamp"; - public static final String RSP_MQ2EVENTMESH_TIMESTAMP = "rsp_mq2eventMesh_timestamp"; - public static final String RSP_EVENTMESH2C_TIMESTAMP = "rsp_eventMesh2c_timestamp"; - - public static final String REQ_SEND_EVENTMESH_IP = "req_send_eventMesh_ip"; - public static final String REQ_RECEIVE_EVENTMESH_IP = "req_receive_eventMesh_ip"; - public static final String RSP_SEND_EVENTMESH_IP = "rsp_send_eventMesh_ip"; - public static final String RSP_RECEIVE_EVENTMESH_IP = "rsp_receive_eventMesh_ip"; + public static final String REQ_C2EVENTMESH_TIMESTAMP = "reqc2eventmeshtimestamp"; + public static final String REQ_EVENTMESH2MQ_TIMESTAMP = "reqeventmesh2mqtimestamp"; + public static final String REQ_MQ2EVENTMESH_TIMESTAMP = "reqmq2eventmeshtimestamp"; + public static final String REQ_EVENTMESH2C_TIMESTAMP = "reqeventmesh2ctimestamp"; + public static final String RSP_C2EVENTMESH_TIMESTAMP = "rspc2eventmeshtimestamp"; + public static final String RSP_EVENTMESH2MQ_TIMESTAMP = "rspeventmesh2mqtimestamp"; + public static final String RSP_MQ2EVENTMESH_TIMESTAMP = "rspmq2eventmeshtimestamp"; + public static final String RSP_EVENTMESH2C_TIMESTAMP = "rspeventmesh2ctimestamp"; + + public static final String REQ_SEND_EVENTMESH_IP = "reqsendeventmeship"; + public static final String REQ_RECEIVE_EVENTMESH_IP = "reqreceiveeventmeship"; + public static final String RSP_SEND_EVENTMESH_IP = "rspsendeventmeship"; + public static final String RSP_RECEIVE_EVENTMESH_IP = "rspreceiveeventmeship"; //default TTL 4 hours public static final Integer DEFAULT_MSG_TTL_MILLS = 14400000; @@ -77,23 +77,23 @@ public class EventMeshConstants { public static final String KEYS_UPPERCASE = "KEYS"; public static final String KEYS_LOWERCASE = "keys"; public static final String RR_REQUEST_UNIQ_ID = "RR_REQUEST_UNIQ_ID"; - public static final String TTL = "TTL"; + public static final String TTL = "ttl"; public static final String TAG = "TAG"; - public static final String MANAGE_SUBSYSTEM = "subSystem"; + public static final String MANAGE_SUBSYSTEM = "subsystem"; public static final String MANAGE_IP = "ip"; public static final String MANAGE_PORT = "port"; - public static final String MANAGE_DEST_IP = "desteventMeshIp"; - public static final String MANAGE_DEST_PORT = "desteventMeshPort"; + public static final String MANAGE_DEST_IP = "desteventmeshIp"; + public static final String MANAGE_DEST_PORT = "desteventmeshport"; public static final String MANAGE_PATH = "path"; public static final String MANAGE_GROUP = "group"; public static final String MANAGE_PURPOSE = "purpose"; public static final String MANAGE_TOPIC = "topic"; - public static final String EVENTMESH_SEND_BACK_TIMES = "eventMeshSendBackTimes"; + public static final String EVENTMESH_SEND_BACK_TIMES = "eventmeshdendbacktimes"; - public static final String EVENTMESH_SEND_BACK_IP = "eventMeshSendBackIp"; + public static final String EVENTMESH_SEND_BACK_IP = "eventmeshsendbackip"; public static final String EVENTMESH_REGISTRY_ADDR_KEY = "eventMeshRegistryAddr"; @@ -103,16 +103,16 @@ public class EventMeshConstants { public static final String PROPERTY_MESSAGE_CLUSTER = "CLUSTER"; - public static final String PROPERTY_MESSAGE_TTL = "TTL"; + public static final String PROPERTY_MESSAGE_TTL = "ttl"; - public static final String PROPERTY_MESSAGE_KEYS = "KEYS"; + public static final String PROPERTY_MESSAGE_KEYS = "keys"; public static final String PROPERTY_MESSAGE_REPLY_TO = "REPLY_TO"; //requester clientId public static final String PROPERTY_RR_REQUEST_ID = "RR_REQUEST_UNIQ_ID"; - public static final String LEAVE_TIME = "LEAVE_TIME"; //leaveBrokerTime - public static final String ARRIVE_TIME = "ARRIVE_TIME"; - public static final String STORE_TIME = "STORE_TIME"; + public static final String LEAVE_TIME = "leavetime"; //leaveBrokerTime + public static final String ARRIVE_TIME = "arrivetime"; + public static final String STORE_TIME = "storetime"; } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java index 8f3e383eb4..071c104d19 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java @@ -367,8 +367,8 @@ public void run() { Iterator sessionIterator = sessionTable.values().iterator(); while (sessionIterator.hasNext()) { Session tmp = sessionIterator.next(); - if (System.currentTimeMillis() - tmp.getLastHeartbeatTime() > - eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSessionExpiredInMills) { + if (System.currentTimeMillis() - tmp.getLastHeartbeatTime() + > eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSessionExpiredInMills) { try { logger.warn("clean expired session,client:{}", tmp.getClient()); closeSession(tmp.getContext()); diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java index f9da554efd..4453249a52 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java @@ -71,25 +71,21 @@ public void init() throws EventMeshException { @Override public void heartbeat() throws EventMeshException { -// if (task == null) { -// synchronized (EventMeshMessageTCPSubClient.class) { - task = scheduler.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - try { - if (!isActive()) { - reconnect(); - } - Package msg = MessageUtils.heartBeat(); - io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); - } catch (Exception ignore) { - // - } + task = scheduler.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + try { + if (!isActive()) { + reconnect(); } - }, EventMeshCommon.HEARTBEAT, EventMeshCommon.HEARTBEAT, TimeUnit.MILLISECONDS); + Package msg = MessageUtils.heartBeat(); + io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + } catch (Exception ignore) { + // + } } -// } -// } + }, EventMeshCommon.HEARTBEAT, EventMeshCommon.HEARTBEAT, TimeUnit.MILLISECONDS); + } @Override public void reconnect() throws EventMeshException { From e8d8e4a897e20f81778d5f040df39132d2900e98 Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Tue, 30 Nov 2021 15:26:24 +0800 Subject: [PATCH 07/13] 1.fix eventmeshmessage protocol adaptor for http with standalone connector --- .../body/message/PushMessageRequestBody.java | 2 +- .../body/message/ReplyMessageRequestBody.java | 6 ++-- .../SendMessageBatchV2RequestBody.java | 4 +-- .../body/message/SendMessageRequestBody.java | 6 ++-- .../protocol/http/common/ProtocolKey.java | 34 +++++++++---------- .../tcp/common/EventMeshTestUtils.java | 2 +- .../SendMessageBatchV2ProtocolResolver.java | 6 ++++ .../SendMessageRequestProtocolResolver.java | 23 ++++++++----- .../processor/BatchSendMessageProcessor.java | 8 ++--- .../BatchSendMessageV2Processor.java | 8 ++--- .../http/processor/ReplyMessageProcessor.java | 10 +++--- .../processor/SendAsyncMessageProcessor.java | 10 +++--- .../processor/SendSyncMessageProcessor.java | 10 +++--- .../runtime/client/common/MessageUtils.java | 2 +- .../EventMeshMessageTCPClient.java | 1 + .../EventMeshMessageTCPPubClient.java | 1 + .../EventMeshMessageTCPSubClient.java | 1 + .../client/tcp/common/EventMeshTestUtils.java | 2 +- 18 files changed, 76 insertions(+), 60 deletions(-) diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java index fad1e95160..8e24580f39 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/PushMessageRequestBody.java @@ -32,7 +32,7 @@ public class PushMessageRequestBody extends Body { public static final String RANDOMNO = "randomNo"; public static final String TOPIC = "topic"; - public static final String BIZSEQNO = "bizSeqNo"; + public static final String BIZSEQNO = "bizseqno"; public static final String UNIQUEID = "uniqueId"; public static final String CONTENT = "content"; public static final String EXTFIELDS = "extFields"; diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java index 93fa8b5f9a..a6a456b51c 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java @@ -30,12 +30,12 @@ public class ReplyMessageRequestBody extends Body { - public static final String ORIGTOPIC = "origTopic"; - public static final String BIZSEQNO = "bizSeqNo"; + public static final String ORIGTOPIC = "origtopic"; + public static final String BIZSEQNO = "bizseqno"; public static final String UNIQUEID = "uniqueId"; public static final String CONTENT = "content"; public static final String EXTFIELDS = "extFields"; - public static final String PRODUCERGROUP = "producerGroup"; + public static final String PRODUCERGROUP = "producergroup"; private String bizSeqNo; diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java index 8f97cf16c0..b08dca7b0c 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageBatchV2RequestBody.java @@ -25,12 +25,12 @@ public class SendMessageBatchV2RequestBody extends Body { - public static final String BIZSEQNO = "bizSeqNo"; + public static final String BIZSEQNO = "bizseqno"; public static final String TOPIC = "topic"; public static final String MSG = "msg"; public static final String TAG = "tag"; public static final String TTL = "ttl"; - public static final String PRODUCERGROUP = "producerGroup"; + public static final String PRODUCERGROUP = "producergroup"; private String bizSeqNo; diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java index 196a9ccb84..25d35922a3 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/SendMessageRequestBody.java @@ -31,13 +31,13 @@ public class SendMessageRequestBody extends Body { public static final String TOPIC = "topic"; - public static final String BIZSEQNO = "bizSeqNo"; - public static final String UNIQUEID = "uniqueId"; + public static final String BIZSEQNO = "bizseqno"; + public static final String UNIQUEID = "uniqueid"; public static final String CONTENT = "content"; public static final String TTL = "ttl"; public static final String TAG = "tag"; public static final String EXTFIELDS = "extFields"; - public static final String PRODUCERGROUP = "producerGroup"; + public static final String PRODUCERGROUP = "producergroup"; private String topic; diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java index a41ab7604a..eb79f04c95 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java @@ -19,34 +19,34 @@ public class ProtocolKey { - public static final String REQUEST_CODE = "Code"; - public static final String LANGUAGE = "Language"; - public static final String VERSION = "Version"; + public static final String REQUEST_CODE = "code"; + public static final String LANGUAGE = "language"; + public static final String VERSION = "version"; - public static final String PROTOCOL_TYPE = "protocol_type"; + public static final String PROTOCOL_TYPE = "protocoltype"; - public static final String PROTOCOL_VERSION = "protocol_version"; + public static final String PROTOCOL_VERSION = "protocolversion"; - public static final String PROTOCOL_DESC = "protocol_desc"; + public static final String PROTOCOL_DESC = "protocoldesc"; public static class ClientInstanceKey { ////////////////////////////////////Protocol layer requester description/////////// - public static final String ENV = "Env"; - public static final String IDC = "Idc"; - public static final String SYS = "Sys"; - public static final String PID = "Pid"; - public static final String IP = "Ip"; - public static final String USERNAME = "Username"; - public static final String PASSWD = "Passwd"; + public static final String ENV = "env"; + public static final String IDC = "idc"; + public static final String SYS = "sys"; + public static final String PID = "pid"; + public static final String IP = "ip"; + public static final String USERNAME = "username"; + public static final String PASSWD = "passwd"; } public static class EventMeshInstanceKey { ///////////////////////////////////////////////Protocol layer EventMesh description - public static final String EVENTMESHCLUSTER = "EventMeshCluster"; - public static final String EVENTMESHIP = "EventMeshIp"; - public static final String EVENTMESHENV = "EventMeshEnv"; - public static final String EVENTMESHIDC = "EventMeshIdc"; + public static final String EVENTMESHCLUSTER = "eventmeshcluster"; + public static final String EVENTMESHIP = "eventmeship"; + public static final String EVENTMESHENV = "eventmeshenv"; + public static final String EVENTMESHIDC = "eventmeshidc"; } diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java index db031171b7..b7b6995a49 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java @@ -110,7 +110,7 @@ public static Package rrResponse(Package request) { public static EventMeshMessage generateSyncRRMqMsg() { EventMeshMessage mqMsg = new EventMeshMessage(); mqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); - mqMsg.getProperties().put("msgType", "persistent"); + mqMsg.getProperties().put("msgtype", "persistent"); mqMsg.getProperties().put("ttl", "300000"); mqMsg.getProperties().put("keys", generateRandomString(16)); mqMsg.setBody("testSyncRR"); diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageBatchV2ProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageBatchV2ProtocolResolver.java index 6e2ad28140..f7c06bacf1 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageBatchV2ProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageBatchV2ProtocolResolver.java @@ -23,12 +23,14 @@ import org.apache.commons.lang3.StringUtils; import org.apache.eventmesh.common.protocol.http.body.Body; import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; import org.apache.eventmesh.common.protocol.http.header.Header; import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader; import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException; +import java.net.URI; import java.nio.charset.StandardCharsets; public class SendMessageBatchV2ProtocolResolver { @@ -61,6 +63,8 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan event = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()) .withSubject(sendMessageBatchV2RequestBody.getTopic()) + .withType("eventmeshmessage") + .withSource(URI.create("/")) .withData(content.getBytes(StandardCharsets.UTF_8)) .withExtension(ProtocolKey.REQUEST_CODE, code) .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) @@ -84,6 +88,8 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan cloudEventBuilder = CloudEventBuilder.v03(); event = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()) .withSubject(sendMessageBatchV2RequestBody.getTopic()) + .withType("eventmeshmessage") + .withSource(URI.create("/")) .withData(content.getBytes(StandardCharsets.UTF_8)) .withExtension(ProtocolKey.REQUEST_CODE, code) .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageRequestProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageRequestProtocolResolver.java index 3c5a0b193c..41d710345d 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageRequestProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageRequestProtocolResolver.java @@ -28,6 +28,7 @@ import org.apache.commons.lang3.StringUtils; +import java.net.URI; import java.nio.charset.StandardCharsets; import io.cloudevents.CloudEvent; @@ -65,6 +66,8 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan event = cloudEventBuilder.withId(sendMessageRequestBody.getBizSeqNo()) .withSubject(sendMessageRequestBody.getTopic()) + .withType("eventmeshmessage") + .withSource(URI.create("/")) .withData(content.getBytes(StandardCharsets.UTF_8)) .withExtension(ProtocolKey.REQUEST_CODE, code) .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) @@ -79,16 +82,19 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) - .withExtension(SendMessageBatchV2RequestBody.BIZSEQNO, sendMessageRequestBody.getBizSeqNo()) - .withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, + .withExtension(SendMessageRequestBody.BIZSEQNO, sendMessageRequestBody.getBizSeqNo()) + .withExtension(SendMessageRequestBody.UNIQUEID, sendMessageRequestBody.getUniqueId()) + .withExtension(SendMessageRequestBody.PRODUCERGROUP, sendMessageRequestBody.getProducerGroup()) - .withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageRequestBody.getTtl()) - .withExtension(SendMessageBatchV2RequestBody.TAG, sendMessageRequestBody.getTag()) + .withExtension(SendMessageRequestBody.TTL, sendMessageRequestBody.getTtl()) + .withExtension(SendMessageRequestBody.TAG, sendMessageRequestBody.getTag()) .build(); } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) { cloudEventBuilder = CloudEventBuilder.v03(); event = cloudEventBuilder.withId(sendMessageRequestBody.getBizSeqNo()) .withSubject(sendMessageRequestBody.getTopic()) + .withType("eventmeshmessage") + .withSource(URI.create("/")) .withData(content.getBytes(StandardCharsets.UTF_8)) .withExtension(ProtocolKey.REQUEST_CODE, code) .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) @@ -103,11 +109,12 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) - .withExtension(SendMessageBatchV2RequestBody.BIZSEQNO, sendMessageRequestBody.getBizSeqNo()) - .withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, + .withExtension(SendMessageRequestBody.BIZSEQNO, sendMessageRequestBody.getBizSeqNo()) + .withExtension(SendMessageRequestBody.UNIQUEID, sendMessageRequestBody.getUniqueId()) + .withExtension(SendMessageRequestBody.PRODUCERGROUP, sendMessageRequestBody.getProducerGroup()) - .withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageRequestBody.getTtl()) - .withExtension(SendMessageBatchV2RequestBody.TAG, sendMessageRequestBody.getTag()) + .withExtension(SendMessageRequestBody.TTL, sendMessageRequestBody.getTtl()) + .withExtension(SendMessageRequestBody.TAG, sendMessageRequestBody.getTag()) .build(); } return event; diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java index 595df2b52c..24e6119f63 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java @@ -107,8 +107,8 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext for (CloudEvent event : eventList) { //validate event if (StringUtils.isBlank(event.getId()) - || event.getSource() != null - || event.getSpecVersion() != null + || event.getSource() == null + || event.getSpecVersion() == null || StringUtils.isBlank(event.getType()) || StringUtils.isBlank(event.getSubject())) { responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( @@ -185,7 +185,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext for (CloudEvent cloudEvent : eventList) { if (StringUtils.isBlank(cloudEvent.getSubject()) - || cloudEvent.getData() != null) { + || cloudEvent.getData() == null) { continue; } @@ -215,7 +215,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext if (StringUtils.isBlank(ttl) || !StringUtils.isNumeric(ttl)) { cloudEvent = CloudEventBuilder.from(cloudEvent) .withExtension(SendMessageRequestBody.TTL, String.valueOf(EventMeshConstants.DEFAULT_MSG_TTL_MILLS)) - .withExtension("msgType", "persistent") + .withExtension("msgtype", "persistent") .build(); } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java index d568fce77c..b15add641d 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java @@ -103,8 +103,8 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext // todo: use validate processor to check //validate event if (StringUtils.isBlank(event.getId()) - || event.getSource() != null - || event.getSpecVersion() != null + || event.getSource() == null + || event.getSpecVersion() == null || StringUtils.isBlank(event.getType()) || StringUtils.isBlank(event.getSubject())) { responseEventMeshCommand = request.createHttpCommandResponse( @@ -148,7 +148,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext if (StringUtils.isBlank(bizNo) || StringUtils.isBlank(topic) || StringUtils.isBlank(producerGroup) - || event.getData() != null) { + || event.getData() == null) { responseEventMeshCommand = request.createHttpCommandResponse( sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody @@ -221,7 +221,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext try { event = CloudEventBuilder.from(event) - .withExtension("msgType", "persistent") + .withExtension("msgtype", "persistent") .withExtension(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())) .withExtension(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java index ca6890bc90..8114f4cc90 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java @@ -89,10 +89,10 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); //validate event - if (event != null + if (event == null || StringUtils.isBlank(event.getId()) - || event.getSource() != null - || event.getSpecVersion() != null + || event.getSource() == null + || event.getSpecVersion() == null || StringUtils.isBlank(event.getType()) || StringUtils.isBlank(event.getSubject())) { responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( @@ -126,7 +126,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext if (StringUtils.isBlank(bizNo) || StringUtils.isBlank(uniqueId) || StringUtils.isBlank(producerGroup) - || event.getData() != null) { + || event.getData() == null) { responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( replyMessageResponseHeader, ReplyMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); @@ -182,7 +182,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext // omsMsg.setBody(replyMessageRequestBody.getContent().getBytes(EventMeshConstants.DEFAULT_CHARSET)); event = CloudEventBuilder.from(event) .withSubject(replyTopic) - .withExtension("msgType", "persistent") + .withExtension("msgtype", "persistent") .withExtension(Constants.PROPERTY_MESSAGE_TIMEOUT, String.valueOf(EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)) .withExtension(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())) .build(); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java index 6b078c8b1a..5cbbd302de 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java @@ -92,10 +92,10 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext CloudEvent event = httpCommandProtocolAdaptor.toCloudEvent(asyncContext.getRequest()); //validate event - if (event != null + if (event == null || StringUtils.isBlank(event.getId()) - || event.getSource() != null - || event.getSpecVersion() != null + || event.getSource() == null + || event.getSpecVersion() == null || StringUtils.isBlank(event.getType()) || StringUtils.isBlank(event.getSubject())) { responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( @@ -131,7 +131,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext || StringUtils.isBlank(uniqueId) || StringUtils.isBlank(producerGroup) || StringUtils.isBlank(topic) - || event.getData() != null) { + || event.getData() == null) { responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( sendMessageResponseHeader, SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg())); @@ -203,7 +203,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext // // bizNo // omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS, sendMessageRequestBody.getBizSeqNo()); event = CloudEventBuilder.from(event) - .withExtension("msgType", "persistent") + .withExtension("msgtype", "persistent") .withExtension(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())) .withExtension(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())) .build(); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java index 5842eed21b..dff902ab5d 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java @@ -93,10 +93,10 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC); //validate event - if (event != null + if (event == null || StringUtils.isBlank(event.getId()) - || event.getSource() != null - || event.getSpecVersion() != null + || event.getSource() == null + || event.getSpecVersion() == null || StringUtils.isBlank(event.getType()) || StringUtils.isBlank(event.getSubject())) { responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( @@ -145,7 +145,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext || StringUtils.isBlank(uniqueId) || StringUtils.isBlank(producerGroup) || StringUtils.isBlank(topic) - || event.getData() != null + || event.getData() == null || StringUtils.isBlank(ttl)) { responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse( sendMessageResponseHeader, @@ -207,7 +207,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext try { event = CloudEventBuilder.from(event) - .withExtension("msgType", "persistent") + .withExtension("msgtype", "persistent") .withExtension(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())) .withExtension(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())) .build(); diff --git a/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/client/common/MessageUtils.java b/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/client/common/MessageUtils.java index d8d01f7f0c..0f20828bcb 100644 --- a/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/client/common/MessageUtils.java +++ b/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/client/common/MessageUtils.java @@ -188,7 +188,7 @@ public static Subscription generateSubscription(String topic, SubscriptionMode s public static EventMeshMessage generateRRMsg(String topic, int i) { EventMeshMessage msg = new EventMeshMessage(); msg.setTopic(topic); - msg.getProperties().put("msgType", "persistent"); + msg.getProperties().put("msgtype", "persistent"); msg.getProperties().put("TTL", "300000"); msg.getProperties().put("KEYS", generateRandomString(16)); msg.setBody("testRR" + i); diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPClient.java index 8ed1ea85b8..0011435f84 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPClient.java @@ -69,6 +69,7 @@ public void broadcast(EventMeshMessage eventMeshMessage, long timeout) throws Ev @Override public void heartbeat() throws EventMeshException { eventMeshMessageTCPPubClient.heartbeat(); + eventMeshMessageTCPSubClient.heartbeat(); } @Override diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java index bcf1e286cc..990322cd06 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPPubClient.java @@ -79,6 +79,7 @@ public void heartbeat() throws EventMeshException { } Package msg = MessageUtils.heartBeat(); io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + log.debug("heartbeat to server from pub client|package {}", msg); } catch (Exception ignore) { // ignore } diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java index 4453249a52..c1887b6249 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/eventmeshmessage/EventMeshMessageTCPSubClient.java @@ -80,6 +80,7 @@ public void run() { } Package msg = MessageUtils.heartBeat(); io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + log.debug("heartbeat to server from sub client|package {}", msg); } catch (Exception ignore) { // } diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestUtils.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestUtils.java index 79093e76b3..f90a2d25ef 100644 --- a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestUtils.java +++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/tcp/common/EventMeshTestUtils.java @@ -102,7 +102,7 @@ public static Package rrResponse(Package request) { public static EventMeshMessage generateSyncRRMqMsg() { EventMeshMessage mqMsg = new EventMeshMessage(); mqMsg.setTopic(TOPIC_PRX_SyncSubscribeTest); - mqMsg.getProperties().put("msgType", "persistent"); + mqMsg.getProperties().put("msgtype", "persistent"); mqMsg.getProperties().put("TTL", "300000"); mqMsg.getProperties().put("KEYS", generateRandomString(16)); mqMsg.setBody("testSyncRR"); From 0c405846c5ea7779046d6b1cf3ae646207195d12 Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Tue, 30 Nov 2021 19:27:29 +0800 Subject: [PATCH 08/13] 1.cloudevents protocol tcp pub/sub for sdk --- .../tcp/common/EventMeshTestUtils.java | 19 ++++ .../demo/pub/cloudevents/AsyncPublish.java | 73 ++++++++++++++++ .../demo/sub/cloudevents/AsyncSubscribe.java | 87 +++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/cloudevents/AsyncPublish.java create mode 100644 eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java index b7b6995a49..8c3a06f513 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java @@ -17,11 +17,15 @@ package org.apache.eventmesh.tcp.common; +import io.cloudevents.CloudEvent; +import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.v1.CloudEventV1; import static org.apache.eventmesh.common.protocol.tcp.Command.RESPONSE_TO_SERVER; import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_SyncSubscribeTest; import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientBroadCast; import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientUniCast; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; import org.apache.eventmesh.client.tcp.common.MessageUtils; import org.apache.eventmesh.common.protocol.tcp.Command; import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; @@ -29,6 +33,9 @@ import org.apache.eventmesh.common.protocol.tcp.Package; import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; public class EventMeshTestUtils { @@ -155,4 +162,16 @@ private static String generateRandomString(int length) { } return builder.toString(); } + + public static CloudEvent generateCloudEventV1() { + CloudEvent event = CloudEventBuilder.v1() + .withId(UUID.randomUUID().toString()) + .withSubject(TOPIC_PRX_WQ2ClientBroadCast) + .withSource(URI.create("/")) + .withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME) + .withData("testAsyncMessage".getBytes(StandardCharsets.UTF_8)) + .withExtension("ttl", "30000") + .build(); + return event; + } } diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/cloudevents/AsyncPublish.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/cloudevents/AsyncPublish.java new file mode 100644 index 0000000000..f279308a53 --- /dev/null +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/cloudevents/AsyncPublish.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.demo.pub.cloudevents; + +import io.cloudevents.CloudEvent; + +import org.apache.eventmesh.client.tcp.EventMeshTCPClient; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.client.tcp.conf.EventMeshTCPClientConfig; +import org.apache.eventmesh.client.tcp.impl.EventMeshTCPClientFactory; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.util.Utils; + +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncPublish { + + public static Logger logger = LoggerFactory.getLogger(AsyncPublish.class); + + private static EventMeshTCPClient client; + + public static AsyncPublish handler = new AsyncPublish(); + + public static void main(String[] agrs) throws Exception { + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final int eventMeshTcpPort = Integer.parseInt(properties.getProperty("eventmesh.tcp.port")); + try { + UserAgent userAgent = EventMeshTestUtils.generateClient1(); + EventMeshTCPClientConfig eventMeshTcpClientConfig = EventMeshTCPClientConfig.builder() + .host(eventMeshIp) + .port(eventMeshTcpPort) + .userAgent(userAgent) + .build(); + client = + EventMeshTCPClientFactory.createEventMeshTCPClient(eventMeshTcpClientConfig, CloudEvent.class); + client.init(); + client.heartbeat(); + + for (int i = 0; i < 5; i++) { + CloudEvent event = EventMeshTestUtils.generateCloudEventV1(); + + logger.info("begin send async msg[{}]==================={}", i, event); + client.publish(event, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + + Thread.sleep(1000); + } + Thread.sleep(2000); + } catch (Exception e) { + logger.warn("AsyncPublish failed", e); + } + } +} diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java new file mode 100644 index 0000000000..b94d12fb44 --- /dev/null +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.tcp.demo.sub.cloudevents; + +import org.apache.eventmesh.client.tcp.EventMeshTCPClient; +import org.apache.eventmesh.client.tcp.common.ReceiveMsgHook; +import org.apache.eventmesh.client.tcp.conf.EventMeshTCPClientConfig; +import org.apache.eventmesh.client.tcp.impl.EventMeshTCPClientFactory; +import org.apache.eventmesh.common.protocol.SubscriptionMode; +import org.apache.eventmesh.common.protocol.SubscriptionType; +import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; +import org.apache.eventmesh.common.protocol.tcp.Package; +import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestUtils; +import org.apache.eventmesh.util.Utils; + +import java.util.Properties; + +import io.cloudevents.CloudEvent; +import io.cloudevents.SpecVersion; +import io.cloudevents.core.builder.CloudEventBuilder; +import io.netty.channel.ChannelHandlerContext; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class AsyncSubscribe implements ReceiveMsgHook { + + public static AsyncSubscribe handler = new AsyncSubscribe(); + + private static EventMeshTCPClient client; + + public static void main(String[] agrs) throws Exception { + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final int eventMeshTcpPort = Integer.parseInt(properties.getProperty("eventmesh.tcp.port")); + UserAgent userAgent = EventMeshTestUtils.generateClient2(); + EventMeshTCPClientConfig eventMeshTcpClientConfig = EventMeshTCPClientConfig.builder() + .host(eventMeshIp) + .port(eventMeshTcpPort) + .userAgent(userAgent) + .build(); + try { + client = EventMeshTCPClientFactory.createEventMeshTCPClient(eventMeshTcpClientConfig, CloudEvent.class); + client.init(); + client.heartbeat(); + + client.subscribe("TEST-TOPIC-TCP-ASYNC", SubscriptionMode.CLUSTERING, SubscriptionType.ASYNC); + client.registerSubBusiHandler(handler); + + client.listen(); + + //client.unsubscribe(); + + // release resource and close client + // client.close(); + + } catch (Exception e) { + log.warn("AsyncSubscribe failed", e); + } + } + + @Override + public void handle(Package msg, ChannelHandlerContext ctx) { + CloudEvent event = convertToProtocolMessage(msg); + log.info("receive async msg====================={}", event); + } + + @Override + public CloudEvent convertToProtocolMessage(Package pkg) { + return CloudEventBuilder.from((CloudEvent) pkg.getBody()).build(); + } +} From 8068fc0799d1228795d80b08747c5a4b9ec24d29 Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Wed, 1 Dec 2021 21:38:05 +0800 Subject: [PATCH 09/13] 1.cloudevents protocol tcp pub/sub for sdk --- build.gradle | 1 + .../common/protocol/tcp/codec/Codec.java | 8 ++- .../tcp/common/EventMeshTestUtils.java | 10 +++- .../build.gradle | 1 + .../CloudEventsProtocolAdaptor.java | 6 ++- .../SendMessageBatchProtocolResolver.java | 17 +++++++ .../SendMessageBatchV2ProtocolResolver.java | 17 +++++++ .../SendMessageRequestProtocolResolver.java | 17 +++++++ .../tcp/TcpMessageProtocolResolver.java | 49 +++++++++++++++---- eventmesh-sdk-java/build.gradle | 1 + .../client/tcp/common/MessageUtils.java | 13 ++++- .../cloudevent/CloudEventTCPSubClient.java | 26 ++++++---- 12 files changed, 141 insertions(+), 25 deletions(-) diff --git a/build.gradle b/build.gradle index 2aa26fb725..260c26b0dc 100644 --- a/build.gradle +++ b/build.gradle @@ -450,6 +450,7 @@ subprojects { dependency "org.powermock:powermock-api-mockito2:2.0.2" dependency "io.cloudevents:cloudevents-core:2.2.0" + dependency "io.cloudevents:cloudevents-json-jackson:2.2.0" } } } \ No newline at end of file diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java index 191a09961d..9570100c98 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java @@ -76,7 +76,13 @@ public void encode(ChannelHandlerContext ctx, Package pkg, ByteBuf out) throws E final String bodyJson = pkg != null ? OBJECT_MAPPER.writeValueAsString(pkg.getBody()) : null; final byte[] headerData = serializeBytes(headerJson); - final byte[] bodyData = serializeBytes(bodyJson); +// final byte[] bodyData = serializeBytes(bodyJson); + + byte[] bodyData = serializeBytes(bodyJson); + + if (headerJson.contains("cloudevents")) { + bodyData = (byte[]) pkg.getBody(); + } if (log.isDebugEnabled()) { log.debug("Encoder headerJson={}|bodyJson={}", headerJson, bodyJson); diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java index 43598324a8..894448275b 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java @@ -36,6 +36,8 @@ import java.net.URI; import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; @@ -165,12 +167,16 @@ private static String generateRandomString(int length) { } public static CloudEvent generateCloudEventV1() { + Map content = new HashMap<>(); + content.put("content", "testAsyncMessage"); + CloudEvent event = CloudEventBuilder.v1() .withId(UUID.randomUUID().toString()) - .withSubject(TOPIC_PRX_WQ2ClientBroadCast) + .withSubject(TOPIC_PRX_WQ2ClientUniCast) .withSource(URI.create("/")) + .withDataContentType("application/cloudevents+json") .withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME) - .withData("testAsyncMessage".getBytes(StandardCharsets.UTF_8)) + .withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8)) .withExtension("ttl", "30000") .build(); return event; diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/build.gradle b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/build.gradle index 4a82353e6e..cf2b974f53 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/build.gradle +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/build.gradle @@ -18,6 +18,7 @@ dependencies { compileOnly project(":eventmesh-protocol-plugin:eventmesh-protocol-api") implementation "io.cloudevents:cloudevents-core" + implementation "io.cloudevents:cloudevents-json-jackson" testImplementation project(":eventmesh-protocol-plugin:eventmesh-protocol-api") testImplementation "io.cloudevents:cloudevents-core" diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java index b670ef9084..6e540e6cdd 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java @@ -18,6 +18,8 @@ package org.apache.eventmesh.protocol.cloudevents; import io.cloudevents.CloudEvent; +import io.cloudevents.core.provider.EventFormatProvider; + import org.apache.commons.lang3.StringUtils; import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.common.protocol.ProtocolTransportObject; @@ -111,7 +113,9 @@ public Map toMap() { return httpCommand; } else if (StringUtils.equals("tcp", protocolDesc)) { Package pkg = new Package(); - pkg.setBody(cloudEvent); + byte[] bodyByte = EventFormatProvider.getInstance().resolveFormat(cloudEvent.getDataContentType()) + .serialize(cloudEvent); + pkg.setBody(bodyByte); return pkg; } else { throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc)); diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchProtocolResolver.java index 6ea8d2695f..2b4f180038 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchProtocolResolver.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.eventmesh.protocol.cloudevents.resolver.http; import io.cloudevents.CloudEvent; diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java index 2c33aec9bf..a863bb5924 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.eventmesh.protocol.cloudevents.resolver.http; import io.cloudevents.CloudEvent; diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java index 7ef619a66e..6c80dd7cb3 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.eventmesh.protocol.cloudevents.resolver.http; import io.cloudevents.CloudEvent; diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/tcp/TcpMessageProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/tcp/TcpMessageProtocolResolver.java index 0e2b303562..bec9b8da44 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/tcp/TcpMessageProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/tcp/TcpMessageProtocolResolver.java @@ -1,17 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.eventmesh.protocol.cloudevents.resolver.tcp; -import io.cloudevents.CloudEvent; -import io.cloudevents.SpecVersion; -import io.cloudevents.core.builder.CloudEventBuilder; -import org.apache.commons.lang3.StringUtils; import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.common.protocol.tcp.Header; +import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException; import org.apache.eventmesh.protocol.cloudevents.CloudEventsProtocolConstant; +import org.apache.commons.lang3.StringUtils; + +import io.cloudevents.CloudEvent; +import io.cloudevents.SpecVersion; +import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.provider.EventFormatProvider; +import io.cloudevents.jackson.JsonFormat; + +import java.nio.charset.StandardCharsets; + +import com.fasterxml.jackson.core.JsonProcessingException; + public class TcpMessageProtocolResolver { - public static CloudEvent buildEvent(Header header, String cloudEventJson) throws ProtocolHandleException { + public static CloudEvent buildEvent(Header header, String cloudEventJson) + throws ProtocolHandleException { CloudEventBuilder cloudEventBuilder; String protocolType = header.getProperty(Constants.PROTOCOL_TYPE).toString(); @@ -28,10 +55,11 @@ public static CloudEvent buildEvent(Header header, String cloudEventJson) throws if (!StringUtils.equals(CloudEventsProtocolConstant.PROTOCOL_NAME, protocolType)) { throw new ProtocolHandleException(String.format("Unsupported protocolType: %s", protocolType)); } - if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) { - // todo: transform cloudEventJson to cloudEvent - cloudEventBuilder = CloudEventBuilder.v1(null); + if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) { + CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(cloudEventJson.getBytes( + StandardCharsets.UTF_8)); + cloudEventBuilder = CloudEventBuilder.v1(event); for (String propKey : header.getProperties().keySet()) { cloudEventBuilder.withExtension(propKey, header.getProperty(propKey).toString()); } @@ -39,8 +67,9 @@ public static CloudEvent buildEvent(Header header, String cloudEventJson) throws return cloudEventBuilder.build(); } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) { - // todo: transform cloudEventJson to cloudEvent - cloudEventBuilder = CloudEventBuilder.v03(null); + CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(cloudEventJson.getBytes( + StandardCharsets.UTF_8)); + cloudEventBuilder = CloudEventBuilder.v03(event); for (String propKey : header.getProperties().keySet()) { cloudEventBuilder.withExtension(propKey, header.getProperty(propKey).toString()); diff --git a/eventmesh-sdk-java/build.gradle b/eventmesh-sdk-java/build.gradle index 108e418652..a60dc141b8 100644 --- a/eventmesh-sdk-java/build.gradle +++ b/eventmesh-sdk-java/build.gradle @@ -27,6 +27,7 @@ dependencies { // protocol implementation "io.cloudevents:cloudevents-core" + implementation "io.cloudevents:cloudevents-json-jackson" implementation "io.openmessaging:openmessaging-api" compileOnly 'org.projectlombok:lombok:1.18.22' diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java index 16cbeef753..d1020d435f 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java @@ -27,13 +27,17 @@ import org.apache.eventmesh.common.protocol.tcp.Package; import org.apache.eventmesh.common.protocol.tcp.Subscription; import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.common.utils.JsonUtils; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; import io.cloudevents.CloudEvent; import io.cloudevents.SpecVersion; +import io.cloudevents.core.provider.EventFormatProvider; +import io.cloudevents.jackson.JsonFormat; import io.openmessaging.api.Message; public class MessageUtils { @@ -91,9 +95,15 @@ public static Package buildPackage(Object message, Command command) { if (message instanceof CloudEvent) { msg.getHeader().putProperty(Constants.PROTOCOL_TYPE, EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME); msg.getHeader().putProperty(Constants.PROTOCOL_VERSION, ((CloudEvent) message).getSpecVersion().toString()); + msg.getHeader().putProperty(Constants.PROTOCOL_DESC, "tcp"); + byte[] bodyByte = EventFormatProvider.getInstance().resolveFormat(((CloudEvent) message).getDataContentType()) + .serialize((CloudEvent) message); + msg.setBody(bodyByte); } else if (message instanceof EventMeshMessage) { msg.getHeader().putProperty(Constants.PROTOCOL_TYPE, EventMeshCommon.EM_MESSAGE_PROTOCOL_NAME); msg.getHeader().putProperty(Constants.PROTOCOL_VERSION, SpecVersion.V1.toString()); + msg.getHeader().putProperty(Constants.PROTOCOL_DESC, "tcp"); + msg.setBody(message); } else if (message instanceof Message) { msg.getHeader().putProperty(Constants.PROTOCOL_TYPE, EventMeshCommon.OPEN_MESSAGE_PROTOCOL_NAME); // todo: this version need to be confirmed. @@ -102,8 +112,7 @@ public static Package buildPackage(Object message, Command command) { // unsupported protocol for server throw new IllegalArgumentException("Unsupported message protocol"); } - msg.getHeader().putProperty(Constants.PROTOCOL_DESC, "tcp"); - msg.setBody(message); + return msg; } diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java index 9ea509be3c..1f1f7a09e8 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java @@ -24,6 +24,7 @@ import org.apache.eventmesh.client.tcp.common.RequestContext; import org.apache.eventmesh.client.tcp.common.TcpClient; import org.apache.eventmesh.client.tcp.conf.EventMeshTCPClientConfig; +import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.common.exception.EventMeshException; import org.apache.eventmesh.common.protocol.SubscriptionItem; import org.apache.eventmesh.common.protocol.SubscriptionMode; @@ -37,12 +38,15 @@ import org.apache.commons.collections4.CollectionUtils; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.provider.EventFormatProvider; +import io.cloudevents.jackson.JsonFormat; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import lombok.extern.slf4j.Slf4j; @@ -154,22 +158,29 @@ private class Handler extends SimpleChannelInboundHandler { protected void channelRead0(ChannelHandlerContext ctx, Package msg) throws Exception { Command cmd = msg.getHeader().getCmd(); log.info("|receive|type={}|msg={}", cmd, msg); + String protocolVersion = msg.getHeader().getProperty(Constants.PROTOCOL_VERSION).toString(); if (cmd == Command.REQUEST_TO_CLIENT) { Package pkg = requestToClientAck(msg); if (callback != null) { - callback.handle((CloudEvent) pkg.getBody(), ctx); + CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(msg.getBody().toString().getBytes(StandardCharsets.UTF_8)); + callback.handle(event, ctx); } send(pkg); } else if (cmd == Command.ASYNC_MESSAGE_TO_CLIENT) { Package pkg = asyncMessageAck(msg); if (callback != null) { - callback.handle((CloudEvent) msg, ctx); + CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(msg.getBody().toString().getBytes(StandardCharsets.UTF_8)); + callback.handle(event, ctx); } send(pkg); } else if (cmd == Command.BROADCAST_MESSAGE_TO_CLIENT) { Package pkg = broadcastMessageAck(msg); if (callback != null) { - callback.handle((CloudEvent) msg, ctx); + CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(msg.getBody().toString().getBytes(StandardCharsets.UTF_8)); + callback.handle(event, ctx); } send(pkg); } else if (cmd == Command.SERVER_GOODBYE_REQUEST) { @@ -190,24 +201,21 @@ protected void channelRead0(ChannelHandlerContext ctx, Package msg) throws Excep private Package requestToClientAck(Package tcpPackage) { Package msg = new Package(); msg.setHeader(new Header(Command.REQUEST_TO_CLIENT_ACK, 0, null, tcpPackage.getHeader().getSeq())); - // todo: Transform json to CloudEvents - msg.setBody(JsonUtils.deserialize(tcpPackage.getBody().toString(), EventMeshMessage.class)); + msg.setBody(tcpPackage.getBody()); return msg; } private Package asyncMessageAck(Package tcpPackage) { Package msg = new Package(); msg.setHeader(new Header(Command.ASYNC_MESSAGE_TO_CLIENT_ACK, 0, null, tcpPackage.getHeader().getSeq())); - // todo: Transform to CloudEvents - msg.setBody(JsonUtils.deserialize(tcpPackage.getBody().toString(), EventMeshMessage.class)); + msg.setBody(tcpPackage.getBody()); return msg; } private Package broadcastMessageAck(Package tcpPackage) { Package msg = new Package(); msg.setHeader(new Header(Command.BROADCAST_MESSAGE_TO_CLIENT_ACK, 0, null, tcpPackage.getHeader().getSeq())); - // todo: Transform to CloudEvents - msg.setBody(JsonUtils.deserialize(tcpPackage.getBody().toString(), EventMeshMessage.class)); + msg.setBody(tcpPackage.getBody()); return msg; } From d593d10418bf91b5951e98ac2a781671eb2e27ac Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Thu, 2 Dec 2021 17:09:37 +0800 Subject: [PATCH 10/13] 1.cloudevents protocol tcp pub/sub for sdk --- .../eventmesh/common/protocol/tcp/codec/Codec.java | 9 +++++++-- .../tcp/demo/pub/cloudevents/AsyncPublish.java | 1 - .../tcp/demo/sub/cloudevents/AsyncSubscribe.java | 8 +++----- .../resolver/tcp/TcpMessageProtocolResolver.java | 13 ++++++------- .../tcp/client/session/push/SessionPusher.java | 5 ++--- .../eventmesh/client/tcp/common/MessageUtils.java | 3 --- .../eventmesh/client/tcp/common/TcpClient.java | 3 ++- .../tcp/impl/cloudevent/CloudEventTCPSubClient.java | 1 - 8 files changed, 20 insertions(+), 23 deletions(-) diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java index 9570100c98..e94587f1c4 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java @@ -26,6 +26,7 @@ import org.apache.eventmesh.common.protocol.tcp.UserAgent; import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; import java.nio.charset.Charset; import java.util.Arrays; @@ -80,8 +81,12 @@ public void encode(ChannelHandlerContext ctx, Package pkg, ByteBuf out) throws E byte[] bodyData = serializeBytes(bodyJson); - if (headerJson.contains("cloudevents")) { - bodyData = (byte[]) pkg.getBody(); + String protocolType = ""; + if (pkg.getHeader().getProperty(Constants.PROTOCOL_TYPE) != null) { + protocolType = pkg.getHeader().getProperty(Constants.PROTOCOL_TYPE).toString(); + if (StringUtils.equals(CLOUD_EVENTS_PROTOCOL_NAME, protocolType)) { + bodyData = (byte[]) pkg.getBody(); + } } if (log.isDebugEnabled()) { diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/cloudevents/AsyncPublish.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/cloudevents/AsyncPublish.java index 6864bf1258..2cdf0494d0 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/cloudevents/AsyncPublish.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/pub/cloudevents/AsyncPublish.java @@ -58,7 +58,6 @@ public static void main(String[] agrs) throws Exception { for (int i = 0; i < 5; i++) { CloudEvent event = EventMeshTestUtils.generateCloudEventV1(); - logger.info("begin send async msg[{}]==================={}", i, event); client.publish(event, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java index b41a65c234..c6e4bda682 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java @@ -23,17 +23,14 @@ import org.apache.eventmesh.client.tcp.impl.EventMeshTCPClientFactory; import org.apache.eventmesh.common.protocol.SubscriptionMode; import org.apache.eventmesh.common.protocol.SubscriptionType; -import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; -import org.apache.eventmesh.common.protocol.tcp.Package; import org.apache.eventmesh.common.protocol.tcp.UserAgent; import org.apache.eventmesh.tcp.common.EventMeshTestUtils; import org.apache.eventmesh.util.Utils; +import java.nio.charset.StandardCharsets; import java.util.Properties; import io.cloudevents.CloudEvent; -import io.cloudevents.SpecVersion; -import io.cloudevents.core.builder.CloudEventBuilder; import io.netty.channel.ChannelHandlerContext; import lombok.extern.slf4j.Slf4j; @@ -75,6 +72,7 @@ public static void main(String[] agrs) throws Exception { @Override public void handle(CloudEvent msg, ChannelHandlerContext ctx) { - log.info("receive async msg====================={}", msg); + String content = new String(msg.getData().toBytes(), StandardCharsets.UTF_8); + log.info("receive async msg====================={}|{}", msg, content); } } diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/tcp/TcpMessageProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/tcp/TcpMessageProtocolResolver.java index bec9b8da44..9bf8f5ddea 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/tcp/TcpMessageProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/tcp/TcpMessageProtocolResolver.java @@ -19,7 +19,6 @@ import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.common.protocol.tcp.Header; -import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException; import org.apache.eventmesh.protocol.cloudevents.CloudEventsProtocolConstant; @@ -33,8 +32,6 @@ import java.nio.charset.StandardCharsets; -import com.fasterxml.jackson.core.JsonProcessingException; - public class TcpMessageProtocolResolver { public static CloudEvent buildEvent(Header header, String cloudEventJson) @@ -57,8 +54,9 @@ public static CloudEvent buildEvent(Header header, String cloudEventJson) } if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) { - CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(cloudEventJson.getBytes( - StandardCharsets.UTF_8)); + // todo:resolve different format + CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(cloudEventJson.getBytes(StandardCharsets.UTF_8)); cloudEventBuilder = CloudEventBuilder.v1(event); for (String propKey : header.getProperties().keySet()) { cloudEventBuilder.withExtension(propKey, header.getProperty(propKey).toString()); @@ -67,8 +65,9 @@ public static CloudEvent buildEvent(Header header, String cloudEventJson) return cloudEventBuilder.build(); } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) { - CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(cloudEventJson.getBytes( - StandardCharsets.UTF_8)); + // todo:resolve different format + CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(cloudEventJson.getBytes(StandardCharsets.UTF_8)); cloudEventBuilder = CloudEventBuilder.v03(event); for (String propKey : header.getProperties().keySet()) { diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java index 78de6fcaad..ef339f0999 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java @@ -89,10 +89,9 @@ public void push(final DownStreamMsgContext downStreamMsgContext) { String retMsg = null; try { pkg = (Package) protocolAdaptor.fromCloudEvent(downStreamMsgContext.event); -// body = EventMeshUtil.encodeMessage(downStreamMsgContext.event); -// pkg.setBody(downStreamMsgContext.event); pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, downStreamMsgContext.seq)); - messageLogger.info("pkg|mq2eventMesh|cmd={}|mqMsg={}|user={}", cmd, EventMeshUtil.printMqMessage(body), session.getClient()); + pkg.getHeader().putProperty(Constants.PROTOCOL_TYPE, protocolType); + messageLogger.info("pkg|mq2eventMesh|cmd={}|mqMsg={}|user={}", cmd, pkg, session.getClient()); } catch (Exception e) { pkg.setHeader(new Header(cmd, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), downStreamMsgContext.seq)); retCode = -1; diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java index d1020d435f..4ad89a6e17 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/MessageUtils.java @@ -27,9 +27,7 @@ import org.apache.eventmesh.common.protocol.tcp.Package; import org.apache.eventmesh.common.protocol.tcp.Subscription; import org.apache.eventmesh.common.protocol.tcp.UserAgent; -import org.apache.eventmesh.common.utils.JsonUtils; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; @@ -37,7 +35,6 @@ import io.cloudevents.CloudEvent; import io.cloudevents.SpecVersion; import io.cloudevents.core.provider.EventFormatProvider; -import io.cloudevents.jackson.JsonFormat; import io.openmessaging.api.Message; public class MessageUtils { diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java index c3c7e65916..1a1b34ca24 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java @@ -121,7 +121,7 @@ public void close() { } protected void heartbeat() { - if (heartTask != null) { + if (heartTask == null) { synchronized (TcpClient.class) { heartTask = scheduler.scheduleAtFixedRate(() -> { try { @@ -130,6 +130,7 @@ protected void heartbeat() { } Package msg = MessageUtils.heartBeat(); io(msg, EventMeshCommon.DEFAULT_TIME_OUT_MILLS); + log.debug("heart beat start {}", msg); } catch (Exception ignore) { // ignore } diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java index 1f1f7a09e8..87a2d37962 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java @@ -158,7 +158,6 @@ private class Handler extends SimpleChannelInboundHandler { protected void channelRead0(ChannelHandlerContext ctx, Package msg) throws Exception { Command cmd = msg.getHeader().getCmd(); log.info("|receive|type={}|msg={}", cmd, msg); - String protocolVersion = msg.getHeader().getProperty(Constants.PROTOCOL_VERSION).toString(); if (cmd == Command.REQUEST_TO_CLIENT) { Package pkg = requestToClientAck(msg); if (callback != null) { From 462881b6d3ea8034305c15a543a5d0145ba2d170 Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Thu, 2 Dec 2021 21:14:36 +0800 Subject: [PATCH 11/13] 1.cloudevents protocol http pub/sub for sdk --- .../body/message/ReplyMessageRequestBody.java | 2 +- .../protocol/http/common/ProtocolKey.java | 2 ++ .../demo/sub/controller/SubController.java | 3 ++- .../CloudEventsProtocolAdaptor.java | 5 ++++- .../SendMessageBatchV2ProtocolResolver.java | 14 ++++++++++++-- .../SendMessageRequestProtocolResolver.java | 18 ++++++++++++++---- .../http/producer/CloudEventProducer.java | 17 ++++++++++++++++- 7 files changed, 51 insertions(+), 10 deletions(-) diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java index a6a456b51c..90d83c1531 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java @@ -32,7 +32,7 @@ public class ReplyMessageRequestBody extends Body { public static final String ORIGTOPIC = "origtopic"; public static final String BIZSEQNO = "bizseqno"; - public static final String UNIQUEID = "uniqueId"; + public static final String UNIQUEID = "uniqueid"; public static final String CONTENT = "content"; public static final String EXTFIELDS = "extFields"; public static final String PRODUCERGROUP = "producergroup"; diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java index eb79f04c95..e032f1fe9a 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java @@ -38,6 +38,8 @@ public static class ClientInstanceKey { public static final String IP = "ip"; public static final String USERNAME = "username"; public static final String PASSWD = "passwd"; + public static final String BIZSEQNO = "bizseqno"; + public static final String UNIQUEID = "uniqueid"; } diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java index 2f0089c411..74a5a6e0ac 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java @@ -43,7 +43,8 @@ public class SubController { @RequestMapping(value = "/test", method = RequestMethod.POST) public String subTest(@RequestBody String message) { - log.info("=======receive message======= {}", JsonUtils.serialize(message)); + log.info("=======receive message======= {}", message); + subService.consumeMessage(message); Map map = new HashMap<>(); diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java index 6e540e6cdd..15f516188c 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java @@ -19,6 +19,7 @@ import io.cloudevents.CloudEvent; import io.cloudevents.core.provider.EventFormatProvider; +import io.cloudevents.jackson.JsonFormat; import org.apache.commons.lang3.StringUtils; import org.apache.eventmesh.common.Constants; @@ -36,6 +37,7 @@ import org.apache.eventmesh.protocol.cloudevents.resolver.http.SendMessageRequestProtocolResolver; import org.apache.eventmesh.protocol.cloudevents.resolver.tcp.TcpMessageProtocolResolver; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -104,7 +106,8 @@ public ProtocolTransportObject fromCloudEvent(CloudEvent cloudEvent) throws Prot final Map map = new HashMap<>(); @Override public Map toMap() { - map.put("content", JsonUtils.serialize(cloudEvent)); + byte[] eventByte = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).serialize(cloudEvent); + map.put("content", new String(eventByte, StandardCharsets.UTF_8)); return map; } }; diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java index a863bb5924..a90484b060 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java @@ -20,13 +20,16 @@ import io.cloudevents.CloudEvent; import io.cloudevents.SpecVersion; import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.provider.EventFormatProvider; import io.cloudevents.core.v03.CloudEventV03; import io.cloudevents.core.v1.CloudEventV1; +import io.cloudevents.jackson.JsonFormat; import org.apache.commons.lang3.StringUtils; import org.apache.eventmesh.common.protocol.http.body.Body; import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody; +import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; import org.apache.eventmesh.common.protocol.http.header.Header; @@ -34,6 +37,8 @@ import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException; +import java.nio.charset.StandardCharsets; + public class SendMessageBatchV2ProtocolResolver { public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHandleException { try { @@ -55,11 +60,13 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan ProtocolVersion version = sendMessageBatchV2RequestHeader.getVersion(); String language = sendMessageBatchV2RequestHeader.getLanguage(); + String producerGroup = sendMessageBatchV2RequestBody.getProducerGroup(); String content = sendMessageBatchV2RequestBody.getMsg(); CloudEvent event = null; if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) { - event = JsonUtils.deserialize(content, CloudEventV1.class); + event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(content.getBytes(StandardCharsets.UTF_8)); event = CloudEventBuilder.from(event) .withExtension(ProtocolKey.REQUEST_CODE, code) .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) @@ -74,9 +81,11 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) + .withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, producerGroup) .build(); } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) { - event = JsonUtils.deserialize(content, CloudEventV03.class); + event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(content.getBytes(StandardCharsets.UTF_8)); event = CloudEventBuilder.from(event) .withExtension(ProtocolKey.REQUEST_CODE, code) .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) @@ -91,6 +100,7 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) + .withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, producerGroup) .build(); } return event; diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java index 6c80dd7cb3..f8e0791e6d 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java @@ -20,8 +20,11 @@ import io.cloudevents.CloudEvent; import io.cloudevents.SpecVersion; import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.provider.EventFormatProvider; import io.cloudevents.core.v03.CloudEventV03; import io.cloudevents.core.v1.CloudEventV1; +import io.cloudevents.jackson.JsonFormat; + import org.apache.commons.lang3.StringUtils; import org.apache.eventmesh.common.protocol.http.body.Body; import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody; @@ -32,6 +35,8 @@ import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException; +import java.nio.charset.StandardCharsets; + public class SendMessageRequestProtocolResolver { public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHandleException { @@ -54,12 +59,14 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan ProtocolVersion version = sendMessageRequestHeader.getVersion(); String language = sendMessageRequestHeader.getLanguage(); + String producerGroup = sendMessageRequestBody.getProducerGroup(); String content = sendMessageRequestBody.getContent(); CloudEvent event = null; if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) { - event = JsonUtils.deserialize(content, CloudEventV1.class); - event = CloudEventBuilder.from(event) + event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(content.getBytes(StandardCharsets.UTF_8)); + event = CloudEventBuilder.v1(event) .withExtension(ProtocolKey.REQUEST_CODE, code) .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) .withExtension(ProtocolKey.ClientInstanceKey.IDC, idc) @@ -73,10 +80,12 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) + .withExtension(SendMessageRequestBody.PRODUCERGROUP, producerGroup) .build(); } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) { - event = JsonUtils.deserialize(content, CloudEventV03.class); - event = CloudEventBuilder.from(event) + event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(content.getBytes(StandardCharsets.UTF_8)); + event = CloudEventBuilder.v03(event) .withExtension(ProtocolKey.REQUEST_CODE, code) .withExtension(ProtocolKey.ClientInstanceKey.ENV, env) .withExtension(ProtocolKey.ClientInstanceKey.IDC, idc) @@ -90,6 +99,7 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType) .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc) .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) + .withExtension(SendMessageRequestBody.PRODUCERGROUP, producerGroup) .build(); } return event; diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java index 49905ad1ac..01d6ca7cf8 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java @@ -14,13 +14,16 @@ import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion; import org.apache.eventmesh.common.protocol.http.common.RequestCode; import org.apache.eventmesh.common.utils.JsonUtils; +import org.apache.eventmesh.common.utils.RandomStringUtils; import java.io.IOException; +import java.nio.charset.StandardCharsets; import com.google.common.base.Preconditions; import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.provider.EventFormatProvider; import io.netty.handler.codec.http.HttpMethod; import lombok.extern.slf4j.Slf4j; @@ -101,17 +104,27 @@ private void validateCloudEvent(CloudEvent cloudEvent) { } private RequestParam buildCommonPostParam(CloudEvent cloudEvent) { + byte[] bodyByte = EventFormatProvider.getInstance().resolveFormat(cloudEvent.getDataContentType()) + .serialize(cloudEvent); + String content = new String(bodyByte, StandardCharsets.UTF_8); + RequestParam requestParam = new RequestParam(HttpMethod.POST); requestParam + .addHeader(ProtocolKey.ClientInstanceKey.ENV, eventMeshHttpClientConfig.getEnv()) + .addHeader(ProtocolKey.ClientInstanceKey.IDC, eventMeshHttpClientConfig.getIdc()) + .addHeader(ProtocolKey.ClientInstanceKey.IP, eventMeshHttpClientConfig.getIp()) + .addHeader(ProtocolKey.ClientInstanceKey.PID, eventMeshHttpClientConfig.getPid()) + .addHeader(ProtocolKey.ClientInstanceKey.SYS, eventMeshHttpClientConfig.getSys()) .addHeader(ProtocolKey.ClientInstanceKey.USERNAME, eventMeshHttpClientConfig.getUserName()) .addHeader(ProtocolKey.ClientInstanceKey.PASSWD, eventMeshHttpClientConfig.getPassword()) .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) .addHeader(ProtocolKey.PROTOCOL_TYPE, PROTOCOL_TYPE) .addHeader(ProtocolKey.PROTOCOL_DESC, PROTOCOL_DESC) .addHeader(ProtocolKey.PROTOCOL_VERSION, cloudEvent.getSpecVersion().toString()) + // todo: move producerGroup tp header .addBody(SendMessageRequestBody.PRODUCERGROUP, eventMeshHttpClientConfig.getProducerGroup()) - .addBody(SendMessageRequestBody.CONTENT, JsonUtils.serialize(cloudEvent)); + .addBody(SendMessageRequestBody.CONTENT, content); return requestParam; } @@ -125,6 +138,8 @@ private CloudEvent enhanceCloudEvent(final CloudEvent cloudEvent) { .withExtension(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA) .withExtension(ProtocolKey.PROTOCOL_DESC, cloudEvent.getSpecVersion().name()) .withExtension(ProtocolKey.PROTOCOL_VERSION, cloudEvent.getSpecVersion().toString()) + .withExtension(ProtocolKey.ClientInstanceKey.BIZSEQNO, RandomStringUtils.generateNum(30)) + .withExtension(ProtocolKey.ClientInstanceKey.UNIQUEID, RandomStringUtils.generateNum(30)) .build(); } From f45f89a258f66d1677757ab733184f6b966b15fc Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Fri, 3 Dec 2021 10:44:08 +0800 Subject: [PATCH 12/13] fix cloudevents protocol pub/sub for http sdk --- eventmesh-examples/build.gradle | 1 + .../pub/cloudevents/AsyncPublishInstance.java | 102 ++++++++++++++++++ .../demo/sub/controller/SubController.java | 28 +++-- .../http/push/AsyncHTTPPushRequest.java | 8 +- 4 files changed, 127 insertions(+), 12 deletions(-) create mode 100644 eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/cloudevents/AsyncPublishInstance.java diff --git a/eventmesh-examples/build.gradle b/eventmesh-examples/build.gradle index 6e57ad6722..7b3d7f6537 100644 --- a/eventmesh-examples/build.gradle +++ b/eventmesh-examples/build.gradle @@ -26,6 +26,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'io.netty:netty-all' implementation "io.cloudevents:cloudevents-core" + implementation "io.cloudevents:cloudevents-json-jackson" implementation "io.openmessaging:openmessaging-api" compileOnly 'org.projectlombok:lombok:1.18.22' diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/cloudevents/AsyncPublishInstance.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/cloudevents/AsyncPublishInstance.java new file mode 100644 index 0000000000..6bc7383254 --- /dev/null +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/cloudevents/AsyncPublishInstance.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.http.demo.pub.cloudevents; + +import org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig; +import org.apache.eventmesh.client.http.producer.EventMeshHttpProducer; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.EventMeshMessage; +import org.apache.eventmesh.common.utils.IPUtils; +import org.apache.eventmesh.common.utils.JsonUtils; +import org.apache.eventmesh.common.utils.RandomStringUtils; +import org.apache.eventmesh.common.utils.ThreadUtils; +import org.apache.eventmesh.util.Utils; + +import org.apache.commons.lang3.StringUtils; + +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.UUID; + +import io.cloudevents.CloudEvent; +import io.cloudevents.core.builder.CloudEventBuilder; +import lombok.extern.slf4j.Slf4j; +import static org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientUniCast; + +@Slf4j +public class AsyncPublishInstance { + + // This messageSize is also used in SubService.java (Subscriber) + public static int messageSize = 5; + + public static void main(String[] args) throws Exception { + Properties properties = Utils.readPropertiesFile("application.properties"); + final String eventMeshIp = properties.getProperty("eventmesh.ip"); + final String eventMeshHttpPort = properties.getProperty("eventmesh.http.port"); + + final String eventMeshIPPort; + if (StringUtils.isBlank(eventMeshIp) || StringUtils.isBlank(eventMeshHttpPort)) { + // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105 + eventMeshIPPort = "127.0.0.1:10105"; + } else { + eventMeshIPPort = eventMeshIp + ":" + eventMeshHttpPort; + } + + final String topic = "TEST-TOPIC-HTTP-ASYNC"; + + EventMeshHttpClientConfig eventMeshClientConfig = EventMeshHttpClientConfig.builder() + .liteEventMeshAddr(eventMeshIPPort) + .producerGroup("EventMeshTest-producerGroup") + .env("env") + .idc("idc") + .ip(IPUtils.getLocalAddress()) + .sys("1234") + .pid(String.valueOf(ThreadUtils.getPID())).build(); + + try (EventMeshHttpProducer eventMeshHttpProducer = new EventMeshHttpProducer(eventMeshClientConfig)) { + for (int i = 0; i < messageSize; i++) { + EventMeshMessage eventMeshMessage = EventMeshMessage.builder() + .bizSeqNo(RandomStringUtils.generateNum(30)) + .content("testPublishMessage") + .topic(topic) + .uniqueId(RandomStringUtils.generateNum(30)) + .build() + .addProp(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000)); + + Map content = new HashMap<>(); + content.put("content", "testAsyncMessage"); + + CloudEvent event = CloudEventBuilder.v1() + .withId(UUID.randomUUID().toString()) + .withSubject(topic) + .withSource(URI.create("/")) + .withDataContentType("application/cloudevents+json") + .withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME) + .withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8)) + .withExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000)) + .build(); + eventMeshHttpProducer.publish(event); + } + Thread.sleep(30000); + } + } +} diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java index 74a5a6e0ac..c82864b4be 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java @@ -17,22 +17,30 @@ package org.apache.eventmesh.http.demo.sub.controller; +import io.cloudevents.CloudEvent; +import io.cloudevents.core.provider.EventFormatProvider; +import io.cloudevents.jackson.JsonFormat; import lombok.extern.slf4j.Slf4j; +import org.apache.eventmesh.client.tcp.common.EventMeshCommon; +import org.apache.eventmesh.common.protocol.http.common.ProtocolKey; import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.http.demo.sub.service.SubService; +import org.apache.commons.lang3.StringUtils; + +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import javax.servlet.http.HttpServletRequest; + import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; + @Slf4j @RestController @RequestMapping("/sub") @@ -42,10 +50,18 @@ public class SubController { private SubService subService; @RequestMapping(value = "/test", method = RequestMethod.POST) - public String subTest(@RequestBody String message) { - log.info("=======receive message======= {}", message); + public String subTest(HttpServletRequest request) { + String content = request.getParameter("content"); + log.info("=======receive message======= {}", content); + Map contentMap = JsonUtils.deserialize(content, HashMap.class); + if (StringUtils.equals(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME, contentMap.get(ProtocolKey.PROTOCOL_TYPE))) { + CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) + .deserialize(content.getBytes(StandardCharsets.UTF_8)); + String data = new String(event.getData().toBytes(), StandardCharsets.UTF_8); + log.info("=======receive data======= {}", data); + } - subService.consumeMessage(message); + subService.consumeMessage(content); Map map = new HashMap<>(); map.put("retCode", 1); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java index 0bcf02c6c6..43750ad446 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java @@ -50,8 +50,8 @@ import org.apache.http.util.EntityUtils; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.*; import org.slf4j.Logger; @@ -152,11 +152,7 @@ public void tryHTTPRequest() { body.add(new BasicNameValuePair(PushMessageRequestBody.EXTFIELDS, JsonUtils.serialize(EventMeshUtil.getEventProp(handleMsgContext.getEvent())))); - try { - builder.setEntity(new UrlEncodedFormEntity(body)); - } catch (UnsupportedEncodingException e) { - return; - } + builder.setEntity(new UrlEncodedFormEntity(body, StandardCharsets.UTF_8)); eventMeshHTTPServer.metrics.summaryMetrics.recordPushMsg(); From 7de5b51ea635fa26fa4237ac1643659e2968fa2c Mon Sep 17 00:00:00 2001 From: xwm1992 Date: Mon, 6 Dec 2021 20:37:31 +0800 Subject: [PATCH 13/13] fix cloudevents/eventmesh message protocol pub/sub for sdk in rocketmq-connector --- .../apache/eventmesh/common/Constants.java | 22 ++++++++--------- .../common/protocol/tcp/codec/Codec.java | 2 +- .../rocketmq/common/EventMeshConstants.java | 24 +++++++++---------- .../rocketmq/consumer/PushConsumerImpl.java | 7 ++++-- .../rocketmq/utils/CloudEventUtils.java | 2 +- .../pub/cloudevents/AsyncPublishInstance.java | 8 ------- .../AsyncPublishInstance.java | 16 +++++++++---- .../demo/sub/cloudevents/AsyncSubscribe.java | 3 ++- .../SendMessageBatchV2ProtocolResolver.java | 20 +++++++++------- .../SendMessageRequestProtocolResolver.java | 20 +++++++++------- .../cloudevent/CloudEventTCPSubClient.java | 1 - 11 files changed, 67 insertions(+), 58 deletions(-) diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java index 20c1b4e742..e5c3e5de0e 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java @@ -57,28 +57,28 @@ public class Constants { public static final String KEY_CONSTANTS_INSTANCE_DESC_PID = "pid"; - public static final String RMB_UNIQ_ID = "RMB_UNIQ_ID"; + public static final String RMB_UNIQ_ID = "rmbuniqid"; public static final String IDC_SEPERATER = "-"; - public static final String PROPERTY_MESSAGE_TIMEOUT = "TIMEOUT"; + public static final String PROPERTY_MESSAGE_TIMEOUT = "timeout"; - public static final String PROPERTY_MESSAGE_SEARCH_KEYS = "SEARCH_KEYS"; + public static final String PROPERTY_MESSAGE_SEARCH_KEYS = "searchkeys"; - public static final String PROPERTY_MESSAGE_QUEUE_ID = "QUEUE_ID"; + public static final String PROPERTY_MESSAGE_QUEUE_ID = "queueid"; - public static final String PROPERTY_MESSAGE_QUEUE_OFFSET = "QUEUE_OFFSET"; + public static final String PROPERTY_MESSAGE_QUEUE_OFFSET = "queueoffset"; - public static final String PROPERTY_MESSAGE_DESTINATION = "DESTINATION"; + public static final String PROPERTY_MESSAGE_DESTINATION = "destination"; - public static final String PROPERTY_MESSAGE_MESSAGE_ID = "MESSAGE_ID"; + public static final String PROPERTY_MESSAGE_MESSAGE_ID = "messageid"; - public static final String PROPERTY_MESSAGE_BORN_HOST = "BORN_HOST"; + public static final String PROPERTY_MESSAGE_BORN_HOST = "bornhost"; - public static final String PROPERTY_MESSAGE_BORN_TIMESTAMP = "BORN_TIMESTAMP"; + public static final String PROPERTY_MESSAGE_BORN_TIMESTAMP = "borntimestamp"; - public static final String PROPERTY_MESSAGE_STORE_HOST = "STORE_HOST"; + public static final String PROPERTY_MESSAGE_STORE_HOST = "storehost"; - public static final String PROPERTY_MESSAGE_STORE_TIMESTAMP = "STORE_TIMESTAMP"; + public static final String PROPERTY_MESSAGE_STORE_TIMESTAMP = "storetimestamp"; } diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java index e94587f1c4..b9508f6b40 100644 --- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java @@ -213,7 +213,7 @@ private static Object deserializeBody(String bodyJsonString, Header header) thro case REDIRECT_TO_CLIENT: return OBJECT_MAPPER.readValue(bodyJsonString, RedirectInfo.class); default: - log.error("Invalidate TCP command: {}", command); + log.warn("Invalidate TCP command: {}", command); return null; } } diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/EventMeshConstants.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/EventMeshConstants.java index 69fb8c10dc..c815df9730 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/EventMeshConstants.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/common/EventMeshConstants.java @@ -69,33 +69,33 @@ public class EventMeshConstants { public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; - public static final String BORN_TIMESTAMP = "BORN_TIME"; - public static final String STORE_TIMESTAMP = "STORE_TIME"; - public static final String LEAVE_TIMESTAMP = "LEAVE_TIME"; - public static final String ARRIVE_TIMESTAMP = "ARRIVE_TIME"; + public static final String BORN_TIMESTAMP = "borntime"; + public static final String STORE_TIMESTAMP = "storetime"; + public static final String LEAVE_TIMESTAMP = "leavetime"; + public static final String ARRIVE_TIMESTAMP = "arrivetime"; public static final String KEYS_UPPERCASE = "KEYS"; public static final String KEYS_LOWERCASE = "keys"; public static final String RR_REQUEST_UNIQ_ID = "RR_REQUEST_UNIQ_ID"; - public static final String TTL = "TTL"; + public static final String TTL = "ttl"; - public static final String TAG = "TAG"; + public static final String TAG = "tag"; - public static final String MANAGE_SUBSYSTEM = "subSystem"; + public static final String MANAGE_SUBSYSTEM = "subsystem"; public static final String MANAGE_IP = "ip"; public static final String MANAGE_PORT = "port"; - public static final String MANAGE_DEST_IP = "destEventMeshIp"; - public static final String MANAGE_DEST_PORT = "destEventMeshPort"; + public static final String MANAGE_DEST_IP = "desteventmeship"; + public static final String MANAGE_DEST_PORT = "desteventmeshport"; public static final String MANAGE_PATH = "path"; public static final String MANAGE_GROUP = "group"; public static final String MANAGE_PURPOSE = "purpose"; public static final String MANAGE_TOPIC = "topic"; - public static final String EventMesh_SEND_BACK_TIMES = "EventMeshSendBackTimes"; + public static final String EventMesh_SEND_BACK_TIMES = "eventmeshsendbacktimes"; - public static final String EventMesh_SEND_BACK_IP = "EventMeshSendBackIp"; + public static final String EventMesh_SEND_BACK_IP = "eventmeshsendbackip"; - public static final String EventMesh_REGISTRY_ADDR_KEY = "EventMeshRegistryAddr"; + public static final String EventMesh_REGISTRY_ADDR_KEY = "eventmeshregistryaddr"; public static int DEFAULT_TIME_OUT_MILLS = 5 * 1000; diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java index dbc07030ea..83049e62ae 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java @@ -21,6 +21,7 @@ import org.apache.eventmesh.api.AsyncConsumeContext; import org.apache.eventmesh.api.EventListener; import org.apache.eventmesh.api.EventMeshAction; +import org.apache.eventmesh.api.EventMeshAsyncConsumeContext; import org.apache.eventmesh.api.exception.ConnectorRuntimeException; import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.connector.rocketmq.cloudevent.RocketMQMessageFactory; @@ -250,7 +251,7 @@ public EventMeshConsumeConcurrentlyStatus handleMessage(MessageExt msg, contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, EventMeshConsumeConcurrentlyStatus.RECONSUME_LATER.name()); - AsyncConsumeContext asyncConsumeContext = new AsyncConsumeContext() { + EventMeshAsyncConsumeContext eventMeshAsyncConsumeContext = new EventMeshAsyncConsumeContext() { @Override public void commit(EventMeshAction action) { switch (action) { @@ -272,7 +273,9 @@ public void commit(EventMeshAction action) { } }; - listener.consume(cloudEvent, asyncConsumeContext); + eventMeshAsyncConsumeContext.setAbstractContext(context); + + listener.consume(cloudEvent, eventMeshAsyncConsumeContext); return EventMeshConsumeConcurrentlyStatus.valueOf( contextProperties.getProperty(NonStandardKeys.MESSAGE_CONSUME_STATUS)); diff --git a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/CloudEventUtils.java b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/CloudEventUtils.java index 1920ce8cbd..5e634855d3 100644 --- a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/CloudEventUtils.java +++ b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/utils/CloudEventUtils.java @@ -64,7 +64,7 @@ public static Message msgConvert(MessageExt rmqMsg) { String.valueOf(rmqMsg.getBornTimestamp())); MessageAccessor.putProperty(message, buildCloudEventPropertyKey(Constants.PROPERTY_MESSAGE_STORE_HOST), String.valueOf(rmqMsg.getStoreHost())); - MessageAccessor.putProperty(message, buildCloudEventPropertyKey("STORE_TIMESTAMP"), + MessageAccessor.putProperty(message, buildCloudEventPropertyKey(Constants.PROPERTY_MESSAGE_STORE_TIMESTAMP), String.valueOf(rmqMsg.getStoreTimestamp())); //use in manual ack diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/cloudevents/AsyncPublishInstance.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/cloudevents/AsyncPublishInstance.java index 6bc7383254..98bdd9e622 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/cloudevents/AsyncPublishInstance.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/cloudevents/AsyncPublishInstance.java @@ -74,14 +74,6 @@ public static void main(String[] args) throws Exception { try (EventMeshHttpProducer eventMeshHttpProducer = new EventMeshHttpProducer(eventMeshClientConfig)) { for (int i = 0; i < messageSize; i++) { - EventMeshMessage eventMeshMessage = EventMeshMessage.builder() - .bizSeqNo(RandomStringUtils.generateNum(30)) - .content("testPublishMessage") - .topic(topic) - .uniqueId(RandomStringUtils.generateNum(30)) - .build() - .addProp(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000)); - Map content = new HashMap<>(); content.put("content", "testAsyncMessage"); diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/eventmeshmessage/AsyncPublishInstance.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/eventmeshmessage/AsyncPublishInstance.java index 8c90d8e321..751cb2cf97 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/eventmeshmessage/AsyncPublishInstance.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/pub/eventmeshmessage/AsyncPublishInstance.java @@ -24,17 +24,17 @@ import org.apache.eventmesh.common.Constants; import org.apache.eventmesh.common.EventMeshMessage; import org.apache.eventmesh.common.utils.IPUtils; +import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.common.utils.RandomStringUtils; import org.apache.eventmesh.common.utils.ThreadUtils; import org.apache.eventmesh.util.Utils; import org.apache.commons.lang3.StringUtils; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - @Slf4j public class AsyncPublishInstance { @@ -63,13 +63,19 @@ public static void main(String[] args) throws Exception { .idc("idc") .ip(IPUtils.getLocalAddress()) .sys("1234") - .pid(String.valueOf(ThreadUtils.getPID())).build(); + .pid(String.valueOf(ThreadUtils.getPID())) + .userName("eventmesh") + .password("pass") + .build(); try (EventMeshHttpProducer eventMeshHttpProducer = new EventMeshHttpProducer(eventMeshClientConfig);) { for (int i = 0; i < messageSize; i++) { + Map content = new HashMap<>(); + content.put("content", "testPublishMessage"); + EventMeshMessage eventMeshMessage = EventMeshMessage.builder() .bizSeqNo(RandomStringUtils.generateNum(30)) - .content("testPublishMessage") + .content(JsonUtils.serialize(content)) .topic(topic) .uniqueId(RandomStringUtils.generateNum(30)) .build() diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java index c6e4bda682..99653b1673 100644 --- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java +++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/demo/sub/cloudevents/AsyncSubscribe.java @@ -24,6 +24,7 @@ import org.apache.eventmesh.common.protocol.SubscriptionMode; import org.apache.eventmesh.common.protocol.SubscriptionType; import org.apache.eventmesh.common.protocol.tcp.UserAgent; +import org.apache.eventmesh.tcp.common.EventMeshTestCaseTopicSet; import org.apache.eventmesh.tcp.common.EventMeshTestUtils; import org.apache.eventmesh.util.Utils; @@ -55,7 +56,7 @@ public static void main(String[] agrs) throws Exception { client = EventMeshTCPClientFactory.createEventMeshTCPClient(eventMeshTcpClientConfig, CloudEvent.class); client.init(); - client.subscribe("TEST-TOPIC-TCP-ASYNC", SubscriptionMode.CLUSTERING, SubscriptionType.ASYNC); + client.subscribe(EventMeshTestCaseTopicSet.TOPIC_PRX_WQ2ClientUniCast, SubscriptionMode.CLUSTERING, SubscriptionType.ASYNC); client.registerSubBusiHandler(handler); client.listen(); diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageBatchV2ProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageBatchV2ProtocolResolver.java index f7c06bacf1..0f3678fae7 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageBatchV2ProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageBatchV2ProtocolResolver.java @@ -61,7 +61,7 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) { cloudEventBuilder = CloudEventBuilder.v1(); - event = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()) + cloudEventBuilder = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()) .withSubject(sendMessageBatchV2RequestBody.getTopic()) .withType("eventmeshmessage") .withSource(URI.create("/")) @@ -81,12 +81,14 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) .withExtension(SendMessageBatchV2RequestBody.BIZSEQNO, sendMessageBatchV2RequestBody.getBizSeqNo()) .withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, sendMessageBatchV2RequestBody.getProducerGroup()) - .withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageBatchV2RequestBody.getTtl()) - .withExtension(SendMessageBatchV2RequestBody.TAG, sendMessageBatchV2RequestBody.getTag()) - .build(); + .withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageBatchV2RequestBody.getTtl()); + if (StringUtils.isNotEmpty(sendMessageBatchV2RequestBody.getTag())) { + cloudEventBuilder = cloudEventBuilder.withExtension(SendMessageRequestBody.TAG, sendMessageBatchV2RequestBody.getTag()); + } + event = cloudEventBuilder.build(); } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) { cloudEventBuilder = CloudEventBuilder.v03(); - event = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()) + cloudEventBuilder = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()) .withSubject(sendMessageBatchV2RequestBody.getTopic()) .withType("eventmeshmessage") .withSource(URI.create("/")) @@ -106,9 +108,11 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion) .withExtension(SendMessageBatchV2RequestBody.BIZSEQNO, sendMessageBatchV2RequestBody.getBizSeqNo()) .withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, sendMessageBatchV2RequestBody.getProducerGroup()) - .withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageBatchV2RequestBody.getTtl()) - .withExtension(SendMessageBatchV2RequestBody.TAG, sendMessageBatchV2RequestBody.getTag()) - .build(); + .withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageBatchV2RequestBody.getTtl()); + if (StringUtils.isNotEmpty(sendMessageBatchV2RequestBody.getTag())) { + cloudEventBuilder = cloudEventBuilder.withExtension(SendMessageRequestBody.TAG, sendMessageBatchV2RequestBody.getTag()); + } + event = cloudEventBuilder.build(); } return event; } catch (Exception e) { diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageRequestProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageRequestProtocolResolver.java index 41d710345d..ace5bfeadd 100644 --- a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageRequestProtocolResolver.java +++ b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/http/SendMessageRequestProtocolResolver.java @@ -64,7 +64,7 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) { cloudEventBuilder = CloudEventBuilder.v1(); - event = cloudEventBuilder.withId(sendMessageRequestBody.getBizSeqNo()) + cloudEventBuilder = cloudEventBuilder.withId(sendMessageRequestBody.getBizSeqNo()) .withSubject(sendMessageRequestBody.getTopic()) .withType("eventmeshmessage") .withSource(URI.create("/")) @@ -86,12 +86,14 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(SendMessageRequestBody.UNIQUEID, sendMessageRequestBody.getUniqueId()) .withExtension(SendMessageRequestBody.PRODUCERGROUP, sendMessageRequestBody.getProducerGroup()) - .withExtension(SendMessageRequestBody.TTL, sendMessageRequestBody.getTtl()) - .withExtension(SendMessageRequestBody.TAG, sendMessageRequestBody.getTag()) - .build(); + .withExtension(SendMessageRequestBody.TTL, sendMessageRequestBody.getTtl()); + if (StringUtils.isNotEmpty(sendMessageRequestBody.getTag())) { + cloudEventBuilder = cloudEventBuilder.withExtension(SendMessageRequestBody.TAG, sendMessageRequestBody.getTag()); + } + event = cloudEventBuilder.build(); } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) { cloudEventBuilder = CloudEventBuilder.v03(); - event = cloudEventBuilder.withId(sendMessageRequestBody.getBizSeqNo()) + cloudEventBuilder = cloudEventBuilder.withId(sendMessageRequestBody.getBizSeqNo()) .withSubject(sendMessageRequestBody.getTopic()) .withType("eventmeshmessage") .withSource(URI.create("/")) @@ -113,9 +115,11 @@ public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHan .withExtension(SendMessageRequestBody.UNIQUEID, sendMessageRequestBody.getUniqueId()) .withExtension(SendMessageRequestBody.PRODUCERGROUP, sendMessageRequestBody.getProducerGroup()) - .withExtension(SendMessageRequestBody.TTL, sendMessageRequestBody.getTtl()) - .withExtension(SendMessageRequestBody.TAG, sendMessageRequestBody.getTag()) - .build(); + .withExtension(SendMessageRequestBody.TTL, sendMessageRequestBody.getTtl()); + if (StringUtils.isNotEmpty(sendMessageRequestBody.getTag())) { + cloudEventBuilder = cloudEventBuilder.withExtension(SendMessageRequestBody.TAG, sendMessageRequestBody.getTag()); + } + event = cloudEventBuilder.build(); } return event; } catch (Exception e) { diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java index 1f1f7a09e8..87a2d37962 100644 --- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java +++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/impl/cloudevent/CloudEventTCPSubClient.java @@ -158,7 +158,6 @@ private class Handler extends SimpleChannelInboundHandler { protected void channelRead0(ChannelHandlerContext ctx, Package msg) throws Exception { Command cmd = msg.getHeader().getCmd(); log.info("|receive|type={}|msg={}", cmd, msg); - String protocolVersion = msg.getHeader().getProperty(Constants.PROTOCOL_VERSION).toString(); if (cmd == Command.REQUEST_TO_CLIENT) { Package pkg = requestToClientAck(msg); if (callback != null) {