I have 2 beans that use Injection to "pass" UserData info that is extracted from HttpRequest
. If I remove @Asynchronous
from WorkerBean then its all working and WorkerBean can access UserInfo thats injected down.
However if I use @Asynchronous
on WorkerBean then injection stops working.
What is the best way to manually create/pass UserInfo into WorkerBean if it has to be asynchronous?
// resource class
@Stateless
class MainRs {
@Context
protected HttpServletRequest request;
@Inject
protected UserData userData;
@EJB
WorkerBean job;
@Path("/batch/job1")
public function startJob() {
// call on worker bean
job.execute();
}
}
// user data extracted from HttpRequest
@RequestScoped
@Default
class UserData {
private HttpServletRequest request;
private String userId;
@Inject
public UserData(HttpServletRequest request) {
super();
this.request = request;
userId = request.getHeader("userId");
}
public int getUserId() {
return userId;
}
}
@Stateless
@Asynchronous
class WorkerBean {
private UserData userData;
// inject userData rom caller bean
@Inject
public WorkerBean(UserData userData) {
super();
this.userData = userData;
}
public function execute() {
String userId = userData.getUserId();
// do something
}
}