Before building the GUI, get your scheduled page retrievals working on the console.
Here is an example app doing just that. Every five seconds for a half-minute we download a page and dump its various parts to console.
This demo uses the HttpClient
classes added to Java 11 per JEP 321: HTTP Client. The web page access code shown here was copied from this article.
Tip: Always gracefully shutdown your executor service, as its thread pool may continue running indefinitely even after your app ends.
package work.basil.example;
import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class PageFetch
{
public static void main ( String[] args )
{
PageFetch app = new PageFetch();
app.demo();
}
private void demo ( )
{
Runnable pageFetchRunnable = ( ) -> { this.fetchPage(); };
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate( pageFetchRunnable , 1 , 5 , TimeUnit.SECONDS ); // Wait one second, then every five seconds.
try
{
Thread.sleep( Duration.ofSeconds( 30 ).toMillis() ); // Let the executor service do its thing for a half-minute, then shut it down.
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
finally
{
scheduledExecutorService.shutdown();
}
}
private void fetchPage ( )
{
// Example code for using `HttpClient` framework of Java 11 taken from this article:
// https://mkyong.com/java/java-11-httpclient-examples/
HttpClient httpClient = HttpClient.newBuilder()
.version( HttpClient.Version.HTTP_2 )
.followRedirects( HttpClient.Redirect.NORMAL )
.connectTimeout( Duration.ofSeconds( 20 ) )
// .proxy( ProxySelector.of(new InetSocketAddress("proxy.yourcompany.com", 80)))
// .authenticator( Authenticator.getDefault())
.build();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri( URI.create( "https://httpbin.org/get" ) )
.setHeader( "User-Agent" , "Java 11 HttpClient Bot" ) // add request header
.build();
HttpResponse < String > response = null;
try
{
System.out.println( "\n-----| Demo |-------------------------------------------" );
System.out.println( "INFO - Access attempt at " + Instant.now() );
response = httpClient.send( request , HttpResponse.BodyHandlers.ofString() );
// print response headers
HttpHeaders headers = response.headers();
headers.map().forEach( ( k , v ) -> System.out.println( k + ":" + v ) );
// print status code
System.out.println( response.statusCode() );
// print response body
System.out.println( response.body() );
}
catch ( IOException e )
{
e.printStackTrace();
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
}
}
When run:
-----| Demo |-------------------------------------------
INFO - Access attempt at 2020-11-20T21:54:37.905896Z
:status:[200]
access-control-allow-credentials:[true]
access-control-allow-origin:[*]
content-length:[242]
content-type:[application/json]
date:[Fri, 20 Nov 2020 21:54:38 GMT]
server:[gunicorn/19.9.0]
200
{
"args": {},
"headers": {
"Host": "httpbin.org",
"User-Agent": "Java 11 HttpClient Bot",
"X-Amzn-Trace-Id": "Root=1-5fb83b1e-7a6acb893aec6fb310984adb"
},
"origin": "76.22.40.96",
"url": "https://httpbin.org/get"
}
-----| Demo |-------------------------------------------
INFO - Access attempt at 2020-11-20T21:54:42.907678Z
:status:[200]
access-control-allow-credentials:[true]
access-control-allow-origin:[*]
content-length:[242]
content-type:[application/json]
date:[Fri, 20 Nov 2020 21:54:43 GMT]
server:[gunicorn/19.9.0]
200
{
"args": {},
"headers": {
"Host": "httpbin.org",
"User-Agent": "Java 11 HttpClient Bot",
"X-Amzn-Trace-Id": "Root=1-5fb83b23-3dbb566f5d58a8a367e0e528"
},
"origin": "76.22.40.96",
"url": "https://httpbin.org/get"
}
… and so on for a half-minute.