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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3212 - (show annotations) (download)
Thu Dec 28 09:34:47 2017 UTC (6 years, 4 months ago) by torben
File size: 977 byte(s)
Use generics to encapsulate returned value
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<byte[]> {
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 }

  ViewVC Help
Powered by ViewVC 1.1.20