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);
}
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);
}
}
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));
}
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));
}
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();
}
Aggregations