Consumer state listener usage #927
-
|
Hello, #836 added listeners to track the state of consumers or producers, which is a very welcome feature for our use case. Our consumers must know whether they do not receive messages because producers don't send them, or because of a connection/broker problem. We can implement some kind of lifesign on the producer/consumer level, but a state listener is obviously the preferred solution. My question is how exactly can I determine when the consumer is disconnected (that is, is not able to receive messages) and when is it healthy again. The javadoc says that "A consumer uses one connection for consuming and another connection for server-side offset tracking", and indeed, they come in pairs for my test consumers: My Resource.StateListener is called once with a Resource.Context for each of the two resources in case of my test Consumer (as the Javadoc says). Now, conservatively, my application could decide that if at least one of the resources is not OPEN, then there is likely a connection issue. But there seem to be no way to programmatically find out how many resources are there, so after my listener is called with an OPEN state, I don't know how many more resources are there. If there is one, I can report to the application that the connection is up. If there is more, I'd have to wait until all resources are reported as OPEN, but again, I don't know how many more to expect. I can track the resources in my consumer, for instance maintaining a map with the Resource as the key to detect all edge cases, but not knowing how many resources are there makes this fragile and possibly memory leaks as it would rely on the stream library to maintain Resources with the same hash during the lifetime of the consumer. Is there something I'm missing? What is the best practice to detect consumer state changes? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
|
The consumer or the producer is the resource. So if there is a connection issue, you should see the state changing to "recovering" then going back to "open". A named consumer that uses server-side offset tracking uses an extra connection, so you'll see also events when this connection goes down and reconnects. |
Beta Was this translation helpful? Give feedback.
Again, the
ConsumerorProducerinstance is the resource fromContext#resource(). If you set up aStateListenerinstance on e.g. aConsumer, it should always see the same consumer instance coming fromContext#resource(). If you don't observe this, there is a bug somewhere. In this case, provide a standalone code sample that reproduces this behavior.Of course if you have a shared
StateListenerinstance between consumers and producers (that is you create aStateListenerinstance and use it when creating your consumers and producers), it will see different resources in its callback, that is perfectly normal.According to the comment in your code, the current behavior should do what you expe…