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

邮差的信 提交于 2019-12-10 16:37:28

问题


In wildfly I execute stateless ejb method asynchronously (it is mapped with @Asynchronous annotation). In the calling method I have some context information in thread local. What is the best way to pass this data to async method? I don't want to add additional parameter to async method signature.


回答1:


Essentially you have only 2 options:

  1. Passing value as a parameter
  2. Storing that value in some global place. Like static variable.

The first option is much cleaner and easier. Don't use the second one :)




回答2:


With a bit of ugly plumbing it can be resolved as follows (wildfly 8.x.x):

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 context data in the additional field of its instance.



来源:https://stackoverflow.com/questions/37430969/is-there-clean-way-to-pass-context-data-to-asynchronous-ejb-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!