package dk.thoerup.circuitbreaker.invocations; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import dk.thoerup.circuitbreaker.CircuitInvocation; public class UrlInvocation implements CircuitInvocation { private String urlStr; private int timeout; public UrlInvocation(String url) { this(url,1000); } public UrlInvocation(String url, int timeout) { this.urlStr = url; this.timeout = timeout; } public byte[] proceed() throws Exception { URLConnection con = new URL(urlStr).openConnection(); con.setConnectTimeout( timeout ); InputStream is = con.getInputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] message = new byte[8192]; int readBytes = 0;; while ((readBytes = is.read(message)) != -1) { os.write(message, 0, readBytes); } is.close(); os.close(); return os.toByteArray(); } }