sngular/scs-multiapi-plugin

Question: How to best integrate this plugin with spring-kafka?

robinmatz opened this issue · 2 comments

Hello there,

I am relatively new to using message broker systems. I am testing out this plugin to generate code from a simple example asyncapi specification
and integrate it with spring-kafka.

asyncapi: 2.3.0
info:
  title: Order Service
  version: 1.0.0
  description: Order management Service
servers:
  local:
    url: localhost
    protocol: kafka
    protocolVersion: 0.9.1
channels:
  helloworld.publish:
    publish:
      operationId: "publishHelloWorld"
      message:
        $ref: '#/components/messages/HelloWorld'
  helloworld.subscribe:
    subscribe:
      operationId: "subscribeHelloWorld"
      message:
        $ref: '#/components/messages/HelloWorld'
components:
  messages:
    HelloWorld:
      payload:
        $ref: '#/components/schemas/HelloWorldMessage'
  schemas:
    HelloWorldMessage:
      type: object
      properties:
        message:
          type: string
          minLength: 3
          maxLength: 100
          example: Hello, world!

I have the build plugin configured like this

 <plugin>
                <groupId>com.sngular</groupId>
                <artifactId>scs-multiapi-maven-plugin</artifactId>
                <version>${scs-multiapi-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>asyncapi-generation</goal>
                        </goals>
                        <configuration>
                            <specFiles>
                                <specFile>
                                    <filePath>src/main/resources/asyncapi.yml</filePath>
                                    <consumer>
                                        <modelNameSuffix>Dto</modelNameSuffix>
                                        <apiPackage>com.example.asyncapi.model.event.consumer
                                        </apiPackage>
                                        <modelPackage>com.example.asyncapi.model.event</modelPackage>
                                    </consumer>
                                    <supplier>
                                        <apiPackage>com.example.asycnapi.model.event.producer
                                        </apiPackage>
                                        <modelPackage>com.example.asyncapi.model.event</modelPackage>
                                    </supplier>
                                </specFile>
                            </specFiles>
                            <springBootVersion>3</springBootVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

And I wanted to ask where I should actually publish messages with spring-kafka

Would I do this in my custom IPublishOperation implementation like so?

package com.example.example.asyncapi.model.event.producer;

import com.example.asycnapi.model.event.HelloWorldMessage;
import com.example.asycnapi.model.event.producer.IPublishHelloWorld;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class PublishHelloWorld implements IPublishHelloWorld {

    private final KafkaTemplate<String, HelloWorldMessage> kafkaTemplate;

    @Override
    @EventListener(ApplicationStartedEvent.class)
    public HelloWorldMessage publishHelloWorld() {
        System.out.println("Am publishing");
        HelloWorldMessage message = HelloWorldMessage.builder()
                .message("Hello, world")
                .build();
        kafkaTemplate.send("mytopic", message);
        return message;
    }
}

or is this the wrong approach?

Thank you for collaborating with the project by giving us feedback! Cheers!

Hi @robinmatz,

the code we actually generate is mainly focus to work with Spring Cloud Stream. Your proposal mean generate code to support Spring Kafka, which will require to generate new code.
If that is your requirement we will added to our task list, unless you want to collaborate with the project.

Cheers.