Search in sources :

Example 1 with EventMeshMessage

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
    }
}
Also used : EventMeshGrpcClientConfig(org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig) HashMap(java.util.HashMap) Properties(java.util.Properties) EventMeshGrpcProducer(org.apache.eventmesh.client.grpc.producer.EventMeshGrpcProducer) EventMeshMessage(org.apache.eventmesh.common.EventMeshMessage)

Example 2 with EventMeshMessage

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
    }
}
Also used : EventMeshGrpcClientConfig(org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Properties(java.util.Properties) EventMeshGrpcProducer(org.apache.eventmesh.client.grpc.producer.EventMeshGrpcProducer) EventMeshMessage(org.apache.eventmesh.common.EventMeshMessage)

Example 3 with EventMeshMessage

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
    }
}
Also used : EventMeshGrpcClientConfig(org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig) HashMap(java.util.HashMap) Properties(java.util.Properties) EventMeshGrpcProducer(org.apache.eventmesh.client.grpc.producer.EventMeshGrpcProducer) EventMeshMessage(org.apache.eventmesh.common.EventMeshMessage)

Example 4 with EventMeshMessage

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;
    }
}
Also used : SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) EventMeshMessage(org.apache.eventmesh.common.EventMeshMessage)

Example 5 with EventMeshMessage

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();
    }
}
Also used : SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) CloudEvent(io.cloudevents.CloudEvent) EventMeshMessage(org.apache.eventmesh.common.EventMeshMessage)

Aggregations

EventMeshMessage (org.apache.eventmesh.common.EventMeshMessage)15 HashMap (java.util.HashMap)6 Properties (java.util.Properties)6 EventMeshHttpClientConfig (org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig)6 EventMeshHttpProducer (org.apache.eventmesh.client.http.producer.EventMeshHttpProducer)6 EventMeshGrpcClientConfig (org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig)4 EventMeshGrpcProducer (org.apache.eventmesh.client.grpc.producer.EventMeshGrpcProducer)4 CloudEvent (io.cloudevents.CloudEvent)3 SimpleMessage (org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)2 CloudEventBuilder (io.cloudevents.core.builder.CloudEventBuilder)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RequestParam (org.apache.eventmesh.client.http.model.RequestParam)1 EventMeshException (org.apache.eventmesh.common.exception.EventMeshException)1 BatchMessage (org.apache.eventmesh.common.protocol.grpc.protos.BatchMessage)1