WELD thread model when handling ajax request: multiple threads to process same HTTPRequest?
jervisliu Sep 24, 2012 3:20 AMHi, we are using GWT as our front-end. We are seeing some weird thread behaviors since we migrated to WELD. Below are some of the code snippets:
@ApplicationScoped
public class RepositoryModuleService implements ModuleService {
@Inject
private RulesRepository rulesRepository;
@WebRemote
@LoggedIn
public Module[] listModules() {
return listModules( null );
}
....
}
@ApplicationScoped
public class RepositoryAssetService implements AssetService {
@Inject
private RulesRepository rulesRepository;
@WebRemote
@LoggedIn
public Asset[] listAssets(String moduleName) {
.....
}
....
}
RulesRepository is not thread safe, it is defined as @RequestScoped.
What we found is the AS7 creates multiple concurrent threads to deal with the client request which comes from same browser. Those multiple threads are associated with same HTTPRequest object and shared a same instance of RulesRepository. We run into problems when one thread finishes its job and destroys the RulesRepository object, while other threads are still processing.
So my question is, is this an expected behavior? ie., the container (or CDI?, not sure where this happens) creates multiple threads to process the requests come from a same HTTPRequest. As we are using GWT, presumbablly, the requests are async calls. As you can see from the log below, threads http--127.0.0.1-8080-1/http--127.0.0.1-8080-4/http--127.0.0.1-8080-2 are processing request from a same client (because they have a same org.jboss.weld.context.bound.BoundRequestContextImpl@48dc2d76 object). We got a "Session closed" problem when http--127.0.0.1-8080-1 tried to use the RulesRepository object which was already closed by thread http--127.0.0.1-8080-4.
21:47:55,821 (http--127.0.0.1-8080-1) ----------RulesRepositoryManager:createRulesRepository, sessoin: session-admin-4, httprequest: org.jboss.weld.context.http.HttpRequestContextImpl@2d84dc98 ,thread: 107
21:47:56,091 (http--127.0.0.1-8080-4) ----------RulesRepositoryManager:createRulesRepository, sessoin: session-admin-5, httprequest: org.jboss.weld.context.http.HttpRequestContextImpl@2d84dc98 ,thread: 110
21:47:56,098 (http--127.0.0.1-8080-1) ----------listModules, sessoin: session-admin-5, httprequest: org.jboss.weld.context.bound.BoundRequestContextImpl@48dc2d76 ,thread: 107
21:47:56,098 (http--127.0.0.1-8080-2) ----------listModules, sessoin: session-admin-5, httprequest: org.jboss.weld.context.bound.BoundRequestContextImpl@48dc2d76 ,thread: 108
21:47:56,098 (http--127.0.0.1-8080-5) ----------listModules, sessoin: session-admin-5, httprequest: org.jboss.weld.context.bound.BoundRequestContextImpl@48dc2d76 ,thread: 111
21:47:56,369 (http--127.0.0.1-8080-2) ----------before listSubModules, sessoin: session-admin-5, httprequest: org.jboss.weld.context.bound.BoundRequestContextImpl@48dc2d76 ,thread: 108
21:47:56,369 (http--127.0.0.1-8080-4) ----------RulesRepositoryManager:logout, sessoin: session-admin-5, httprequest: org.jboss.weld.context.http.HttpRequestContextImpl@2d84dc98 ,thread: 110
21:47:56,369 (http--127.0.0.1-8080-5) ----------before listSubModules, sessoin: session-admin-5, httprequest: org.jboss.weld.context.bound.BoundRequestContextImpl@48dc2d76 ,thread: 111
21:47:56,369 (http--127.0.0.1-8080-1) ----------before listSubModules, sessoin: session-admin-5, httprequest: org.jboss.weld.context.bound.BoundRequestContextImpl@48dc2d76 ,thread: 107
21:47:56,384 INFO [org.apache.jackrabbit.core.TransientRepository] (http--127.0.0.1-8080-4) Session closed
21:47:56,379 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/drools-guvnor]] (http--127.0.0.1-8080-1) Exception while dispatching incoming RPC call: com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract org.drools.guvnor.client.rpc.Module[] org.drools.guvnor.client.rpc.ModuleService.listModules()' threw an unexpected exception: org.drools.repository.RulesRepositoryException: javax.jcr.RepositoryException: This session has been closed. See the chained exception for a trace of where the session was closed.
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389) [gwt-servlet-2.5.0-rc1.jar:]
Am I missing anything? Is there sth wrong when AS7 or WELD handls request from AJAX client?
Thanks,
Jervis