【发布时间】:2025-12-13 21:55:02
【问题描述】:
我有一个关于身份验证和 TimerService @Timeout 注释的问题:
在我们的应用程序中,用户可以安排任务以给定的时间间隔执行,他们从任务列表中进行选择,每个任务都与应用程序中的特定 EJB 相关联。当用户按下保存按钮时,一个新的计时器将添加到 Timer 服务中,当调用 @Timeout 方法时,使用 @Timeout 方法调用用户指定的 EJB 上的一个方法
Timerservice 调用超时方法时,当前主体为“ANONYMOUS”。如果您需要从 timout 方法中调用任何受保护的 EJB,这将是有问题的。是否可以将当前的主体和角色更改为与创建计时器的用户相同的主体和角色?
@Singleton
@LocalBean
public class TestEJB
{
@Resource
SessionContext context;
@Resource
TimerService timerService;
@Timeout
public void execute(Timer timer) throws Exception {
//Get the current username
System.out.println(context.getCallerPrincipal().getName());
}
public void createScheduledTimer(ScheduleExpression e,String timerId) throws IllegalArgumentException, IllegalStateException, EJBException {
TimerConfig tc = new TimerConfig();
tc.setInfo(timerId);
tc.setPersistent(true);
timerService.createCalendarTimer(e, tc);
}
}
【问题讨论】:
标签: java authentication timer ejb-3.1