KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > client > TestAsyncCall


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package test.client;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import org.apache.axis.client.Call;
22 import org.apache.axis.client.async.AsyncCall;
23 import org.apache.axis.client.async.IAsyncCallback;
24 import org.apache.axis.client.async.IAsyncResult;
25 import org.apache.axis.client.async.Status;
26
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29
30 public class TestAsyncCall extends TestCase {
31     public TestAsyncCall(String JavaDoc name) {
32         super(name);
33     }
34
35     public static Test suite() {
36         return new TestSuite(TestAsyncCall.class);
37     }
38
39     protected void setup() {
40     }
41
42     public void testAsyncPollWeatherService() throws MalformedURLException JavaDoc, InterruptedException JavaDoc {
43         Call call = new Call(new URL JavaDoc("http://live.capescience.com:80/ccx/GlobalWeather"));
44         call.setUseSOAPAction(true);
45         call.setSOAPActionURI("capeconnect:GlobalWeather:StationInfo#listCountries");
46         call.setTimeout(new Integer JavaDoc(15 * 1000));
47         call.setOperationName(new javax.xml.namespace.QName JavaDoc("capeconnect:GlobalWeather:StationInfo", "listCountries"));
48         AsyncCall ac = new AsyncCall(call);
49         IAsyncResult result = ac.invoke(new Object JavaDoc[]{});
50         System.out.println("STARTED");
51         Status status = null;
52         while ((status = result.getStatus()) == Status.NONE) {
53             System.out.print('.');
54             Thread.sleep(50);
55         }
56         System.out.println("FINISHED");
57         if (status == Status.COMPLETED) {
58             String JavaDoc[] c = (String JavaDoc[]) result.getResponse();
59             System.out.println(c.length);
60             for (int i = 0; i < c.length; i++) {
61                 System.out.println(c[i]);
62             }
63         } else if (status == Status.EXCEPTION) {
64             result.getException().printStackTrace();
65         }
66     }
67
68     public void testAsyncCallbackWeatherService() throws MalformedURLException JavaDoc, InterruptedException JavaDoc {
69         final Call call = new Call(new URL JavaDoc("http://live.capescience.com:80/ccx/GlobalWeather"));
70         call.setUseSOAPAction(true);
71         call.setSOAPActionURI("capeconnect:GlobalWeather:StationInfo#listCountries");
72         call.setTimeout(new Integer JavaDoc(15 * 1000));
73         call.setOperationName(new javax.xml.namespace.QName JavaDoc("capeconnect:GlobalWeather:StationInfo", "listCountries"));
74         final AsyncCall ac = new AsyncCall(call, new IAsyncCallback() {
75             public void onCompletion(IAsyncResult result) {
76                 Status status = result.getStatus();
77                 System.out.println(".....FINISHED");
78                 if (status == Status.COMPLETED) {
79                     String JavaDoc[] c = (String JavaDoc[]) result.getResponse();
80                     System.out.println(c.length);
81                     for (int i = 0; i < c.length; i++) {
82                         System.out.println(c[i]);
83                     }
84                 } else if (status == Status.EXCEPTION) {
85                     result.getException().printStackTrace();
86                 }
87                 synchronized (call) {
88                     call.notifyAll();
89                 }
90             }
91         });
92         IAsyncResult result = ac.invoke(new Object JavaDoc[]{});
93         System.out.println("STARTED....");
94         synchronized (call) {
95             call.wait(0);
96         }
97     }
98 }
99
Popular Tags