cancel
Showing results for 
Search instead for 
Did you mean: 

JobExecutor to run jobs within a context.

susubhas
Champ in-the-making
Champ in-the-making
Hi,

Is it possible to make jobExecutor to run jobs within some context??. The usecase here is, the async tasks which would communicate with my application would need some user permissions. The process instance starts with this security context in the thread local but this context is not propogated to Jobexecutor as the jobs will be run by altogether different threads.


Thanks
Sujata
1 REPLY 1

frederikherema1
Star Contributor
Star Contributor
Inside Alfresco, activiti uses a custom JobHandler to achieve this:

http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/repository/source/jav...

Before executing the Timer (or any other job you want to handle) a context is built up for the thread, depending on task's assignee of process-initiator. We wrap the default one provided by activiti:


// Execute timer
        AuthenticationUtil.runAs(new RunAsWork<Void>()
        {
            @SuppressWarnings("synthetic-access")
            public Void doWork() throws Exception
            {
                wrappedHandler.execute(job, configuration, execution, commandContext);
                return null;
            }
        }, userName);

Using a custom ProcessEngineConfiguration, we wrap the original job-handler:


@Override
    protected void initJobExecutor()
    {
        super.initJobExecutor();

        // Get the existing timer-job handler and wrap
        // with one that is alfresco-authentication aware
        JobHandler jobHandler = jobHandlers.get(TimerExecuteNestedActivityJobHandler.TYPE);
        JobHandler wrappingJobHandler = new AuthenticatedTimerJobHandler(jobHandler);

        jobHandlers.put(TimerExecuteNestedActivityJobHandler.TYPE, wrappingJobHandler);
    }