gRPC Protocol

EventMesh SDK for Java implements the gRPC producer and consumer of synchronous, asynchronous, and broadcast messages. Both the producer and consumer require an instance of EventMeshGrpcClientConfig class that specifies the configuration of EventMesh gRPC client. The liteEventMeshAddr, userName, and password fields should match the eventmesh.properties file of EventMesh runtime.

  1. import org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig;
  2. import org.apache.eventmesh.client.grpc.consumer.ReceiveMsgHook;
  3. import io.cloudevents.CloudEvent;
  4. public class CloudEventsAsyncSubscribe implements ReceiveMsgHook<CloudEvent> {
  5. public static void main(String[] args) throws InterruptedException {
  6. EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
  7. .serverAddr("localhost")
  8. .serverPort(10205)
  9. .consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
  10. .env("env").idc("idc")
  11. .sys("1234").build();
  12. /* ... */
  13. }
  14. }

gRPC Consumer

Stream Consumer

The EventMesh runtime sends the message from producers to the stream consumer as a series of event streams. The consumer should implement the ReceiveMsgHook class, which is defined in ReceiveMsgHook.java.

  1. public interface ReceiveMsgHook<T> {
  2. Optional<T> handle(T msg) throws Throwable;
  3. String getProtocolType();
  4. }

The EventMeshGrpcConsumer class implements the registerListener, subscribe, and unsubscribe methods. The subscribe method accepts a list of SubscriptionItem that defines the topics to be subscribed to. The registerListener accepts an instance of a class that implements the ReceiveMsgHook. The handle method will be invoked when the consumer receives a message from the topic it subscribes. If the SubscriptionType is SYNC, the return value of handle will be sent back to the producer.

  1. import org.apache.eventmesh.client.grpc.consumer.EventMeshGrpcConsumer;
  2. import org.apache.eventmesh.client.grpc.consumer.ReceiveMsgHook;
  3. import org.apache.eventmesh.client.tcp.common.EventMeshCommon;
  4. import org.apache.eventmesh.common.protocol.SubscriptionItem;
  5. import org.apache.eventmesh.common.protocol.SubscriptionMode;
  6. import org.apache.eventmesh.common.protocol.SubscriptionType;
  7. import io.cloudevents.CloudEvent;
  8. public class CloudEventsAsyncSubscribe implements ReceiveMsgHook<CloudEvent> {
  9. public static CloudEventsAsyncSubscribe handler = new CloudEventsAsyncSubscribe();
  10. public static void main(String[] args) throws InterruptedException {
  11. /* ... */
  12. SubscriptionItem subscriptionItem = new SubscriptionItem(
  13. "eventmesh-async-topic",
  14. SubscriptionMode.CLUSTERING,
  15. SubscriptionType.ASYNC
  16. );
  17. EventMeshGrpcConsumer eventMeshGrpcConsumer = new EventMeshGrpcConsumer(eventMeshClientConfig);
  18. eventMeshGrpcConsumer.init();
  19. eventMeshGrpcConsumer.registerListener(handler);
  20. eventMeshGrpcConsumer.subscribe(Collections.singletonList(subscriptionItem));
  21. /* ... */
  22. eventMeshGrpcConsumer.unsubscribe(Collections.singletonList(subscriptionItem));
  23. }
  24. @Override
  25. public Optional<CloudEvent> handle(CloudEvent message) {
  26. log.info("Messaged received: {}", message);
  27. return Optional.empty();
  28. }
  29. @Override
  30. public String getProtocolType() {
  31. return EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME;
  32. }
  33. }

Webhook Consumer

The subscribe method of the EventMeshGrpcConsumer class accepts a list of SubscriptionItem that defines the topics to be subscribed and an optional callback URL. If the callback URL is provided, the EventMesh runtime will send a POST request that contains the message in the CloudEvents format to the callback URL. The SubController.java file implements a Spring Boot controller that receives and parses the callback messages.

  1. import org.apache.eventmesh.client.grpc.consumer.EventMeshGrpcConsumer;
  2. import org.apache.eventmesh.client.grpc.consumer.ReceiveMsgHook;
  3. import org.apache.eventmesh.client.tcp.common.EventMeshCommon;
  4. import org.apache.eventmesh.common.protocol.SubscriptionItem;
  5. import org.apache.eventmesh.common.protocol.SubscriptionMode;
  6. import org.apache.eventmesh.common.protocol.SubscriptionType;
  7. @Component
  8. public class SubService implements InitializingBean {
  9. final String url = "http://localhost:8080/callback";
  10. public void afterPropertiesSet() throws Exception {
  11. /* ... */
  12. eventMeshGrpcConsumer = new EventMeshGrpcConsumer(eventMeshClientConfig);
  13. eventMeshGrpcConsumer.init();
  14. SubscriptionItem subscriptionItem = new SubscriptionItem(
  15. "eventmesh-async-topic",
  16. SubscriptionMode.CLUSTERING,
  17. SubscriptionType.ASYNC
  18. );
  19. eventMeshGrpcConsumer.subscribe(Collections.singletonList(subscriptionItem), url);
  20. /* ... */
  21. eventMeshGrpcConsumer.unsubscribe(Collections.singletonList(subscriptionItem), url);
  22. }
  23. }

gRPC Producer

Asynchronous Producer

The EventMeshGrpcProducer class implements the publish method. The publish method accepts the message to be published and an optional timeout value. The message should be an instance of either of these classes:

  • org.apache.eventmesh.common.EventMeshMessage
  • io.cloudevents.CloudEvent
  1. /* ... */
  2. EventMeshGrpcProducer eventMeshGrpcProducer = new EventMeshGrpcProducer(eventMeshClientConfig);
  3. eventMeshGrpcProducer.init();
  4. Map<String, String> content = new HashMap<>();
  5. content.put("content", "testAsyncMessage");
  6. CloudEvent event = CloudEventBuilder.v1()
  7. .withId(UUID.randomUUID().toString())
  8. .withSubject(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC)
  9. .withSource(URI.create("/"))
  10. .withDataContentType(ExampleConstants.CLOUDEVENT_CONTENT_TYPE)
  11. .withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME)
  12. .withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8))
  13. .withExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000))
  14. .build();
  15. eventMeshGrpcProducer.publish(event);

Synchronous Producer

The EventMeshGrpcProducer class implements the requestReply method. The requestReply method accepts the message to be published and an optional timeout value. The method returns the message returned from the consumer. The message should be an instance of either of these classes:

  • org.apache.eventmesh.common.EventMeshMessage
  • io.cloudevents.CloudEvent

Batch Producer

The EventMeshGrpcProducer class overloads the publish method, which accepts a list of messages to be published and an optional timeout value. The messages in the list should be an instance of either of these classes:

  • org.apache.eventmesh.common.EventMeshMessage
  • io.cloudevents.CloudEvent
  1. /* ... */
  2. List<CloudEvent> cloudEventList = new ArrayList<>();
  3. for (int i = 0; i < 5; i++) {
  4. CloudEvent event = CloudEventBuilder.v1()
  5. .withId(UUID.randomUUID().toString())
  6. .withSubject(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC)
  7. .withSource(URI.create("/"))
  8. .withDataContentType(ExampleConstants.CLOUDEVENT_CONTENT_TYPE)
  9. .withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME)
  10. .withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8))
  11. .withExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000))
  12. .build();
  13. cloudEventList.add(event);
  14. }
  15. eventMeshGrpcProducer.publish(cloudEventList);
  16. /* ... */