Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
robinmatz opened this issue Oct 18, 2023 · 2 comments
Open

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

robinmatz opened this issue Oct 18, 2023 · 2 comments
Assignees

Comments

@robinmatz
Copy link

robinmatz commented Oct 18, 2023

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?

@github-actions
Copy link

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

@jemacineiras
Copy link
Contributor

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.

@jemacineiras jemacineiras self-assigned this Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants