Skip to Content
0
Aug 09, 2023 at 12:59 PM

SAP JC0 3.1.7 - Getting destination inside thread. Losing Context

272 Views Last edit Sep 20, 2023 at 01:40 PM 4 rev

The following does not work when i am inside a new thread in Java.

JCoDestinationManager.getDestination("_________")


Context:
The Java application is a springboot application deployed in BTP. It uses the connectivity and destination Service.
It works perfectly fine if I call the above method outside a new thread.
When I create a new thread ( in my case, it is the `ExecutorService` in Java), I get the following

com.sap.conn.jco.JCoException: (106) JCO_ERROR_RESOURCE: Destination _______ does not exist


I am also doing the following to have the security context propagated in the new thread:

SecurityContext context= SecurityContextHolder.getContext();
DelegatingSecurityContextRunnable wrappedRunnable = 
                     new DelegatingSecurityContextRunnable(operation, context);


Here `operation` is a runnable where i call `JCoDestinationManager.getDestination`


Can anybody please guide here what I have to do?

I am even manually setting the security context inside the thread.

Here is the code:

import com.sap.cloud.security.spring.token.SpringSecurityContext;
import com.sap.cloud.security.token.SecurityContext;
import java.util.concurrent.ExecutorService;


private ExecutorService executorService;

...

Token token = SpringSecurityContext.getToken();
JCoDestinationManager.getDestination("_________") //THIS WORKS
final Runnable runnable = () ->{
SecurityContext.setToken(token);
JCoDestinationManager.getDestination("_________"); //THIS DOES NOT WORK }

SecurityContext context= SecurityContextHolder.getContext(); DelegatingSecurityContextRunnable wrappedRunnable = new DelegatingSecurityContextRunnable(runnable, context);

executorService.submit(wrappedRunnable);