The EJB 3.0 specification does not allow a business method of a stateless session bean to create new threads. Why is that? What is wrong with creating additional worker thre
One type of workaround:
import java.util.concurrent.Executor;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
@Stateless
public class TransactionalExecutor implements Executor {
@Override @Asynchronous
public void execute(Runnable command) {
command.run();
}
}
Now you can use TransactionalExecutor as an executor:
@Stateless
public class SlowService {
@Inject
Executor command;
public void invoke(){
Runnable command = new Runnable() {
@Override
public void run() {
// heavy task
}
};
command.execute(command);
}
}