Show Table of Contents
6.10. SolverEventListener
Each time a new best solution is found, the
Solver fires a BestSolutionChangedEvent, in the solver's thread.
To listen to such events, add a
SolverEventListener to the Solver:
public interface Solver {
// ...
void addEventListener(SolverEventListener<? extends Solution> eventListener);
void removeEventListener(SolverEventListener<? extends Solution> eventListener);
}
The
BestSolutionChangedEvent's newBestSolution might not be initialized or feasible. Use the methods on BestSolutionChangedEvent to detect such cases:
solver.addEventListener(new SolverEventListener<CloudBalance>() {
public void bestSolutionChanged(BestSolutionChangedEvent<CloudBalance> event) {
// Ignore invalid solutions
if (event.isNewBestSolutionInitialized()
&& event.getNewBestSolution().getScore().isFeasible()) {
...
}
}
});Warning
The
bestSolutionChanged() method is called in the solver's thread, as part of Solver.solve(). So it should return quickly to avoid slowing down the solving.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.