I am writing a java library ( jar )
that contains numerous API's that will be called by an external software . One of the API will be a blocking API and will be performing an operation that can be quite lengthy .
I want a way to be able to provide regular feedback ( % completion ) of the operation to the client .
NOTE
Since its a library I cannot use the Java Swing mechanism of a progress bar to get and update a progress percent . Everything i search somehow leads to that .
Is there anyway to do this ?
Answer Update
Many thanks to all who posted answers . It helped me gain a grasp . I exposed the following interfaces on my jar . The client worker thread will register and listen for my progress updates . Then it will update the progress bar accordingly .
void LongAPI(){
while{
do work ;
fireprogress();
}
}
public void addProgressListener(Listener listener) {
listeners.add(listener);
}
public void removeProgressListener(Listener listener) {
listeners.add(listener);
}
private void fireProgress(double pourcent) {
for (Listener listener : listeners) {
listener.setProgress(pourcent);
}
}
The Listener
is an abstract interface that all clients much implement . It has the setProgress property using which the client can update its UI as it wishes .
I would give it a PropertyChangeSupport object as well as addPropertyChangeListener(...)
and removePropertyChangeListener(...)
methods. Internally, I'd create bound properties with setters that fire the support object. This way external libraries can be notified in a standard way that the property of interest has changed. If it is a Swing based library, consider using specifically a SwingPropertyChangeSupport object so that all PropertyChangeListeners that have been added to the support object are notified on the Swing event thread.
For example, please have a look at the answer to these questions: