Hello, how do we integrate Kafka and create a Consumer that listens to a topic upon Cuba start up? is it supported by Cuba?
Hi,
CUBA has Spring Framework in its core, therefore, most of the Spring’s libraries are supported, Kafka is not an exception here.
All you need to do is to add the dependency to your project’s build.gradle:
configure([globalModule, coreModule, webModule]) {
apply(plugin: 'java')
apply(plugin: 'maven')
apply(plugin: 'cuba')
dependencies {
compile 'org.springframework.kafka:spring-kafka:2.5.3.RELEASE'
testCompile('org.junit.jupiter:junit-jupiter-api:5.5.2')
testCompile('org.junit.jupiter:junit-jupiter-engine:5.5.2')
testCompile('org.junit.vintage:junit-vintage-engine:5.5.2')
}
And then you will be able to interact with Kafka. You can use official Spring guide and create a configuration
class based on the example provided by Spring.
There is an example attached, I used Docker Kafka Set Up on my local workstation for it. Please pay attention to the following classes:
com.company.throttler.config.KafkaConfig
com.company.throttler.config.TestListener
com.company.throttler.service.HelloServiceBean
You can test message producing and and consuming by invoking the HelloService
using CUBA’s REST API:
GET http://localhost:8080/app-portal/hello/Boss
Headers:
content-type: text/plain
authorization: Basic YWRtaW46YWRtaW4=
And do not forget to assign rest-api-access to the admin user.
throttler.zip (98.3 KB)
Thanks a lot! I did try to setup Kafka same way as ordinary Spring but it did not work at first. I noticed I missed the @EnableKafka annotation. Thank you for the complete starter project