final Function callback = function;
final ContextFactory factory = cx.getFactory();
timeout = ((Number) args[1]).longValue();
String uuid = UUID.randomUUID().toString();
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
final int tenantId = carbonContext.getTenantId();
final String tenantDomain = carbonContext.getTenantDomain();
final String applicationName = carbonContext.getApplicationName();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ScheduledFuture future = timerExecutor.schedule(new Callable<Void>() {
public Void call() throws Exception {
//set the context classloader
Thread currentThread = Thread.currentThread();
ClassLoader originalClassLoader = currentThread.getContextClassLoader();
Thread.currentThread().setContextClassLoader(contextClassLoader);
// child inherits context properties form the parent thread.
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
carbonContext.setTenantId(tenantId);
carbonContext.setTenantDomain(tenantDomain);
carbonContext.setApplicationName(applicationName);
try {
Context ctx = RhinoEngine.enterContext(factory);
RhinoEngine.putContextProperty(EngineConstants.JAGGERY_CONTEXT, context);
callback.call(ctx, thisObj, thisObj, params);