forked from apache/eventmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue apache#417] support Grpc broadcast async publish
- Loading branch information
1 parent
a8bce21
commit adf637c
Showing
5 changed files
with
283 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...s/src/main/java/org/apache/eventmesh/grpc/pub/eventmeshmessage/AsyncPublishBroadcast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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.grpc.pub.eventmeshmessage; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig; | ||
import org.apache.eventmesh.client.grpc.producer.EventMeshGrpcProducer; | ||
import org.apache.eventmesh.common.Constants; | ||
import org.apache.eventmesh.common.EventMeshMessage; | ||
import org.apache.eventmesh.common.utils.JsonUtils; | ||
import org.apache.eventmesh.common.utils.RandomStringUtils; | ||
import org.apache.eventmesh.util.Utils; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
@Slf4j | ||
public class AsyncPublishBroadcast { | ||
|
||
// 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 eventMeshGrpcPort = properties.getProperty("eventmesh.grpc.port"); | ||
|
||
final String topic = "TEST-TOPIC-GRPC-BROADCAST"; | ||
|
||
EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder() | ||
.serverAddr(eventMeshIp) | ||
.serverPort(Integer.parseInt(eventMeshGrpcPort)) | ||
.producerGroup("EventMeshTest-producerGroup") | ||
.env("env").idc("idc") | ||
.sys("1234").build(); | ||
|
||
EventMeshGrpcProducer eventMeshGrpcProducer = new EventMeshGrpcProducer(eventMeshClientConfig); | ||
|
||
eventMeshGrpcProducer.init(); | ||
|
||
Map<String, String> content = new HashMap<>(); | ||
content.put("content", "testAsyncMessage"); | ||
|
||
for (int i = 0; i < messageSize; i++) { | ||
EventMeshMessage eventMeshMessage = EventMeshMessage.builder() | ||
.content(JsonUtils.serialize(content)) | ||
.topic(topic) | ||
.uniqueId(RandomStringUtils.generateNum(30)) | ||
.bizSeqNo(RandomStringUtils.generateNum(30)) | ||
.build() | ||
.addProp(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000)); | ||
eventMeshGrpcProducer.publish(eventMeshMessage); | ||
Thread.sleep(1000); | ||
} | ||
Thread.sleep(30000); | ||
try (EventMeshGrpcProducer ignore = eventMeshGrpcProducer) { | ||
// ignore | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...esh-examples/src/main/java/org/apache/eventmesh/grpc/sub/EventmeshSubscribeBroadcast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.apache.eventmesh.grpc.sub; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig; | ||
import org.apache.eventmesh.client.grpc.consumer.EventMeshGrpcConsumer; | ||
import org.apache.eventmesh.client.grpc.consumer.ReceiveMsgHook; | ||
import org.apache.eventmesh.client.tcp.common.EventMeshCommon; | ||
import org.apache.eventmesh.common.EventMeshMessage; | ||
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.util.Utils; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
|
||
@Slf4j | ||
public class EventmeshSubscribeBroadcast implements ReceiveMsgHook<EventMeshMessage> { | ||
|
||
public static EventmeshSubscribeBroadcast handler = new EventmeshSubscribeBroadcast(); | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Properties properties = Utils.readPropertiesFile("application.properties"); | ||
final String eventMeshIp = properties.getProperty("eventmesh.ip"); | ||
final String eventMeshGrpcPort = properties.getProperty("eventmesh.grpc.port"); | ||
|
||
final String topic = "TEST-TOPIC-GRPC-BROADCAST"; | ||
|
||
EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder() | ||
.serverAddr(eventMeshIp) | ||
.serverPort(Integer.parseInt(eventMeshGrpcPort)) | ||
.consumerGroup("EventMeshTest-consumerGroup") | ||
.env("env").idc("idc") | ||
.sys("1234").build(); | ||
|
||
SubscriptionItem subscriptionItem = new SubscriptionItem(); | ||
subscriptionItem.setTopic(topic); | ||
subscriptionItem.setMode(SubscriptionMode.BROADCASTING); | ||
subscriptionItem.setType(SubscriptionType.ASYNC); | ||
|
||
EventMeshGrpcConsumer eventMeshGrpcConsumer = new EventMeshGrpcConsumer(eventMeshClientConfig); | ||
|
||
eventMeshGrpcConsumer.init(); | ||
|
||
eventMeshGrpcConsumer.registerListener(handler); | ||
|
||
eventMeshGrpcConsumer.subscribe(Collections.singletonList(subscriptionItem)); | ||
|
||
Thread.sleep(60000); | ||
eventMeshGrpcConsumer.unsubscribe(Collections.singletonList(subscriptionItem)); | ||
} | ||
|
||
@Override | ||
public Optional<EventMeshMessage> handle(EventMeshMessage msg) { | ||
log.info("receive async broadcast msg====================={}", msg); | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public String getProtocolType() { | ||
return EventMeshCommon.EM_MESSAGE_PROTOCOL_NAME; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.