I'm trying to run a task automatically (all the 30s).
For that, I built a singleton :
public class PortalSingleton {
private static final Logger LOG = LoggerFactory.getLogger(PortalSingleton.class);
private static final int INITIAL_DELAY = 0;
private static final int DELAY = 30;
private static volatile ScheduledExecutorService instance;
private static HomepageView homeView = new HomepageView();
private PortalSingleton() {}
public static final void refreshGridHomePageAutomatically() {
Runnable task = () -> UI.getCurrent().access(() -> {
homeView.refreshGrid();
LOG.info("The grid has been refreshed Automatically");
});
getInstance().scheduleWithFixedDelay(task, INITIAL_DELAY, DELAY, TimeUnit.SECONDS);
}
public final static ScheduledExecutorService getInstance() {
if (instance == null) {
synchronized (ScheduledExecutorService.class) {
if (instance == null) {
instance = Executors.newScheduledThreadPool(1);
}
}
}
return instance;
}
}
But, I didn't have any issue/error AND I didn't have my log msg and my grid hasn't been refreshed..
The behavior expected is :
Even if I delete the line homeView.refreshGrid();
, I don't have my log msg...
What did I do wrong?
Thanks,
EDIT : I call it by doing : PortalSingleton.refreshGridHomePageAutomatically();
EDIT2, thanks @Holger :
public class PortalSingleton {
private static final Logger LOG = LoggerFactory.getLogger(PortalSingleton.class);
private static final int INITIAL_DELAY = 0;
private static final int DELAY = 30;
private static final ScheduledExecutorService instance = Executors.newScheduledThreadPool(1);
private static HomepageView homeView = new HomepageView();
private PortalSingleton() {
}
public static final void refreshGridHomePageAutomatically() {
Runnable task = () -> UI.getCurrent().access(() -> {
homeView.refreshGrid();
LOG.info("The grid has been refreshed Automatically");
});
try {
getInstance().scheduleWithFixedDelay(task, INITIAL_DELAY, DELAY, TimeUnit.SECONDS);
} catch (Exception e) {
LOG.error("error" + e);
}
}
public final static ScheduledExecutorService getInstance() {
return instance;
}
}
When you schedule an action, you do not get a feedback when an exception occurs. Instead, it will just stop executing it:
ScheduledExecutorService.scheduleWithFixedDelay(…)
:
…If any execution of the task encounters an exception, subsequent executions are suppressed.
Therefore, you will have to use a try … catch
block in the action itself to report it, e.g. in the lambda expression defining your Runnable
:
Runnable task = () -> {
try { UI.getCurrent().access(…); }
catch (Exception e) { LOG.error("error" + e); }
};
It looks suspicious to me that you are calling UI.getCurrent()
from a non-UI thread which I suspect to return null
causing a NullPointerException
when trying to invoke a method on it.