1

update window data with ScheduledExecutorService

asked 2014-03-03 11:59:00 +0800

Neus gravatar image Neus
1415 14

updated 2014-03-03 16:04:38 +0800

Hi, I'm using ScheduledExecutorService to update some data in db every X seconds. Everytime data is updated I need to refresh a chart in a window if the user is consulting it. My problem is that I can't call any Event inside the task because it says:

Exception in thread "pool-1-thread-5 actualizacion-aforos" java.lang.IllegalStateException: Not in an execution
at org.zkoss.zk.ui.event.impl.EventQueueProviderImpl.lookup(EventQueueProviderImpl.java:43)
at org.zkoss.zk.ui.event.EventQueues.lookup(EventQueues.java:133)
at org.sts.Aforos.Herramientas.ExtendedScheduledExecutor.afterExecute(ExtendedScheduledExecutor.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

So... how can I do it???

Thank you!

EDIT: This is how I schedule tasks:

scheduledExecService = new ExtendedScheduledExecutor(numThreads, myThreadFactory);
TareaActualizacion act = new TareaActualizacion(inst);
ScheduledFuture<?> handle = scheduledExecService.scheduleWithFixedDelay(act, retrasoInicial, segundosRefresco, TimeUnit.SECONDS);

This code is called in WebAppInit.

act is a Runnable class that recive some data by parameter:

public class TareaActualizacion implements Runnable {
private Instalacion instalacion;
public TareaActualizacion(Instalacion instalacion) {
    this.instalacion = instalacion;
}
@Override
public void run() {
    //Do something
}
public Instalacion getInstalacion() {
    return instalacion;
}

}

I want to refresh data in screen only if the user is consulting the "Instalacion" that is being updated by the task. So in the run() method of the task after update de db I tried to post an event using both

BindUtils.postGlobalCommand(null, null, "updateDatosPantalla", args);

or

EventQueues.lookup("updateDatosPantalla",EventQueues.APPLICATION,true).publish(new     Event("onActualizarDatosPantalla",null,instalacion.getId()));

but the expection I mentioned above is thrown.

After this I try to do the same from the method afterExecute() of the ScheduledThreadPoolExecutor class but the same excpetion was thrown.

I understand that the thread I'm using to execute the task is not in a execution and not always would be a window executed when the db is updated. But I need to, if there's a window consulting the data updated, to refresh the window data.

delete flag offensive retag edit

Comments

Is it an other(like server)project where you call the ScheduledExecutorService?

chillworld ( 2014-03-03 12:53:44 +0800 )edit

I edited my post to explain a little bit more my problem. It is very complicated for me to explin it. I hope you can understand it

Neus ( 2014-03-03 16:05:27 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-03-03 19:33:34 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9
https://github.com/chillw...

updated 2014-03-04 08:43:27 +0800

I think this can be of help :

http://forum.zkoss.org/question/70806/building-zk-front-end-to-handle-multiple-simultaneous-long-running-data-queries/

Another possibility is when a session is created, check if that process is already running, if not create one. Like this you will unload the server when no users are there.

Don't forget to subscribe(and lookup) to the APPLICATION EventQueue, otherwise you won't reach all your sessions that are alive.

edit : You need to register this class as a listener in zk.xml!

public class AnalyticsThreadPool implements WebAppInit, WebAppCleanup {
    private static ExtendedScheduledExecutor EXECUTOR_SERVICE;

    public void init(WebApp webApp) {
      if (EXECUTOR_SERVICE == null) {
        //do all the instanciate your threadpool.
      }
      //create the eventqueue.
      EventQueues.lookup("threadQueue", webApp, true);
      //create your task, if you can't reach the eventqueue in that class just pass it along then from here.
      Runnable task = SomeClass.createTask(); 
      //add task in the executors.

    }
    public void cleanup(WebApp webApp) {
      EXECUTOR_SERVICE.shutdownNow();
      EXECUTOR_SERVICE = null;
      EventQueues.remove("threadQueue", webApp, true);
    }

    public static void submit(Runnable task) {
      EXECUTOR_SERVICE.submit(task);
    }
}

In the creation of your thread you will need to call the EventQueue like this :

EventQueues.lookup("threadQueue", EventQueues.APPLICATION, true);

Hope this is more what you can use.

Greetz chill.

link publish delete flag offensive edit

Comments

I have to update db even if there are no users logged in. The data is used for representing a chart and I need to save all the data no matetr if there are no users. I doesn't know how to solve it yet because in the post you show me the task is called from an Execution, isn't it?

Neus ( 2014-03-04 08:04:56 +0800 )edit

Thank you! I finally manage to get it work thank to your clues!

Neus ( 2014-03-04 09:31:57 +0800 )edit

Your welcome ;)

chillworld ( 2014-03-04 10:08:42 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-03-03 11:59:00 +0800

Seen: 30 times

Last updated: Mar 04 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More