0

SOLVED: my Achilles' heel: ExecutorService

asked 2017-04-19 13:43:32 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

updated 2017-04-27 16:36:26 +0800

This is my Achilles' heel... I'd like to read cyclically some data from db and warn the user on specified presence of event. I tried writing some solution...without success. Now the error is : "org.zkoss.zul.Timer cannot be cast to org.zkoss.zk.ui.event.EventListener"

Can someone help me? thanks, /Luca

zk.xml

<listener>
     <description>Utilizzato leggere dal database </description>
     <display-name>Ricontatto cellulare </display-name>
     <listener-class>rvl.gm.utility.RicontattoCellulare</listener-class>
</listener>

RicontattoCellulare

public class RicontattoCellulare implements WebAppInit, WebAppCleanup
{
    private static volatile ExecutorService executor;    
    public static ExecutorService getExecutor() 
    {
        return executor;
    }
    @Override
    public void cleanup(WebApp wapp) throws Exception 
    {
        if (executor != null) 
        {
            executor.shutdown();
            System.out.println("ExecutorService gestione telefonate a cellulare ->shutdown");
        }
    }
    @Override
    public void init(WebApp wapp) throws Exception 
    {
        executor = Executors.newSingleThreadExecutor();
        System.out.println("ExecutorService gestione telefonate a cellulare ->startup");
    }

}

RicontattoCellulareRun

public class RicontattoCellulareRun implements Runnable
{

    private final Desktop desktop;
    private final EventListener<Event> eventListener;
    private final Nazioni gNazione;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public RicontattoCellulareRun(Desktop desktop, EventListener eventListener, Nazioni gNazione)
    {
        this.desktop = desktop;
        this.eventListener = eventListener;
        this.gNazione = gNazione;
    }

    @Override
    public void run() 
    {
        try 
        {
            desktop.enableServerPush(true);
            TelefonateSuCellulareDAO tdao = (TelefonateSuCellulareDAO) SpringUtil.getBean("TelefonateSuCellulareDAO");
            List<TelefonateSuCellulare> lstTel = tdao.findAll(gNazione);
            Executions.schedule(desktop, eventListener, new Event("onReadTel",null, lstTel));
            desktop.enableServerPush(false);
        } catch (Exception e) 
        {
            System.err.println("RicontattoCellulareRun: " + e.getMessage());
        }
    }
}

Menu

@Wire("#timer")
    Timer timer;
@AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view)
    {
...
    timer.addEventListener("onReadTel", new EventListener()
    {
        @Override
        public void onEvent(Event event) throws Exception 
        {
            System.out.println("WWW");
        }
    });

    RicontattoCellulare.getExecutor().execute((new RicontattoCellulareRun(desktop,(EventListener) timer, gNazione)));
delete flag offensive retag edit

Comments

last line, new RicontattoCellulareRun(desktop,(EventListener) timer, gNazione) => a timer isn't an eventListener.so your casting here is not correct.

chillworld ( 2017-04-20 06:41:56 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2017-04-27 16:35:43 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

updated 2017-04-27 16:53:49 +0800

Ok... after long elacubrations I solved in this way.

I hope will be useful

/Luca

timer.addEventListener("onReadTel", new EventListener()
        {
            @Override
            public void onEvent(Event event) throws Exception 
            {
                System.out.println("WWW");
            }
        });

        timer.addEventListener("onTimer", new EventListener()
        {
            @Override
            public void onEvent(Event event) throws Exception 
            {
                lblOrario.setValue(dateFormat.format(java.util.Calendar.getInstance(locale).getTime()));
                executor.execute(new RicontattoCellulareRun(desktop, gNazione, timer));
            }
        });

        executor=RicontattoCellulare.getExecutor();

RicontattoCellulareRun

 public class RicontattoCellulareRun implements Runnable
{

    private final Desktop desktop;
    private final Nazioni nazione;
    private final Timer timer;

    public RicontattoCellulareRun(Desktop desktop, Nazioni nazione, Timer timer)
    {
        this.desktop = desktop;
        this.nazione = nazione;
        this.timer = timer;
        if(!desktop.isServerPushEnabled())
            desktop.enableServerPush(true);
    }

    @Override
    public void run() 
    {
        try 
        {
            Executions.activate(desktop);
            TelefonateSuCellulareDAO tdao = (TelefonateSuCellulareDAO) SpringUtil.getBean("TelefonateSuCellulareDAO");
            List<TelefonateSuCellulare> lstTel = tdao.findAll(nazione);
            final Event event = new Event("onReadTel", timer, lstTel);
            Executions.schedule(desktop, new EventListener<Event>() {
                @Override
                public void onEvent(Event evt) throws Exception 
                {
                    Events.sendEvent(evt);
                }
            }, event);

            Executions.deactivate(desktop);
        } catch (Exception e) 
        {
            System.err.println("RicontattoCellulareRun: " + e.getMessage());
        }
    }


}
link publish delete flag offensive edit
0

answered 2017-04-27 08:17:48 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

Thanks chillworld, any suggestion to resolve my problem? The purpose of performing this task every 5 minutes...

link publish delete flag offensive 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
2 followers

RSS

Stats

Asked: 2017-04-19 13:43:32 +0800

Seen: 36 times

Last updated: Apr 27 '17

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