Skip to content

The settings of the properties field have the same problem, and SpEL expressions cannot solve it #1880

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

Closed
linchutao opened this issue Jul 22, 2021 · 9 comments · Fixed by #1890

Comments

@linchutao
Copy link

Affects Version(s): <Spring for Apache Kafka version>

@linchutao linchutao changed the title The settings of the properties field have the same problem, and SpEL expressions cannot solve it The settings of the properties field have the same problem, and SpEL expressions cannot solve it #361 Jul 22, 2021
@linchutao linchutao changed the title The settings of the properties field have the same problem, and SpEL expressions cannot solve it #361 The settings of the properties field have the same problem, and SpEL expressions cannot solve it Jul 22, 2021
@linchutao
Copy link
Author

#361

@artembilan
Copy link
Member

I'm sorry what is the problem? Can you be, please, more specific?
Otherwise this looks like Invalid and will be closed respectively.

Thank you for understanding!

@linchutao
Copy link
Author

linchutao commented Jul 22, 2021

Sorry, let me show you a demo

@KafkaListener(groupId = "my-group", topics = "#{'${consumer-config.topics}'.split(',')}", properties = "#{'${consumer-config.properties}'.split(',')}")

with

application.yml

consumer-config:  
  topics: topic1,topic2  
  properties: max.poll.records:100,group.instance.id:instance1

properties cannot be processed normally like topics,I noticed that the processing of properties in the source code is not the same as topics

@garyrussell
Copy link
Contributor

Unlike the other properties that support SpEL, there is no mention of SpEL in the javadocs for properties.

	/**
	 * Kafka consumer properties; they will supersede any properties with the same name
	 * defined in the consumer factory (if the consumer factory supports property overrides).
	 * <p>
	 * <b>Supported Syntax</b>
	 * <p>The supported syntax for key-value pairs is the same as the
	 * syntax defined for entries in a Java
	 * {@linkplain java.util.Properties#load(java.io.Reader) properties file}:
	 * <ul>
	 * <li>{@code key=value}</li>
	 * <li>{@code key:value}</li>
	 * <li>{@code key value}</li>
	 * </ul>
	 * {@code group.id} and {@code client.id} are ignored.
	 * @return the properties.
	 * @since 2.2.4
	 * @see org.apache.kafka.clients.consumer.ConsumerConfig
	 * @see #groupId()
	 * @see #clientIdPrefix()
	 */
	String[] properties() default {};

Vs

	/**
	 * The topics for this listener.
	 * The entries can be 'topic name', 'property-placeholder keys' or 'expressions'.
	 * An expression must be resolved to the topic name.
	 * This uses group management and Kafka will assign partitions to group members.
	 * <p>
	 * Mutually exclusive with {@link #topicPattern()} and {@link #topicPartitions()}.
	 * @return the topic names or expressions (SpEL) to listen to.
	 */
	String[] topics() default {};

This would be a new feature.

@linchutao
Copy link
Author

linchutao commented Jul 23, 2021

Thanks @garyrussell , This is how I deal with it now

@KafkaListener(groupId = "my-group",
        topics = "#{'${consumer-config.topics}'.split(',')}",
        properties = {
        "max.poll.records:" + "${consumer-config.properties.max.poll.records}", 
            "group.instance.id:" + "${consumer-config.properties.group.instance.id}"
    })

with

consumer-config:
  topics: topic1,topic2
  properties:
    max.poll.records: 100
    group.instance.id: instance1

It works, but the configuration looks a little cumbersome

@garyrussell
Copy link
Contributor

Understood; this is a reasonable new feature request.

@garyrussell
Copy link
Contributor

garyrussell commented Jul 27, 2021

@linchutao I have implemented the change, but FYI, this works with current versions:

application.properties:

some.prop=max.poll.records: 10\nfetch.min.bytes: 123456

or

some.prop=max.poll.records: 10\n \
 fetch.min.bytes: 123456
	@KafkaListener(id = "kgh1880", topics = "kgh1880", autoStartup = "false",
			properties = "${some.prop}")

i.e. insert a newline between properties

@garyrussell
Copy link
Contributor

garyrussell commented Jul 27, 2021

application.yml

some:
  prop: |
    max.poll.records: 10
    fetch.min.bytes: 123456

garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Jul 27, 2021
Resolves spring-projects#1880

SpEL can resolve to `String`, `String[]` or `Collection<String>`.
artembilan pushed a commit that referenced this issue Jul 27, 2021
Resolves #1880

SpEL can resolve to `String`, `String[]` or `Collection<String>`.
@artembilan artembilan modified the milestones: Backlog, 2.8.0-M2 Jul 27, 2021
garyrussell added a commit that referenced this issue Jul 28, 2021
@linchutao
Copy link
Author

Thanks @garyrussell , It looks much more comfortable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants