I have a library which returns a webservice client.
Client c = Clientlib.getclient();
Now what I did was I created a wrapper on top of it:
public class Myclient {
private static Client c = ClientLib.getClient();
private static instance = new MyClient();
public static MyClient getInstance(){
return instance;
}
private Myclient(){
//singleton
}
public Data getDate(Sting id){
Data1 d1 = cleint.getData();
convert d1 to Data and return data
}
}
Is this the correct way to do this? Should I create only once instance of Client?
Will I have some problem with this approach? What if two threads call the getData method
at the same time. This situation is likely as this is a web based app.
What if the connection to the server goes off and comes back? I dont think it should be a problem as http is stateless.
Generally I think a wrapper is a good idea. You can rebuild the classes from the webservice without affecting the rest of you code. Also allows you to do other things like caching, security etc.
If you intend on MyClient to be a singleton, then Client c doesn’t need to be static. Not a huge thing but worth noting.
Whether there are threading issues depends on the implementation of ClientLib.getClient(). You might need to do some reading to find out if you are allowed to access it in a multithreaded way or whether you should be proving synchronization. By wrapping the client though, you can easily synchronize the access in one place. Generally a webservice should be accessible from multiple threads, it’s just a call over http.
Only other thing I can suggest is the addition of an interface over MyClient just to decouple anything that uses this class. Makes it a little easier for testing.