server push thread doesn't stop after browser refresh

Link to SF Tracker: http://sourceforge.net/support/tracker.php?aid=2979748
This commit is contained in:
Heng Sin Low 2010-03-31 04:04:31 +00:00
parent e2bb7765dd
commit 8d75aa84ee
6 changed files with 114 additions and 15 deletions

View File

@ -30,6 +30,7 @@ import org.adempiere.webui.component.ZoomCommand;
import org.adempiere.webui.desktop.DefaultDesktop; import org.adempiere.webui.desktop.DefaultDesktop;
import org.adempiere.webui.desktop.IDesktop; import org.adempiere.webui.desktop.IDesktop;
import org.adempiere.webui.event.TokenEvent; import org.adempiere.webui.event.TokenEvent;
import org.adempiere.webui.session.SessionContextListener;
import org.adempiere.webui.session.SessionManager; import org.adempiere.webui.session.SessionManager;
import org.adempiere.webui.theme.ThemeManager; import org.adempiere.webui.theme.ThemeManager;
import org.adempiere.webui.util.BrowserToken; import org.adempiere.webui.util.BrowserToken;
@ -91,6 +92,10 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
private static final CLogger logger = CLogger.getCLogger(AdempiereWebUI.class); private static final CLogger logger = CLogger.getCLogger(AdempiereWebUI.class);
public static final String EXECUTION_CARRYOVER_SESSION_KEY = "execution.carryover";
public static final String ZK_DESKTOP_SESSION_KEY = "zk.desktop";
public AdempiereWebUI() public AdempiereWebUI()
{ {
this.addEventListener(Events.ON_CLIENT_INFO, this); this.addEventListener(Events.ON_CLIENT_INFO, this);
@ -106,7 +111,8 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
Properties ctx = Env.getCtx(); Properties ctx = Env.getCtx();
langSession = Env.getContext(ctx, Env.LANGUAGE); langSession = Env.getContext(ctx, Env.LANGUAGE);
SessionManager.setSessionApplication(this); SessionManager.setSessionApplication(this);
if (!SessionManager.isUserLoggedIn(ctx)) Session session = Executions.getCurrent().getDesktop().getSession();
if (session.getAttribute(SessionContextListener.SESSION_CTX) == null || !SessionManager.isUserLoggedIn(ctx))
{ {
loginDesktop = new WLogin(this); loginDesktop = new WLogin(this);
loginDesktop.createPart(this.getPage()); loginDesktop.createPart(this.getPage());
@ -186,7 +192,7 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
IDesktop d = (IDesktop) currSess.getAttribute("application.desktop"); IDesktop d = (IDesktop) currSess.getAttribute("application.desktop");
if (d != null && d instanceof IDesktop) if (d != null && d instanceof IDesktop)
{ {
ExecutionCarryOver eco = (ExecutionCarryOver) currSess.getAttribute("execution.carryover"); ExecutionCarryOver eco = (ExecutionCarryOver) currSess.getAttribute(EXECUTION_CARRYOVER_SESSION_KEY);
if (eco != null) { if (eco != null) {
//try restore //try restore
try { try {
@ -229,8 +235,10 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
component.setPage(this.getPage()); component.setPage(this.getPage());
} }
appDesktop.setPage(this.getPage()); appDesktop.setPage(this.getPage());
currSess.setAttribute("execution.carryover", current); currSess.setAttribute(EXECUTION_CARRYOVER_SESSION_KEY, current);
} }
currSess.setAttribute(ZK_DESKTOP_SESSION_KEY, this.getPage().getDesktop());
} catch (Throwable t) { } catch (Throwable t) {
//restore fail //restore fail
appDesktop = null; appDesktop = null;
@ -247,7 +255,8 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
appDesktop.createPart(this.getPage()); appDesktop.createPart(this.getPage());
currSess.setAttribute("application.desktop", appDesktop); currSess.setAttribute("application.desktop", appDesktop);
ExecutionCarryOver eco = new ExecutionCarryOver(this.getPage().getDesktop()); ExecutionCarryOver eco = new ExecutionCarryOver(this.getPage().getDesktop());
currSess.setAttribute("execution.carryover", eco); currSess.setAttribute(EXECUTION_CARRYOVER_SESSION_KEY, eco);
currSess.setAttribute(ZK_DESKTOP_SESSION_KEY, this.getPage().getDesktop());
} }
if ("Y".equalsIgnoreCase(Env.getContext(ctx, BrowserToken.REMEMBER_ME)) && MSystem.isZKRememberUserAllowed()) if ("Y".equalsIgnoreCase(Env.getContext(ctx, BrowserToken.REMEMBER_ME)) && MSystem.isZKRememberUserAllowed())
@ -288,6 +297,7 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
public void logout() public void logout()
{ {
appDesktop.logout(); appDesktop.logout();
Executions.getCurrent().getDesktop().getSession().getAttributes().clear();
MSession mSession = MSession.get(Env.getCtx(), false); MSession mSession = MSession.get(Env.getCtx(), false);
if (mSession != null) { if (mSession != null) {

View File

@ -18,7 +18,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.webui.AdempiereWebUI;
import org.adempiere.webui.desktop.IDesktop; import org.adempiere.webui.desktop.IDesktop;
import org.adempiere.webui.session.ServerContext; import org.adempiere.webui.session.ServerContext;
import org.adempiere.webui.session.SessionContextListener; import org.adempiere.webui.session.SessionContextListener;
@ -27,6 +29,7 @@ import org.compiere.model.MSysConfig;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.zkoss.util.Locales; import org.zkoss.util.Locales;
import org.zkoss.zk.ui.Desktop; import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.DesktopUnavailableException;
/** /**
* *
@ -45,7 +48,6 @@ public class DashboardRunnable implements Runnable, Serializable
private IDesktop appDesktop; private IDesktop appDesktop;
private Locale locale; private Locale locale;
@SuppressWarnings("unused")
private static final CLogger logger = CLogger.getCLogger(DashboardRunnable.class); private static final CLogger logger = CLogger.getCLogger(DashboardRunnable.class);
private final static String ZK_DASHBOARD_REFRESH_INTERVAL = "ZK_DASHBOARD_REFRESH_INTERVAL"; private final static String ZK_DASHBOARD_REFRESH_INTERVAL = "ZK_DASHBOARD_REFRESH_INTERVAL";
@ -63,6 +65,12 @@ public class DashboardRunnable implements Runnable, Serializable
locale = Locales.getCurrent(); locale = Locales.getCurrent();
} }
public DashboardRunnable(DashboardRunnable tmp, Desktop desktop,
IDesktop appDesktop) {
this(desktop, appDesktop);
this.dashboardPanels = tmp.dashboardPanels;
}
public void run() public void run()
{ {
// default Update every one minutes // default Update every one minutes
@ -76,13 +84,65 @@ public class DashboardRunnable implements Runnable, Serializable
if (desktop.isAlive()) { if (desktop.isAlive()) {
Locales.setThreadLocal(locale); Locales.setThreadLocal(locale);
refreshDashboard(); try {
refreshDashboard();
} catch (DesktopUnavailableException de) {
killSession();
break;
} catch (Exception e) {
logger.log(Level.INFO, e.getLocalizedMessage(), (e.getCause() != null ? e.getCause() : e));
break;
}
} else { } else {
killSession();
break; break;
} }
} }
} }
private void killSession() {
if (desktop.getSession() != null && desktop.getSession().getNativeSession() != null)
{
//differentiate between real destroy and refresh
try
{
Thread.sleep(90000);
}
catch (InterruptedException e)
{
try
{
desktop.getSession().getAttributes().clear();
desktop.getSession().invalidate();
}
catch (Exception e1) {}
return;
}
try
{
Object sessionObj = desktop.getSession().getAttribute(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY);
if (sessionObj != null && sessionObj instanceof Desktop)
{
Desktop sessionDesktop = (Desktop) sessionObj;
//don't destroy session if it have been attached to another desktop ( refresh will do that )
if (sessionDesktop == desktop)
{
desktop.getSession().getAttributes().clear();
desktop.getSession().invalidate();
}
}
else
{
desktop.getSession().getAttributes().clear();
desktop.getSession().invalidate();
}
}
catch (Exception e1) {}
}
}
/** /**
* Refresh dashboard content * Refresh dashboard content
*/ */

View File

@ -397,6 +397,16 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria
if (this.page != page) { if (this.page != page) {
layout.setPage(page); layout.setPage(page);
this.page = page; this.page = page;
if (dashboardThread != null && dashboardThread.isAlive()) {
dashboardRunnable.stop();
dashboardThread.interrupt();
DashboardRunnable tmp = dashboardRunnable;
dashboardRunnable = new DashboardRunnable(tmp, layout.getDesktop(), this);
dashboardThread = new Thread(dashboardRunnable, "UpdateInfo");
dashboardThread.setDaemon(true);
dashboardThread.start();
}
} }
} }

View File

@ -419,6 +419,16 @@ public class NavBar2Desktop extends TabbedDesktop implements MenuListener, Seria
layout.setPage(page); layout.setPage(page);
this.page = page; this.page = page;
} }
if (dashboardThread != null && dashboardThread.isAlive()) {
dashboardRunnable.stop();
dashboardThread.interrupt();
DashboardRunnable tmp = dashboardRunnable;
dashboardRunnable = new DashboardRunnable(tmp, layout.getDesktop(), this);
dashboardThread = new Thread(dashboardRunnable, "UpdateInfo");
dashboardThread.setDaemon(true);
dashboardThread.start();
}
} }
/** /**

View File

@ -435,6 +435,16 @@ public class NavBarDesktop extends TabbedDesktop implements MenuListener, Serial
layout.setPage(page); layout.setPage(page);
this.page = page; this.page = page;
} }
if (dashboardThread != null && dashboardThread.isAlive()) {
dashboardRunnable.stop();
dashboardThread.interrupt();
DashboardRunnable tmp = dashboardRunnable;
dashboardRunnable = new DashboardRunnable(tmp, layout.getDesktop(), this);
dashboardThread = new Thread(dashboardRunnable, "UpdateInfo");
dashboardThread.setDaemon(true);
dashboardThread.start();
}
} }
/** /**

View File

@ -13,10 +13,9 @@
*****************************************************************************/ *****************************************************************************/
package org.adempiere.webui.util; package org.adempiere.webui.util;
import java.util.logging.Level; import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.CLogger;
import org.zkoss.zk.ui.Desktop; import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.DesktopUnavailableException;
import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.Executions;
/** /**
@ -29,8 +28,6 @@ public class ServerPushTemplate {
private Desktop desktop; private Desktop desktop;
private final static CLogger logger = CLogger.getCLogger(ServerPushTemplate.class);
/** /**
* *
* @param desktop * @param desktop
@ -49,16 +46,18 @@ public class ServerPushTemplate {
try { try {
if (!inUIThread) { if (!inUIThread) {
//1 second timeout //half second timeout
if (Executions.activate(desktop, 1000)) { if (Executions.activate(desktop, 500)) {
desktopActivated = true; desktopActivated = true;
} else { } else {
return; throw new DesktopUnavailableException("Timeout activating desktop.");
} }
} }
callback.updateUI(); callback.updateUI();
} catch (DesktopUnavailableException de) {
throw de;
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.INFO, "Server push error="+e.getLocalizedMessage(), e); throw new AdempiereException("Failed to update client in server push worker thread.", e);
} finally { } finally {
if (!inUIThread && desktopActivated) { if (!inUIThread && desktopActivated) {
Executions.deactivate(desktop); Executions.deactivate(desktop);