Last active
August 7, 2017 08:56
-
-
Save antonarhipov/d10f3abf5b6d8e9265f33fc54c7a6178 to your computer and use it in GitHub Desktop.
Running periodic task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zt; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
public class App { | |
public static void main(String[] args) throws Exception { | |
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | |
scheduler.scheduleAtFixedRate(App::get, 3, 5, TimeUnit.SECONDS); | |
} | |
private static String get() { | |
StringBuilder sb = new StringBuilder(); | |
try { | |
String spec = "http://www.ee"; | |
URL url = new URL(spec); | |
URLConnection urlConnection = url.openConnection(); | |
InputStream inputStream = urlConnection.getInputStream(); | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); | |
String line; | |
while ((line = bufferedReader.readLine()) != null) { | |
sb.append(line); | |
} | |
bufferedReader.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment