flush a failed JMX connection

I connect to a server using JMX connection periodically each 10 sec.
using JMXConnectorFactory.connect
when I unplug the network cable and plug it again ket's say after 20 seconds, seems like there exist more than one connection is already alive.
I want to flush memory if I tried a failed connection so when I plug again the cable I only have one connection not several connections.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do you shut down the client cleanly?
Mick BarryJava DeveloperCommented:
make sure you are catching all exceptions (and not ignoring any) and handling them correctly.
bachra04Author Commented:
Actually the problem I have is this:

I have two threads:

The timer thread and the daemon thread.

The daemon thread has a queue of events and periodically checks this queue to pop events and handle them.

When the daemon receives a TIME OUT event, it makes a JMX connection to get some data.

The problem happens when there is a network problem, at this time since the daemon spends a lot of time trying to make a JMX connection; meanwhile the timer just put another TIME OUT event in the queue and by the time the connection is restored the daemon ends up with 4 or 5 TIME OUT events in his queue and processes them one after the other.

This threading model is not done by me and I shouldnt change at least right now.
I need a good solution to avoid this accumulation of TIME OUT events in the queue.
You have to know that queue is also holding other kinds of events (config change event&) so that the daemon can handle them.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Mick BarryJava DeveloperCommented:
you need to limit your queu to only hold one tiomeout at a time, for example by a check when timeouts are added.
What causes the TIME OUT?
Mick BarryJava DeveloperCommented:
or maybe stop the timer while trying to make a connection
bachra04Author Commented:
I need to use wait and signal in the same way used in C++.
just to let the timer waiting before sending the event till he gets notified by the daemon.
Do you know how to do it in java?
Just make your daemon own a BlockingQueue. When it's full, the timer thread, offering, will not be able to enqueue


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.