oraedoa
oraedoa

Reputation: 95

How to gracefully shutdown spring-kafka consumer application

i have implemented spring-kafka consumer application.

i wants consumer application graceful shutdown.

the current consumer application is terminated with the Linux command kill -9 pid

i am using @KafkaListener annotation now.

if i quit the Spring boot app, i want to reliably close the consumer, what should i do ?


i've been using @Predestory to reliably exit the spring boot app, but i'm not quite sure if this has anything to do with it.

Upvotes: 6

Views: 12703

Answers (4)

Dmitry Krychylskyy
Dmitry Krychylskyy

Reputation: 197

Spring provides a sub-project Actuator, which allows you to receive information about the status of a component as well as control it.

In order to install it, simply add two dependencies to pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

Also in the properties file you must assign the port number :

server.port= your free port like 8081

Also, in order for the actuator to switch the component off correctly, another parameter must be set in the properties file :

server.shutdown=graceful

To turn a component off using an actuator, use the command :

curl 'http://localhost:8080/actuator/shutdown' -i -X POST

All Actuator's commands

Upvotes: 0

msangel
msangel

Reputation: 10377

Ok, I will put here my thoughts on this. Either kill will cause thread interruption. It's ok, when the message consumer is single-threaded. But in case if message processing using threads inside (or executors, anything that works in different threads), those will catch the interruption too. And no way to prevent it.

In the case of batch mode, it is even hard to track propper offset, because some messages have been processed, and some - no. So there is another approach - tell the consumer to stop listening but finish processing or those items that are polled in this step. So we should not use any of the interruptions, but the stopping should be a programmatic command to the application.

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174799

kill -9 is like the death star

     Some of the more commonly used signals:

     1       HUP (hang up)
     2       INT (interrupt)
     3       QUIT (quit)
     6       ABRT (abort)
     9       KILL (non-catchable, non-ignorable kill)
     14      ALRM (alarm clock)
     15      TERM (software termination signal)

The default kill signal SIGTERM (15)

kill <pid>

Boot will shut down everything gracefully; it registers a shutdown hook, which can't intercept a kill -9.

Upvotes: 6

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39950

If you want to stop a single consumer, just call stop() on the listener container.


Note that when you call stop() the container will process all records that have been fetched from poll() until that point, before the container shuts down.

Upvotes: 4

Related Questions