Search in sources :

Example 1 with CloudEvent

use of io.cloudevents.CloudEvent in project incubator-eventmesh by apache.

the class SubController method subTest.

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String subTest(HttpServletRequest request) {
    String protocolType = request.getHeader(ProtocolKey.PROTOCOL_TYPE);
    String content = request.getParameter("content");
    log.info("=======receive message======= {}", content);
    Map<String, String> contentMap = JsonUtils.deserialize(content, HashMap.class);
    if (StringUtils.equals(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME, protocolType)) {
        String contentType = request.getHeader(ProtocolKey.CONTENT_TYPE);
        CloudEvent event = EventFormatProvider.getInstance().resolveFormat(contentType).deserialize(content.getBytes(StandardCharsets.UTF_8));
        String data = new String(event.getData().toBytes(), StandardCharsets.UTF_8);
        log.info("=======receive data======= {}", data);
    }
    subService.consumeMessage(content);
    Map<String, Object> map = new HashMap<>();
    map.put("retCode", 1);
    return JsonUtils.serialize(map);
}
Also used : HashMap(java.util.HashMap) CloudEvent(io.cloudevents.CloudEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with CloudEvent

use of io.cloudevents.CloudEvent 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 eventMeshHttpPort = properties.getProperty(ExampleConstants.EVENTMESH_HTTP_PORT);
    // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105
    String eventMeshIPPort = ExampleConstants.DEFAULT_EVENTMESH_IP_PORT;
    if (StringUtils.isNotBlank(eventMeshIp) || StringUtils.isNotBlank(eventMeshHttpPort)) {
        eventMeshIPPort = eventMeshIp + ":" + eventMeshHttpPort;
    }
    EventMeshHttpClientConfig eventMeshClientConfig = EventMeshHttpClientConfig.builder().liteEventMeshAddr(eventMeshIPPort).producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP).env("env").idc("idc").ip(IPUtils.getLocalAddress()).sys("1234").pid(String.valueOf(ThreadUtils.getPID())).userName("eventmesh").password("pass").build();
    try (EventMeshHttpProducer eventMeshHttpProducer = new EventMeshHttpProducer(eventMeshClientConfig)) {
        for (int i = 0; i < MESSAGE_SIZE; i++) {
            Map<String, String> content = new HashMap<>();
            content.put("content", "testAsyncMessage");
            CloudEvent event = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSubject(ExampleConstants.EVENTMESH_HTTP_ASYNC_TEST_TOPIC).withSource(URI.create("/")).withDataContentType(ExampleConstants.CLOUDEVENT_CONTENT_TYPE).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);
            log.info("publish event success content: {}", content);
        }
        Thread.sleep(30000);
    }
}
Also used : EventMeshHttpProducer(org.apache.eventmesh.client.http.producer.EventMeshHttpProducer) HashMap(java.util.HashMap) EventMeshHttpClientConfig(org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig) Properties(java.util.Properties) CloudEvent(io.cloudevents.CloudEvent)

Example 3 with CloudEvent

use of io.cloudevents.CloudEvent in project incubator-eventmesh by apache.

the class ProducerImplTest method testSend_WithException.

@Test
public void testSend_WithException() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    DefaultMQProducer defaultMQProducer = new DefaultMQProducer("testGroup");
    DefaultMQProducerImpl defaultMQProducerImpl = new DefaultMQProducerImpl(defaultMQProducer);
    defaultMQProducerImpl.setServiceState(ServiceState.RUNNING);
    Mockito.when(rocketmqProducer.getDefaultMQProducerImpl()).thenReturn(defaultMQProducerImpl);
    MQClientException exception = new MQClientException("Send message to RocketMQ broker failed.", new Exception());
    Mockito.when(rocketmqProducer.send(any(Message.class))).thenThrow(exception);
    try {
        CloudEvent cloudEvent = CloudEventBuilder.v1().withId("id1").withSource(URI.create("https://github.com/cloudevents/*****")).withType("producer.example").withSubject("HELLO_TOPIC").withData(new byte[] { 'a' }).build();
        producer.send(cloudEvent);
        failBecauseExceptionWasNotThrown(ConnectorRuntimeException.class);
    } catch (Exception e) {
        assertThat(e).hasMessageContaining("Send message to RocketMQ broker failed.");
    }
    Mockito.verify(rocketmqProducer).send(any(Message.class));
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException) ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) CloudEvent(io.cloudevents.CloudEvent) Test(org.junit.Test)

Example 4 with CloudEvent

use of io.cloudevents.CloudEvent in project incubator-eventmesh by apache.

the class ProducerImplTest method testSend_OK.

@Test
public void testSend_OK() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    SendResult sendResult = new SendResult();
    sendResult.setMsgId("TestMsgID");
    sendResult.setSendStatus(SendStatus.SEND_OK);
    MessageQueue messageQueue = new MessageQueue("HELLO_TOPIC", "testBroker", 0);
    sendResult.setMessageQueue(messageQueue);
    Mockito.when(rocketmqProducer.send(any(Message.class))).thenReturn(sendResult);
    DefaultMQProducer defaultMQProducer = new DefaultMQProducer("testGroup");
    DefaultMQProducerImpl defaultMQProducerImpl = new DefaultMQProducerImpl(defaultMQProducer);
    defaultMQProducerImpl.setServiceState(ServiceState.RUNNING);
    Mockito.when(rocketmqProducer.getDefaultMQProducerImpl()).thenReturn(defaultMQProducerImpl);
    CloudEvent cloudEvent = CloudEventBuilder.v1().withId("id1").withSource(URI.create("https://github.com/cloudevents/*****")).withType("producer.example").withSubject("HELLO_TOPIC").withData("hello world".getBytes()).build();
    org.apache.eventmesh.api.SendResult result = producer.send(cloudEvent);
    assertThat(result.getMessageId()).isEqualTo("TestMsgID");
    Mockito.verify(rocketmqProducer).getDefaultMQProducerImpl();
    Mockito.verify(rocketmqProducer).send(any(Message.class));
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) CloudEvent(io.cloudevents.CloudEvent) Test(org.junit.Test)

Example 5 with CloudEvent

use of io.cloudevents.CloudEvent in project incubator-eventmesh by apache.

the class StandaloneBroker method getMessage.

/**
 * Get the message, if the queue is empty return null
 *
 * @param topicName
 */
public CloudEvent getMessage(String topicName) {
    TopicMetadata topicMetadata = new TopicMetadata(topicName);
    MessageEntity head = messageContainer.computeIfAbsent(topicMetadata, k -> new MessageQueue()).getHead();
    if (head == null) {
        return null;
    }
    return head.getMessage();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Pair(org.apache.commons.lang3.tuple.Pair) CloudEvent(io.cloudevents.CloudEvent) MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity) HistoryMessageClearTask(org.apache.eventmesh.connector.standalone.broker.task.HistoryMessageClearTask) TopicMetadata(org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity) TopicMetadata(org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata)

Aggregations

CloudEvent (io.cloudevents.CloudEvent)123 Test (org.junit.jupiter.api.Test)35 IOException (java.io.IOException)16 TraceEvent (org.kie.kogito.tracing.event.trace.TraceEvent)16 CloudEventBuilder (io.cloudevents.core.builder.CloudEventBuilder)14 Test (org.junit.Test)13 EvaluateEvent (org.kie.kogito.tracing.decision.event.evaluate.EvaluateEvent)12 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)11 HashMap (java.util.HashMap)10 List (java.util.List)10 URI (java.net.URI)9 ArrayList (java.util.ArrayList)8 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)8 StaticConfigBean (org.kie.kogito.conf.StaticConfigBean)8 Collectors (java.util.stream.Collectors)7 Collections (java.util.Collections)6 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)6 Gson (com.google.gson.Gson)5 SendCallback (org.apache.eventmesh.api.SendCallback)5 SendResult (org.apache.eventmesh.api.SendResult)5