aspose file tools*
The moose likes Web Services and the fly likes Asynchronous web service : Thread getting terminated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Asynchronous web service : Thread getting terminated " Watch "Asynchronous web service : Thread getting terminated " New topic
Author

Asynchronous web service : Thread getting terminated

Wiked Vicky Malhotra
Greenhorn

Joined: Dec 11, 2010
Posts: 10
Hello,

I was trying one of the examples from the book "Java web services: up and running" by Martin Kalin. Following is code snippet from my client:

ItemSearch search = new ItemSearch();
search.getRequest().add(request);
search.setAWSAccessKeyId(accessKey);

port.itemSearchAsync(search, new MyHandler());

try{
Thread.sleep(400);
}catch(Exception e){
System.out.println(e);
}


Also the MyHandler class:

public class MyHandler implements AsyncHandler<ItemSearchResponse> {
public void handleResponse(Response<ItemSearchResponse> future){
try{
// Unpack the response to print the book titles.
ItemSearchResponse response = future.get();
List<Items> listItems = response.getItems();
for (Items next : listItems)
for (Item item : next.getItem() )// each Item in the list
System.out.println(item.getItemAttributes().getTitle());
}catch(InterruptedException e){
System.out.println(e);
}catch(Exception e){
System.out.println(e);
}

}
}


Now the problem I am facing is every time i run my Asynchronous client the main thread gets terminated and no results are thrown. However when i changed the milliseconds in Thread.sleep() to 10 I got the response from Amazon service but only once. After that even this was not working.

can anyone tell me what am I doing wrong???
Wiked Vicky Malhotra
Greenhorn

Joined: Dec 11, 2010
Posts: 10
Hello,

Since no one replied to this post and I found the solution so I would write the solution myself. Basically in the statement thread.sleep(m) the value of m should be large enough that the main thread remains in the JVM for long enough that the asynchronous Web service thread receives the response and could print results.


instead of this we could use wait() and notify() on the MyHandler() object. for example instead of :

port.itemSearchAsync(search, new MyHandler());

try{
Thread.sleep(400);
}catch(Exception e){
System.out.println(e);
}



we could use:

MyHandler myHandler= new MyHandler();
port.itemSearchAsync(search, myHandler);

try{
synchronized (myHandler) {

myHandler.wait();
}




and in the MyHandler class call notify() method as:

synchronized (this) {
this.notify();
}



i hope this helps.......


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Asynchronous web service : Thread getting terminated