Is there a clean way to pass context data in @Asynchronous ejb call?

In wildfly, I execute an asynchronous asynchronous method without saving (it maps to @Asynchronous annotation). In the calling method, I have some contextual information in the local thread. What is the best way to pass this data to async method? I do not want to add an additional parameter to the async method signature.

+4
source share
2 answers

With a bit of ugly plumbing, it can be solved as follows (wildfly 8.xx):

if (SecurityContextAssociation.getSecurityContext()==null)
    SecurityContextAssociation.setSecurityContext(new JBossSecurityContext("background-job"));
SecurityContext current = SecurityContextAssociation.getSecurityContext();
final Object cred = current.getUtil().getCredential();
final Subject s = current.getUtil().getSubject();
final Principal up = current.getUtil().getUserPrincipal();
boolean needToUpdatePrincipal=true;
if (up instanceof TenantPrincipal) {
    if (t.getTenantName().equals(((TenantPrincipal) up).getAdditonalField())) {
        needToUpdatePrincipal=false;
    }
}
if (needToUpdatePrincipal) {
    TenantPrincipal tp=new TenantPrincipal(up.getName());
    tp.setAdditionalField(t.getTenantName());
    current.getUtil().createSubjectInfo(
            , cred, (Subject) s);
}

Basically, you need to create your own Principal class and set contextual data in an additional field of its instance.

0
source

2 :

  • . .

. :)

+1

All Articles