1

Is it possible to check whether a function was called from a async function or from a normal synchronous function?

void syncFunction(){
    functionWithCheck();
}

@Async
void asyncFunction(){
    functionWithCheck();
}

void functionWithCheck(){
    // here I want to find out if the function was called from  an async or sync context 
    if(...){
        System.out.println("hello from asyncFunction");
    }else{
        System.out.println("hello from syncFunction");
    }
}



simibac
  • 7,672
  • 3
  • 36
  • 48
  • Well, in theory it should be possible. You'd need to get the call stack and use reflection to check for the annotation on the calling method. However, that function might not be called in an async context at all, e.g. if you had `syncFunction()` directly call `asyncFunction()`. Getting it right in all circumstances is probably very hard... just why do you need that information? – Thomas Sep 16 '20 at 07:12
  • 1
    The simplest way would be passing a `boolean` parameter. – Nikolas Charalambidis Sep 16 '20 at 07:14
  • Simply passing a boolean is not an option because the functions are nested more than this toy example. The reason why i want to know is because there are some inconsistencies when I enable MODE_INHERITABLETHREADLOCAL in the security context and the user logs out (https://stackoverflow.com/questions/5246428/spring-security-and-async-authenticated-users-mixed-up). – simibac Sep 16 '20 at 07:44

0 Answers0