WildFly injects destroyed bean during remote EJB invocation
noinfo Mar 13, 2015 10:40 AMHi,
I have Stateless EJB that uses another @Request scoped bean.
My EJB looks like this:
@Stateless(name = "HelloService")
@Remote(Service.class)
public class HelloService implements Service {
@Inject
private TimeService timeService;
@Override
public String handleRequest() {
return "Hello: " + timeService.getTime();
}
}
Declared remote interface:
public interface Service {
public String handleRequest();
}
@RequestScoped bean that has a state:
@RequestScoped
public class TimeService {
private boolean disposed;
private String time;
public String getTime() {
if (disposed) {
throw new RuntimeException("already disposed");
}
return time;
}
@PostConstruct
private void initialize() {
time = OffsetDateTime.now().toString();
disposed = false;
System.out.println("initialized");
}
@PreDestroy
private void dispose() {
disposed = true;
System.out.println("disposed");
}
}
That is how I am creating proxy for EJB invocation:
@ApplicationScoped
public class ServiceProvider {
private final InitialContext context;
public ServiceProvider() throws NamingException, IOException {
context = new InitialContext();
}
private <T> T get(String module, String bean, Class<T> contract) throws NamingException {
final String application = "ServiceEAR";
final String distinct = "";
final String name = contract.getName();
T service = (T) context.lookup("ejb:" + application + "/" + module + "/" + distinct + "/" + bean + "!" + name);
return service;
}
public Service getService() throws NamingException {
return get("Services", "HelloService", Service.class);
}
}
And using it like this:
@Named(value = "catcher")
@SessionScoped
public class CallerController implements Serializable {
@Inject
private ServiceProvider provider;
private String value = "";
public String getValue() {
return value;
}
public void callService() {
int i = 0;
String result = "";
try {
for (; i < 5; i++) {
System.out.println("begin call: i = " + i);
String response = provider.getService().handleRequest();
System.out.println(response);
System.out.println("end call");
result += response;
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
System.out.println("end call");
result += ex.getMessage();
}
this.value = result;
}
}
Server log after execution:
13:56:00,597 INFO [stdout] (default task-52) begin call: i = 0 13:56:00,599 INFO [stdout] (default task-52) initialized 13:56:00,599 INFO [stdout] (default task-52) disposed 13:56:00,599 INFO [stdout] (default task-52) Hello: 2015-03-12T13:56:00.599+04:00 13:56:00,599 INFO [stdout] (default task-52) end call 13:56:00,599 INFO [stdout] (default task-52) begin call: i = 1 13:56:00,919 INFO [stdout] (default task-52) java.lang.RuntimeException: already disposed 13:56:00,919 INFO [stdout] (default task-52) end call
So when I called "handleRequest" method via remote interface in my JSF application two times in a row I got RuntimeException with the message "already disposed".
Is there something wrong with @Request scoped bean that it is injected twice even after its destruction? Am I missing some configuration files?
My JSF application deployed outside of EJB's EAR on the same application server. Wildfly version is 8.2.0.Final.
-
RemoteInvocationTest.zip 22.1 KB