有没有干净的方法将上下文数据传递给@Asynchronous ejb 调用?

vin*_*nga 5 java asynchronous wildfly

在wildfly 中,我异步执行无状态ejb 方法(它用@Asynchronous 注释映射)。在调用方法中,我在线程本地中有一些上下文信息。将此数据传递给异步方法的最佳方法是什么?我不想向异步方法签名添加其他参数。

vin*_*nga 0

通过一些丑陋的管道,可以按如下方式解决(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);
}
Run Code Online (Sandbox Code Playgroud)

基本上,您需要创建自己的主体类并在其实例的附加字段中设置上下文数据。