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