use of org.apache.eventmesh.common.EventMeshMessage in project incubator-eventmesh by apache.
the class AsyncPublishInstance method main.
public static void main(String[] args) throws Exception {
Properties properties = Utils.readPropertiesFile(ExampleConstants.CONFIG_FILE_NAME);
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);
EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder().serverAddr(eventMeshIp).serverPort(Integer.parseInt(eventMeshGrpcPort)).producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP).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(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_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
}
}
use of org.apache.eventmesh.common.EventMeshMessage in project incubator-eventmesh by apache.
the class BatchPublishInstance method main.
public static void main(String[] args) throws Exception {
Properties properties = Utils.readPropertiesFile(ExampleConstants.CONFIG_FILE_NAME);
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);
EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder().serverAddr(eventMeshIp).serverPort(Integer.parseInt(eventMeshGrpcPort)).producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP).env("env").idc("idc").sys("1234").build();
EventMeshGrpcProducer eventMeshGrpcProducer = new EventMeshGrpcProducer(eventMeshClientConfig);
eventMeshGrpcProducer.init();
Map<String, String> content = new HashMap<>();
content.put("content", "testRequestReplyMessage");
List<EventMeshMessage> messageList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
EventMeshMessage message = EventMeshMessage.builder().topic(ExampleConstants.EVENTMESH_GRPC_ASYNC_TEST_TOPIC).content((JsonUtils.serialize(content))).uniqueId(RandomStringUtils.generateNum(30)).bizSeqNo(RandomStringUtils.generateNum(30)).build().addProp(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000));
messageList.add(message);
}
eventMeshGrpcProducer.publish(messageList);
Thread.sleep(10000);
try (EventMeshGrpcProducer ignore = eventMeshGrpcProducer) {
// ignore
}
}
use of org.apache.eventmesh.common.EventMeshMessage in project incubator-eventmesh by apache.
the class RequestReplyInstance method main.
public static void main(String[] args) throws Exception {
Properties properties = Utils.readPropertiesFile(ExampleConstants.CONFIG_FILE_NAME);
final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
final String eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);
EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder().serverAddr(eventMeshIp).serverPort(Integer.parseInt(eventMeshGrpcPort)).producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP).env("env").idc("idc").sys("1234").build();
EventMeshGrpcProducer eventMeshGrpcProducer = new EventMeshGrpcProducer(eventMeshClientConfig);
eventMeshGrpcProducer.init();
Map<String, String> content = new HashMap<>();
content.put("content", "testRequestReplyMessage");
for (int i = 0; i < messageSize; i++) {
EventMeshMessage eventMeshMessage = EventMeshMessage.builder().content(JsonUtils.serialize(content)).topic(ExampleConstants.EVENTMESH_GRPC_RR_TEST_TOPIC).uniqueId(RandomStringUtils.generateNum(30)).bizSeqNo(RandomStringUtils.generateNum(30)).build().addProp(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000));
eventMeshGrpcProducer.requestReply(eventMeshMessage, EventMeshCommon.DEFAULT_TIME_OUT_MILLS);
Thread.sleep(1000);
}
Thread.sleep(30000);
try (EventMeshGrpcProducer ignore = eventMeshGrpcProducer) {
// ignore
}
}
use of org.apache.eventmesh.common.EventMeshMessage in project incubator-eventmesh by apache.
the class EventMeshGrpcProducer method requestReply.
public EventMeshMessage requestReply(EventMeshMessage message, int timeout) {
logger.info("RequestReply message " + message.toString());
SimpleMessage simpleMessage = EventMeshClientUtil.buildSimpleMessage(message, clientConfig, PROTOCOL_TYPE);
try {
SimpleMessage reply = publisherClient.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).requestReply(simpleMessage);
logger.info("Received reply message" + reply.toString());
Object msg = EventMeshClientUtil.buildMessage(reply, PROTOCOL_TYPE);
if (msg instanceof EventMeshMessage) {
return (EventMeshMessage) msg;
} else {
return null;
}
} catch (Exception e) {
logger.error("Error in RequestReply message {}, error {}", message, e.getMessage());
return null;
}
}
use of org.apache.eventmesh.common.EventMeshMessage in project incubator-eventmesh by apache.
the class EventMeshClientUtil method buildSimpleMessage.
public static <T> SimpleMessage buildSimpleMessage(T message, EventMeshGrpcClientConfig clientConfig, String protocolType) {
if (EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME.equals(protocolType)) {
CloudEvent cloudEvent = (CloudEvent) message;
String contentType = StringUtils.isEmpty(cloudEvent.getDataContentType()) ? "application/cloudevents+json" : cloudEvent.getDataContentType();
byte[] bodyByte = EventFormatProvider.getInstance().resolveFormat(contentType).serialize(cloudEvent);
String content = new String(bodyByte, StandardCharsets.UTF_8);
String ttl = cloudEvent.getExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL) == null ? "4000" : cloudEvent.getExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL).toString();
String seqNum = cloudEvent.getExtension(ProtocolKey.SEQ_NUM) == null ? RandomStringUtils.generateNum(30) : cloudEvent.getExtension(ProtocolKey.SEQ_NUM).toString();
String uniqueId = cloudEvent.getExtension(ProtocolKey.UNIQUE_ID) == null ? RandomStringUtils.generateNum(30) : cloudEvent.getExtension(ProtocolKey.UNIQUE_ID).toString();
SimpleMessage.Builder builder = SimpleMessage.newBuilder().setHeader(EventMeshClientUtil.buildHeader(clientConfig, protocolType)).setProducerGroup(clientConfig.getProducerGroup()).setTopic(cloudEvent.getSubject()).setTtl(ttl).setSeqNum(seqNum).setUniqueId(uniqueId).setContent(content).putProperties(ProtocolKey.CONTENT_TYPE, contentType);
for (String extName : cloudEvent.getExtensionNames()) {
builder.putProperties(extName, cloudEvent.getExtension(extName).toString());
}
return builder.build();
} else {
EventMeshMessage eventMeshMessage = (EventMeshMessage) message;
String ttl = eventMeshMessage.getProp(Constants.EVENTMESH_MESSAGE_CONST_TTL) == null ? "4000" : eventMeshMessage.getProp(Constants.EVENTMESH_MESSAGE_CONST_TTL);
Map<String, String> props = eventMeshMessage.getProp() == null ? new HashMap<>() : eventMeshMessage.getProp();
String seqNum = eventMeshMessage.getBizSeqNo() == null ? RandomStringUtils.generateNum(30) : eventMeshMessage.getBizSeqNo();
String uniqueId = eventMeshMessage.getUniqueId() == null ? RandomStringUtils.generateNum(30) : eventMeshMessage.getUniqueId();
return SimpleMessage.newBuilder().setHeader(EventMeshClientUtil.buildHeader(clientConfig, protocolType)).setProducerGroup(clientConfig.getProducerGroup()).setTopic(eventMeshMessage.getTopic()).setContent(eventMeshMessage.getContent()).setSeqNum(seqNum).setUniqueId(uniqueId).setTtl(ttl).putAllProperties(props).build();
}
}
Aggregations