From 5c1062fd7d2b03439e02d23139721e1d89278f74 Mon Sep 17 00:00:00 2001 From: Semen Date: Sun, 26 Apr 2020 20:24:41 +0300 Subject: [PATCH] feat: generalize Kafka properties (#16) * feat: generalize kafka properties. use spring-kafka props inst.of custom, move props to template params * fix wrong param name in example * fix: generation for mqtt and amqp (#14) * fix api calls for mqtt and amqp * fix port variables * feat: add annotations to the model generation (#12) * WIP, update to spec 2.0 * WIP, update to spec 2.0, remove definition of topic via extension * working version for api 2.0 * add model generation * Add support of Kafka generation * update readme * fix value ref, add jackson lib * add validation annotations to schema * fix no type case * add annotation for jackson * add description and examples to javadoc * Update readme, move missed features block * switch publish to subscribe and vice versa, because of wrong understanding of spec * Fix consumed key, add key to publish operation * move jackson and validation dependencies from kafka block * fix README formatting * mark annotations as done in README * add support of multi-line description * fix json in examples * chore(release): 0.5.0 (#15) chore(release): 0.5.0 * fix api calls for mqtt and amqp * fix port variables * fix yet another api operation call Co-authored-by: asyncapi-bot <61865014+asyncapi-bot@users.noreply.github.com> * chore(release): 0.5.1 (#19) chore(release): 0.5.1 * feat: generalize kafka properties. use spring-kafka props inst.of custom, move props to template params * fix wrong param name in example Co-authored-by: asyncapi-bot <61865014+asyncapi-bot@users.noreply.github.com> --- .tp-config.json | 12 +++++++++++- README.md | 19 ++++++++++++++++--- partials/KafkaConfig.java | 15 --------------- template/src/main/resources/application.yml | 11 ++++++----- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/.tp-config.json b/.tp-config.json index 59fde25cc..2e651341a 100644 --- a/.tp-config.json +++ b/.tp-config.json @@ -2,5 +2,15 @@ "supportedProtocols": ["kafka", "amqp", "mqtt"], "nonRenderableFiles": [ "**/*.jar" - ] + ], + "parameters": { + "listenerPollTimeout": { + "description": "Only for Kafka. Timeout to use when polling the consumer.", + "required": false + }, + "listenerConcurrency": { + "description": "Only for Kafka. Number of threads to run in the listener containers.", + "required": false + } + } } diff --git a/README.md b/README.md index c66470bdc..44a059896 100644 --- a/README.md +++ b/README.md @@ -84,10 +84,18 @@ components: Options: -V, --version output the version number - -t, --templates directory where templates are located (defaults to internal templates directory) + -o, --output directory where to put the generated files (defaults to current directory) + -p, --param additional param to pass to templates -h, --help output usage information ``` +#### Supported parameters + +|Name|Description|Required|Default| +|---|---|---|---| +|listenerPollTimeout|Only for Kafka. Timeout in ms to use when polling the consumer.|No|`3000`| +|listenerConcurrency|Only for Kafka. Number of threads to run in the listener containers.|No|`3`| + #### Examples The shortest possible syntax: @@ -95,11 +103,16 @@ The shortest possible syntax: ag asyncapi.yaml @asyncapi/java-spring-template ``` -Specify where to put the result: +Specify where to put the result and define poll timeout: ```bash -ag -o ./src asyncapi.yaml @asyncapi/java-spring-template +ag -o ./src asyncapi.yaml -p listenerPollTimeout=5000 @asyncapi/java-spring-template ``` +If you don't have the AsyncAPI Generator installed, you can install it like this: + +``` +npm install -g @asyncapi/generator +``` ### Run it Go to the root folder of the generated code and run this command (you need the JDK1.8): diff --git a/partials/KafkaConfig.java b/partials/KafkaConfig.java index c7122dadb..e7fcfc902 100644 --- a/partials/KafkaConfig.java +++ b/partials/KafkaConfig.java @@ -32,17 +32,6 @@ @Configuration {% if hasPublish %}@EnableKafka{% endif %} public class Config { -{%- if hasSubscribe or hasPublish %} - @Value("${kafka.bootstrap-servers:localhost:9092}") - private String bootstrapServers; -{% endif %} -{%- if hasPublish %} - @Value("${kafka.subscribe.pool-timeout:3000}") - private long poolTimeout; - - @Value("${kafka.subscribe.amount-of-listeners:3}") - private Integer amountOfListeners; -{% endif %} {%- if hasSubscribe %} @Bean public KafkaTemplate kafkaTemplate() { @@ -57,7 +46,6 @@ public ProducerFactory producerFactory() { @Bean public Map producerConfigs() { Map props = new HashMap<>(); - props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); props.put(JsonSerializer.TYPE_MAPPINGS, @@ -78,8 +66,6 @@ public Map producerConfigs() { ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>(); factory.setConsumerFactory(consumerFactory()); - factory.setConcurrency(amountOfListeners); - factory.getContainerProperties().setPollTimeout(poolTimeout); return factory; } @@ -91,7 +77,6 @@ public ConsumerFactory consumerFactory() { @Bean public Map consumerConfigs() { Map props = new HashMap<>(); - props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class); props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class); props.put(JsonSerializer.TYPE_MAPPINGS, diff --git a/template/src/main/resources/application.yml b/template/src/main/resources/application.yml index 4b765c839..6f62594b0 100644 --- a/template/src/main/resources/application.yml +++ b/template/src/main/resources/application.yml @@ -41,11 +41,12 @@ mqtt: {% endfor %} {% endif %}{% endfor %} {%- if asyncapi | isProtocol('kafka') %} -kafka: - bootstrap-servers: {% for serverName, server in asyncapi.servers() %}{% if server.variable('port') %}{{server.url() | replace('{port}', server.variable('port').defaultValue())}}{% else %}{{server.url()}}{% endif %}{% if not loop.last %},{% endif %}{% endfor %} +spring: + kafka: + bootstrap-servers: {% for serverName, server in asyncapi.servers() %}{% if server.variable('port') %}{{server.url() | replace('{port}', server.variable('port').defaultValue())}}{% else %}{{server.url()}}{% endif %}{% if not loop.last %},{% endif %}{% endfor %} {%- if hasPublish %} - subscribe: - pool-timeout: 3000 - amount-of-listeners: 3 + listener: + poll-timeout: {% if params.listenerPollTimeout %}{{params.listenerPollTimeout}}{% else %}3000{% endif%} + concurrency: {% if params.listenerConcurrency %}{{params.listenerConcurrency}}{% else %}3{% endif%} {% endif %} {% endif %} \ No newline at end of file