Clean up AMQP/KAFKA Cloud Event sample README

This commit is contained in:
Oleg Zhurakousky
2020-12-18 18:54:21 +01:00
parent 0d0ef3350c
commit dacca71cc2
4 changed files with 20 additions and 33 deletions

View File

@@ -6,28 +6,14 @@ their requirement while taking care-off non-functional aspects. For more informa
our https://spring.io/projects/spring-cloud-function[project page].
The example consists of a Spring boot configuration class
https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-samples/function-sample-cloudevent/src/main/java/io/spring/cloudevent/DemoApplication.java[DemoApplication]
https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-samples/function-sample-cloudevent-stream/src/main/java/io/spring/cloudevent/DemoApplication.java[DemoApplication]
which contains a sample function which you can interact with following via AMQP and Apache Kafka.
### Function as message handler (From RabbitMQ to Apache Kafka)
### From RabbitMQ to Apache Kafka
Assuming you have RabbitMQ and Kafka running, start the application and send a Message to RabbitMQ. You can use RabbitMQ dashboard (if you have it installed)
and send message to `hire-in-0` exchange.
To stay compliant with Cloud Event specification you should provide attributes with AMQP appropriate prefixes (i.e., cloudEvents:). For example:
Assuming you have RabbitMQ and Kafka running, start the application and send a Message to RabbitMQ.
```
cloudEvents:specversion=1.0
cloudEvents:type=hire
cloudEvents:source:spring.io/spring-event
cloudEvents:id=0001
```
And your data:
```
{"firstName":"John", "lastName":"Doe"}
```
To simplify this demo part we included a test case which effectively automates this demo by sending Cloud Event to RabbitMQ and receives one from Apache Kafka.
We included a https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-samples/function-sample-cloudevent-stream/src/test/java/io/spring/cloudevent/DemoApplicationTests.java[demo test case] which effectively automates this demo by sending Cloud Event to RabbitMQ and receives one from Apache Kafka.
```
Message<byte[]> messageToAMQP = CloudEventMessageBuilder
@@ -50,4 +36,19 @@ since it was determined by the framework that the target destination is Apache K
This last point is worth elaborating a bit. We already established that setting Cloud Event attributes is a non-functional aspect and because
of it weve exposed a mechanism for you to deal with it outside of your business logic. But what about attribute prefixes? Note that we are running the
same code in different execution contexts. This means that the attribute prefixes actually depend on the execution context. So by being aware of the execution
context, the framework ensures the correctness of the Cloud Event attribute prefixes.
context, the framework ensures the correctness of the Cloud Event attribute prefixes.
You can also use http://localhost:15672/[RabbitMQ dashboard] (if you have it installed) and send message to `hire-in-0` exchange.
To stay compliant with Cloud Event specification you should provide attributes with AMQP appropriate prefixes (i.e., `cloudEvents:`). For example:
```
cloudEvents:specversion=1.0
cloudEvents:type=hire
cloudEvents:source:spring.io/spring-event
cloudEvents:id=0001
```
And your data:
```
{"firstName":"John", "lastName":"Doe"}
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 181 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 180 KiB

View File

@@ -1,12 +1,9 @@
package io.spring.cloudevent;
import java.util.Collections;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.KafkaAdminClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
@@ -15,23 +12,13 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessagingMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.function.cloudevent.CloudEventMessageBuilder;
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
import org.springframework.cloud.function.context.config.JsonMessageConverter;
import org.springframework.integration.kafka.inbound.KafkaMessageSource;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeTypeUtils;
@SpringBootTest
@@ -45,7 +32,6 @@ public class DemoApplicationTests {
@Test
public void test() throws Exception {
Message<byte[]> messageToAMQP = CloudEventMessageBuilder
.withData("{\"firstName\":\"John\", \"lastName\":\"Doe\"}".getBytes())
.setSource("https://cloudevent.demo")