I am working for a bank and I develop and application with Java 1.4.
I have to call asynchronously some certain AS400 services, so I decided to use this approach: I create a private static Map and I populate it in a static block. When I have to call the instance, I get the name of the service to call as parameter, search if the map contains that String as key and instantiate the service class.
The problem I've encountered is that, having many asynchronous processes working at the same time on the same JVM, they migth retrieve the same service class instance at the same time.
For example:
Since the map is static, I will provide to the processes always the same instance of the worker class, however, each process will have a different input and expecting different output from the same service.
Here is the code I've explained:
public class AS400ServiceFactory {
private static Map serviceMap;
static {
serviceMap = new HashMap();
serviceMap.put("WBX133", new Wbx133Service());
serviceMap.put("WBX134", new Wbx134Service());
serviceMap.put("WBX220", new Wbx220Service());
serviceMap.put("WBX360", new Wbx360Service());
serviceMap.put("WBX370", new Wbx370Service());
serviceMap.put("WBX371", new Wbx371Service());
}
public static AS400Container getInstance(String serviceName) {
ASService service = null;
if (serviceMap.containsKey(serviceName)) {
service = (ASService) serviceMap.get(serviceName);
}
}
}
A quick (but, in my opinion, not elegant) solution would be, instead of having a static map, just:
public class AS400ServiceFactory {
public static AS400Container getInstance(String serviceName) {
ASService service = null;
if (("WBX133").equals(serviceName)) {
service = new Wbx133Service();
}
if (("WBX134").equals(serviceName)) {
service = new Wbx134Service();
}
if (("WBX220").equals(serviceName)) {
service = new Wbx220Service();
}
if (("WBX360").equals(serviceName)) {
service = new Wbx360Service();
}
if (("WBX370").equals(serviceName)) {
service = new Wbx370Service();
}
if (("WBX371").equals(serviceName)) {
service = new Wbx371Service();
}
}
}
In this way I won't have anymore the problem of having the same instance always. However, I will have in the future something like 60-70 services and having a 60-70 if-else if is not really readable.
Any suggestion is apreciated. Thanks in advance, Francesco
You might create a map of service factories and then use it.
public class AS400ServiceFactory {
private static Map factories = createFactories();
private static Map createFactories() {
Map factories = new HashMap();
factories.put("WBX133", new ServiceFactory(){ public ASService create() { return new Wbx133Service(); } });
factories.put("WBX134", new ServiceFactory(){ public ASService create() { return new Wbx134Service(); } });
factories.put("WBX220", new ServiceFactory(){ public ASService create() { return new Wbx220Service(); } });
// More factories here
return factories;
}
public static AS400Container getInstance(String serviceName) {
ServiceFactory factory = (ServiceFactory)factories.get(serviceName);
ASService service = null;
if (factory != null) {
service = factory.create();
}
// Rest of the method
}
private interface ServiceFactory {
ASService create();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With