01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.jaxws.sample.parallelasync.common;
21:
22: import java.util.concurrent.Future;
23: import java.util.concurrent.TimeoutException;
24:
25: import javax.xml.ws.AsyncHandler;
26: import javax.xml.ws.Response;
27:
28: /**
29: * Generic Async callback handler. The get method emulates Response.get by
30: * throwning an exception if one is received.
31: */
32: public class CallbackHandler<T> implements AsyncHandler<T> {
33:
34: private T response = null;
35: private Response<T> resp = null;
36:
37: private Exception exception = null;
38:
39: public void handleResponse(Response<T> response) {
40:
41: try {
42: T res = (T) response.get();
43: this .response = res;
44: this .resp = response;
45: } catch (Exception e) {
46: this .exception = e;
47: }
48: }
49:
50: public Response<T> getResponse() {
51: return this .resp;
52: }
53:
54: public T get() throws Exception {
55:
56: if (exception != null)
57: throw exception;
58: return this .response;
59: }
60:
61: /**
62: * Auxiliary method used to wait for a monitor for a certain amount of time
63: * before timing out
64: *
65: * @param monitor
66: */
67: public void waitBlocking(Future<?> monitor) throws Exception {
68: // wait for request to complete
69: int sec = Constants.CLIENT_MAX_SLEEP_SEC;
70: while (!monitor.isDone()) {
71: Thread.sleep(1000);
72: sec--;
73: if (sec <= 0)
74: break;
75: }
76:
77: if (sec <= 0)
78: throw new TimeoutException(
79: "Stopped waiting for Async response after "
80: + Constants.CLIENT_MAX_SLEEP_SEC + " sec");
81: }
82:
83: }
|