I've never done this myself -- very few people ever need it -- but I think this is one way to do it.
abstract class ConnectionReference extends PhantomReference<Connection> {
abstract void cleanUp();
}
...
ReferenceQueue<Connection> connectionQueue = new ReferenceQueue<>();
...
Connection newConnection = ...
ConnectionReference ref = new ConnectionReference(newConnection, connectionQueue, ...);
...
// draining the queue in some thread somewhere...
Reference<? extends Connection> reference = connectionQueue.poll();
if (reference != null) {
((ConnectionReference) reference).cleanUp();
}
...
This is more or less similar to what this post suggests.