answered
2014-09-07 06:51:35 +0800
chillworld 5367 ● 4 ● 9
https://github.com/chillw...
You must ask yourself if server AAA wait for the response of server BBB.
Normally he does and your next line of code is when you have reach the answer.
I work also with webservices and you use it like a DB. (DB is normally also on server CCC).
Like I understand there are 2 possibility's.
First one :
You just call the webservice from your viewmodel through the service tier.
Now nothing is broken, and just do notifychanged
.
Second one :
Put it in a thread if you need it to be in the background.
Nothing is wrong with creating a thread and then follow your links.
Edit :
Example how you could use thread and even notify the correct composer
It's not in ZK envirment but you found already the 2 links for threads so just implement that.
Composer.java :
/**
*
* @author chillworld
*/
public class Composer {
private int number;
public Composer(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public void callManagerForData() {
Thread t = new CallWebService() {
@Override
public void run() {
System.out.println("add obeserver " + getNumber());
Manager.INSTANCE.callWebService(this);
}
};
t.start();
}
public void refresh(String data) {
System.out.println("Refresh done of composer " + number + " with data : '" + data + "'");
}
abstract class CallWebService extends Thread implements Observer {
@Override
public void update(Observable o, Object arg) {
refresh((String)arg);
}
}
}
Manager.java:
/**
*
* @author chillworld
*/
public enum Manager {
INSTANCE;
private AnswerWebService service = new AnswerWebService();
public void callWebService(Observer o) {
service.addObserver(o);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
}
service.postAnswer();
}
}
AnswerWebService.java:
/**
*
* @author cossaer.f
*/
public class AnswerWebService {
private final List<Observer> observers = Collections.synchronizedList(new ArrayList<Observer>());
public void postAnswer() {
// handle data
synchronized (observers) {
if (observers.size() > 0) {
Observer o = observers.remove(0);
o.update(null, "mine data");
}
}
}
public boolean addObserver (Observer o) {
return observers.add(o);
}
}
TestObserverPattern.java:
/**
*
* @author chillworld
*/
public class TestObserverPattern {
public static void main(String[] args) {
List<Composer> composers = new ArrayList<Composer>();
for (int i = 0; i<20;i++) {
composers.add(new Composer(i));
}
Collections.shuffle(composers);
for (Composer c : composers) {
c.callManagerForData();
}
}
}
Hope this could be usefull in your case.
Take care, cause I don't know how you link the asyn call to the answer, I created this case to link first call => first answer.
This is normally open for bugs when call's come at close time rate.
Greetz chill.