I need to log when a client is disconnected from Apache Camel connection. How can I detect a netty-tcp disconnection?
from("{{uri.client.address}}")
I need to log when a client is disconnected from Apache Camel connection. How can I detect a netty-tcp disconnection?
from("{{uri.client.address}}")
Its a bit more complicated to log individual client disconnects but I will try to share what I had to do.
You need to extend SimpleChannelInboundHandler with your own class and add the methods needed. For an example class see here:
https://github.com/apache/camel/blob/master/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/handlers/ServerChannelHandler.java
The main method there to look at which I have changed to include client disconnect is this one:
Note the part where I log if the channel has connected.
You can also log the ip address of different client if you want to do different things for different clients in your main route. This should help you.
Edit: Once you have extended that class, call the extended class TCPServerHandler. Then you need to create a TCPServerHandlerFactory to refer to previous class and set your encoder/decoder. This class can look like this:
The line marked with ** is where you are creating the TCPServerHandler class.
Then back in camel in your route class you need to add the factory to your registry like this:
Then refer to it as such:
Now you may wondering, all of this to register a log for a disconnect. This is because to be able to distinguish when a client disconnects and actually get the ip of the client you need to the Netty code. You can save some value in the exchange and then later in your route do something.
Use an ErrorHandler, e.g. the LoggingErrorHandler: