IDEMPIERE-391 Scheduler improvements. Fixed session context management issue.

This commit is contained in:
Heng Sin Low 2012-12-18 11:48:50 +08:00
parent 4ff1981554
commit 16773b295b
9 changed files with 26 additions and 36 deletions

View File

@ -232,7 +232,8 @@ public final class Env
} }
/** /**
* Set Context * Replace the contents of the current session/process context.
* Don't use this to setup a new session/process context, use ServerContext.setCurrentInstance instead.
* @param ctx context * @param ctx context
*/ */
public static void setCtx (Properties ctx) public static void setCtx (Properties ctx)

View File

@ -23,6 +23,7 @@ import java.text.SimpleDateFormat;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.util.ServerContext;
import org.compiere.model.MAttachment; import org.compiere.model.MAttachment;
import org.compiere.model.MClient; import org.compiere.model.MClient;
import org.compiere.model.MNote; import org.compiere.model.MNote;
@ -106,8 +107,7 @@ public class Scheduler extends AdempiereServer
Timestamp ts = new Timestamp(System.currentTimeMillis()); Timestamp ts = new Timestamp(System.currentTimeMillis());
SimpleDateFormat dateFormat4Timestamp = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat dateFormat4Timestamp = new SimpleDateFormat("yyyy-MM-dd");
Env.setContext(m_schedulerctx, "#Date", dateFormat4Timestamp.format(ts)+" 00:00:00" ); // JDBC format Env.setContext(m_schedulerctx, "#Date", dateFormat4Timestamp.format(ts)+" 00:00:00" ); // JDBC format
Properties currentctx = Env.getCtx(); ServerContext.setCurrentInstance(m_schedulerctx);
Env.setCtx(m_schedulerctx);
MProcess process = new MProcess(m_schedulerctx, m_model.getAD_Process_ID(), null); MProcess process = new MProcess(m_schedulerctx, m_model.getAD_Process_ID(), null);
try try
@ -129,8 +129,8 @@ public class Scheduler extends AdempiereServer
m_trx.close(); m_trx.close();
} }
// Restore system context // clear thread local context
Env.setCtx(currentctx); ServerContext.dispose();
// //
int no = m_model.deleteLog(); int no = m_model.deleteLog();

View File

@ -18,6 +18,7 @@ import java.util.Properties;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.RestletUtil; import org.adempiere.util.RestletUtil;
import org.adempiere.util.ServerContext;
import org.compiere.interfaces.impl.ServerBean; import org.compiere.interfaces.impl.ServerBean;
import org.compiere.model.MRole; import org.compiere.model.MRole;
import org.compiere.process.ProcessInfo; import org.compiere.process.ProcessInfo;
@ -61,12 +62,9 @@ public class ExecuteProcessCommand extends ServerResource {
} }
ServerBean bean = new ServerBean(); ServerBean bean = new ServerBean();
//back up properties
Properties backup = new Properties();
backup.putAll(Env.getCtx());
try try
{ {
Env.setCtx(context); ServerContext.setCurrentInstance(context);
if (procedureName != null && procedureName.trim().length() > 0) if (procedureName != null && procedureName.trim().length() > 0)
return bean.dbProcess(context, pi, procedureName); return bean.dbProcess(context, pi, procedureName);
else else
@ -74,7 +72,7 @@ public class ExecuteProcessCommand extends ServerResource {
} }
finally finally
{ {
Env.setCtx(backup); ServerContext.dispose();
} }
} }

View File

@ -18,6 +18,7 @@ import java.util.Properties;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.RestletUtil; import org.adempiere.util.RestletUtil;
import org.adempiere.util.ServerContext;
import org.compiere.interfaces.impl.ServerBean; import org.compiere.interfaces.impl.ServerBean;
import org.compiere.model.MRole; import org.compiere.model.MRole;
import org.compiere.util.Env; import org.compiere.util.Env;
@ -57,17 +58,14 @@ public class ExecuteTaskCommand extends ServerResource {
} }
ServerBean bean = new ServerBean(); ServerBean bean = new ServerBean();
//back up properties
Properties backup = new Properties();
backup.putAll(Env.getCtx());
try try
{ {
Env.setCtx(context); ServerContext.setCurrentInstance(context);
return bean.executeTask(context, AD_Task_ID); return bean.executeTask(context, AD_Task_ID);
} }
finally finally
{ {
Env.setCtx(backup); ServerContext.dispose();
} }
} }
} }

View File

@ -18,6 +18,7 @@ import java.util.Properties;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.RestletUtil; import org.adempiere.util.RestletUtil;
import org.adempiere.util.ServerContext;
import org.compiere.interfaces.impl.ServerBean; import org.compiere.interfaces.impl.ServerBean;
import org.compiere.model.MRole; import org.compiere.model.MRole;
import org.compiere.process.ProcessInfo; import org.compiere.process.ProcessInfo;
@ -60,17 +61,14 @@ public class ExecuteWorkflowCommand extends ServerResource {
} }
ServerBean bean = new ServerBean(); ServerBean bean = new ServerBean();
//back up properties
Properties backup = new Properties();
backup.putAll(Env.getCtx());
try try
{ {
Env.setCtx(context); ServerContext.setCurrentInstance(context);
return bean.workflow(context, pi, AD_Workflow_ID); return bean.workflow(context, pi, AD_Workflow_ID);
} }
finally finally
{ {
Env.setCtx(backup); ServerContext.dispose();
} }
} }
} }

View File

@ -18,6 +18,7 @@ import java.util.Properties;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.RestletUtil; import org.adempiere.util.RestletUtil;
import org.adempiere.util.ServerContext;
import org.compiere.interfaces.impl.ServerBean; import org.compiere.interfaces.impl.ServerBean;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.restlet.representation.Representation; import org.restlet.representation.Representation;
@ -57,17 +58,14 @@ public class PostDocumentCommand extends ServerResource {
boolean force = (Boolean) entity.get("force"); boolean force = (Boolean) entity.get("force");
ServerBean bean = new ServerBean(); ServerBean bean = new ServerBean();
//back up properties
Properties backup = new Properties();
backup.putAll(Env.getCtx());
try try
{ {
Env.setCtx(context); ServerContext.setCurrentInstance(context);
msg = bean.postImmediate(context, AD_Client_ID, AD_Table_ID, Record_ID, force); msg = bean.postImmediate(context, AD_Client_ID, AD_Table_ID, Record_ID, force);
} }
finally finally
{ {
Env.setCtx(backup); ServerContext.dispose();
} }
return msg; return msg;
} }

View File

@ -18,6 +18,7 @@ import java.util.Properties;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.RestletUtil; import org.adempiere.util.RestletUtil;
import org.adempiere.util.ServerContext;
import org.compiere.interfaces.impl.ServerBean; import org.compiere.interfaces.impl.ServerBean;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.restlet.representation.Representation; import org.restlet.representation.Representation;
@ -53,17 +54,14 @@ public class ResetCacheCommand extends ServerResource {
String tableName = (String) entity.get("tableName"); String tableName = (String) entity.get("tableName");
ServerBean bean = new ServerBean(); ServerBean bean = new ServerBean();
//back up properties
Properties backup = new Properties();
backup.putAll(Env.getCtx());
try try
{ {
Env.setCtx(context); ServerContext.setCurrentInstance(context);
return bean.cacheReset(context, tableName, Record_ID); return bean.cacheReset(context, tableName, Record_ID);
} }
finally finally
{ {
Env.setCtx(backup); ServerContext.dispose();
} }
} }
} }

View File

@ -18,6 +18,7 @@ import java.util.Properties;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.RestletUtil; import org.adempiere.util.RestletUtil;
import org.adempiere.util.ServerContext;
import org.compiere.interfaces.impl.ServerBean; import org.compiere.interfaces.impl.ServerBean;
import org.compiere.util.EMail; import org.compiere.util.EMail;
import org.compiere.util.Env; import org.compiere.util.Env;
@ -53,17 +54,14 @@ public class SendEmailCommand extends ServerResource {
EMail email = (EMail) entity.get("email"); EMail email = (EMail) entity.get("email");
ServerBean bean = new ServerBean(); ServerBean bean = new ServerBean();
//back up properties
Properties backup = new Properties();
backup.putAll(Env.getCtx());
try try
{ {
Env.setCtx(context); ServerContext.setCurrentInstance(context);
return bean.sendEMail(context, email); return bean.sendEMail(context, email);
} }
finally finally
{ {
Env.setCtx(backup); ServerContext.dispose();
} }
} }
} }

View File

@ -8,6 +8,7 @@ import java.text.DecimalFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Properties; import java.util.Properties;
import org.adempiere.util.ServerContext;
import org.compiere.model.MUser; import org.compiere.model.MUser;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
@ -113,7 +114,7 @@ public class CompiereService {
{ {
CompiereUtil.initWeb(); CompiereUtil.initWeb();
Env.setCtx(m_ctx); ServerContext.setCurrentInstance(m_ctx);
Env.setContext( m_ctx, "#AD_Language", "en_US" ); Env.setContext( m_ctx, "#AD_Language", "en_US" );
m_lang = Language.getLanguage("en_US"); m_lang = Language.getLanguage("en_US");