1 |
package dk.thoerup.circuitbreaker.invocations;
|
2 |
|
3 |
import java.io.ByteArrayOutputStream;
|
4 |
import java.io.InputStream;
|
5 |
import java.net.URL;
|
6 |
import java.net.URLConnection;
|
7 |
|
8 |
import dk.thoerup.circuitbreaker.CircuitInvocation;
|
9 |
|
10 |
public class UrlInvocation implements CircuitInvocation {
|
11 |
|
12 |
private String urlStr;
|
13 |
private int timeout;
|
14 |
|
15 |
public UrlInvocation(String url) {
|
16 |
this(url,1000);
|
17 |
}
|
18 |
|
19 |
public UrlInvocation(String url, int timeout) {
|
20 |
this.urlStr = url;
|
21 |
this.timeout = timeout;
|
22 |
}
|
23 |
|
24 |
public byte[] proceed() throws Exception {
|
25 |
URLConnection con = new URL(urlStr).openConnection();
|
26 |
con.setConnectTimeout( timeout );
|
27 |
InputStream is = con.getInputStream();
|
28 |
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
29 |
byte[] message = new byte[8192];
|
30 |
int readBytes = 0;;
|
31 |
while ((readBytes = is.read(message)) != -1) {
|
32 |
os.write(message, 0, readBytes);
|
33 |
}
|
34 |
is.close();
|
35 |
os.close();
|
36 |
return os.toByteArray();
|
37 |
}
|
38 |
|
39 |
}
|