Why not initializing a TcpSyslogMessageSender before every periodic sendMessage()?

173 Views Asked by At

I am using CloudBees Syslog Java Client in a simple application to send Syslog messages periodically to an server. All works fine but I wondered - if the TcpSyslogMessageSender-Class is initialized on every loop, it will stop sending new messages after 10 iterations without any exception. I can easily change this and move the object initialization to the constructor of the calling class, but I want to understand why this is. From my point of view I am initializing a clean new object on every iteration. The Garbage Collection should remove the older objects and free the used network resources. But maybe it is not that easy. :)

while(true){
            TcpSyslogMessageSender messageSender = new TcpSyslogMessageSender();
            messageSender.setDefaultMessageHostname(...);
            ...
            messageSender.sendMessage(msg);
}

Would like to learn about it!

Cheers,

cmax

1

There are 1 best solutions below

0
On BEST ANSWER

I am answering to myself: I learned that TcpSyslogMessageSender has a close-method which actually closes the used socket. It seems that it was not possible to open more that 10 sockets from one Java-instance in parallel. Unfortunately, the close-call was not part of the given code example. Now, it is absolutely no problem to initialize and reinitialize the TcpSyslogMessageSender for many times. Hope someone will find this helpful.