-
1. Re: Separate Static Instances Used in Web Application
sjunejo Jun 16, 2011 10:22 AM (in response to sjunejo)Some more information regarding the above problem. In my web application, I have class called AgentConnector.java which has following code;
ServiceLocator asl = null;
asl = ServiceLocator.getInstance();
XConnectionFactory cxf = asl.lookupXConnectionFactory();
My ServiceLocator class is responsible for lookups and code is as follows;
public final static String XCONNECTION_FACTORY_JNDI_NAME = "java:comp/env/jca/xConnectionFactory";
private static ServiceLocator _instance = null;
private InitialContext _context = null;
private Map<String, XConnectionFactory> _cacheXConnectionFactory = null;
public ServiceLocator() {
try {
_context = new InitialContext();
} catch (NamingException e) {
throw new RuntimeException("Configuration error - cannot create initial context");
}
_cacheXConnectionFactory = Collections.synchronizedMap(new HashMap<String, XConnectionFactory>());
}
public static synchronized ServiceLocator getInstance() {
if (_instance == null) {
_instance = new ServiceLocator();
}
return _instance;
}
public XConnectionFactory lookupXConnectionFactory() {
XConnectionFactory cxFactory = null;
try {
if (_cacheXConnectionFactory.containsKey(XCONNECTION_FACTORY_JNDI_NAME)) {
cxFactory = _cacheXConnectionFactory.get(XCONNECTION_FACTORY_JNDI_NAME);
} else {
Object factoryObj = _context.lookup(XCONNECTION_FACTORY_JNDI_NAME);
cxFactory = (XConnectionFactory) factoryObj;
_cacheXConnectionFactory.put(XCONNECTION_FACTORY_JNDI_NAME, cxFactory);
}
} catch (NamingException e) {
// LOGGER.fatal("Configuration error " + e.getMessage(), e);
throw new RuntimeException("Configuration error [" + XCONNECTION_FACTORY_JNDI_NAME + "] " + e.getMessage());
}
return cxFactory;
}
Now, If I deploy two instances of this application in single jboss 5.1+ instance, both will end up using single instnace of 'ServiceLocator' object because it is 'static'. Now is there anyway I can restrict the access of this 'ServiceLocator' i.e. Second instance should create its own ServiceLocator instance and use it rather than the one created by first instance. I want to acheive this without changing my code but with the help of configurations, class-loading etc etc?
Thanks,
--
SJunejo
-
2. Re: Separate Static Instances Used in Web Application
sjunejo Jul 25, 2011 4:32 AM (in response to sjunejo)Please refer to the link for the possible solution and information.