Calling Service from Core Module

I have a long running database task, which can vary from few minutes to few hours depending on the data to be processed. Since the background Task is dependent on the screen . I am calling an ExecutorService from Background Task,so even if the screen is closed the task is not interrupted.

At the end of the process , the ExecutorService process write summarized data into Excel.
First approach is write into separate folder then using fileuploadservice upload the excel file.
Second approach is read the bytes and add using fileloader service ( )

I prefer the second approach and so i created a Service to perform the operation,but since the service is being called from Core Module , its throwing null pointer exception , where as if i manually call it via web module(screen) its working.

I cannot use AppContext Listener becuase users needs to call this Executor Service again and again using different input parameter.

How can i call Service from Core Module ? Also do you suggest any other better way other than ExecutorService,which cuba supports.

You should be able to call services from core the same way as from the client tier: by obtaining a reference using injection to a managed bean or using AppBeans.get() method.
Could you clarify what exception you get and how you invoke the service?

I am getting the error -
java.util.concurrent.ExecutionException: java.lang.SecurityException: No security context bound to the current thread.

Based on the link -
I tried adding the security context but still the same above error.

<b>public void </b>loadFiles(ExecutorService executor,XSSFWorkbook wb, String fileName) <b>throws </b>ExecutionException, InterruptedException

    <i>//ExecutorService executor = Executors.newSingleThreadExecutor();
</i><i>    </i>Future future = executor.submit(<b>new </b>SecurityContextAwareRunnable(<b>new </b>Runnable() {
        <b>public void </b>run() {
            AppBeans.<i>get</i>(ExcelService.<b>class</b>).execute(wb, fileName);
    <i>/*final SecurityContext securityContext = AppContext.getSecurityContext();
</i><i>    Future future = executor.submit(new Runnable() {
</i><i>        @Override
</i><i>        public void run() {
</i><i>            AppContext.setSecurityContext(securityContext);
</i><i>            System.out.println("hit");
</i><i>            AppBeans.get(ExcelService.class).execute(wb, fileName);
</i><i>            System.out.println("complete");
</i><i>            //return 1;
</i><i>        }
</i><i>    });*/
</i><i>    </i>future.get();


In ExcelService , iam loading the workbook bytes using fileloader and datamanger. ( )

OK, but where do you invoke loadFiles() from?

I am triggering ExecutorService.submit(Callable) in Service.
This callable inturn calls multiple java

Screen -> Background Task -> Service ->ExecutorService
ExecutorService -> Callable
—> Java Class1
—> Java Class2
—> Java Class3
—> LoadFiles

The reason I am doing this is becuase when the screen is closed ,the process gets terminated and I have a long process and so i call the ExecutorService.submit(Callable) and completes the service, but in the background the callable is triggering various subclasses and finally loadFiles.

Please let me know if you need specific details.

I’m just wondering if the SecurityContext is present at the moment when the callable is submitted. Could you check it by printing AppContext.getSecurityContext() before submit?

Yes iam getting value - SecurityContext{sessionId=df2aa7fc-cca2-62fc-37b0-a078d4565589}

Should i pass it as part of the Input parameter in callable.

No, it should be passed automatically by SecurityContextAwareRunnable. Could you provide the whole exception stacktrace?