/[projects]/CircuitBreaker/src/dk/thoerup/circuitbreaker/invocations/UrlInvocation.java
ViewVC logotype

Contents of /CircuitBreaker/src/dk/thoerup/circuitbreaker/invocations/UrlInvocation.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 469 - (show annotations) (download)
Mon Oct 26 12:34:25 2009 UTC (14 years, 7 months ago) by torben
File size: 969 byte(s)
This invocation type will probably be used often so why not have an implementation within the library ?
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[1024];
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 }

  ViewVC Help
Powered by ViewVC 1.1.20