Issue
I'm using Spring Cloud Stream (Kafka as the binder) in my current project and the default thread name for StreamThread
keeps me bothered as it is very long.
<applicationId>-<GUID>-StreamThread-1
someEvent-578fb764-3fba-4a54-9a34-ea9796053530-StreamThread-1
With this long thread name, it is hard to see the actual log message.
Would like to know if there's a way to configure the thread name.
I already tried the one below but it is not working:
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> listenerCustomizer() {
return (container, destinationName, group) -> {
// Format to "[group].[destination]
container.setBeanName(String.format("%s.%s", group, destinationName));
};
}
Maybe I'm in the wrong config?
Side note: I enjoy using Spring Cloud Stream, and it is quite popular from what I see. But it keeps me wondering why I can't get any good documentation.
Edit:
Solved the issue by adding spring.kafka.client-id
in application.properties
.
Having the client-id
present, KafkaStreams
will use the user-defined client-id
as the thread name.
Solution
Are you sure that you use plain Kafka binder, but not Kafka Streams binder? That StreamThread
gets its name like final String threadId = clientId + "-StreamThread-" + threadIdx;
. See StreamThread
source code. So, you need to be sure what is that clientId
propagated down to Kafka Streams.
See StreamsConfig.CLIENT_ID_CONFIG
which you can configure via spring.cloud.stream.kafka.streams.binder.applicationId
property.
UPDATE
OK. I found the logic in the KafkaStreams
:
// The application ID is a required config and hence should always have value
final String userClientId = config.getString(StreamsConfig.CLIENT_ID_CONFIG);
final String applicationId = config.getString(StreamsConfig.APPLICATION_ID_CONFIG);
if (userClientId.length() <= 0) {
clientId = applicationId + "-" + processId;
} else {
clientId = userClientId;
}
...
final String baseName = clientId + "-StreamThread-";
So, the final thread name is based on that clientId
exactly.
If that StreamsConfig.CLIENT_ID_CONFIG
is empty, then it is based on the applicationId
plus that processId
. which might be indeed a GUID you mention.
Therefore consider to set that StreamsConfig.CLIENT_ID_CONFIG
instead of an applicationId
.
Answered By - Artem Bilan
Answer Checked By - Marie Seifert (JavaFixing Admin)