Skip to content

Commit

Permalink
Make NdwClient implement AutoCloseable
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Dec 3, 2024
1 parent eb07603 commit e64a47e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
import java.util.Map;
import java.util.Objects;

public final class NdwClient {
public final class NdwClient implements AutoCloseable {

private static final Logger LOG = LoggerFactory.getLogger(NdwClient.class);
private static final String USER_AGENT = "github.com/bertrik/verkeersdrukte";

private final OkHttpClient httpClient;
private final INdwApi restApi;

NdwClient(INdwApi restApi) {
NdwClient(OkHttpClient httpClient, INdwApi restApi) {
this.httpClient = Objects.requireNonNull(httpClient);
this.restApi = Objects.requireNonNull(restApi);
}

Expand All @@ -34,7 +36,13 @@ public static NdwClient create(NdwConfig config) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(config.getUrl())
.addConverterFactory(ScalarsConverterFactory.create()).client(client).build();
INdwApi restApi = retrofit.create(INdwApi.class);
return new NdwClient(restApi);
return new NdwClient(client, restApi);
}

@Override
public void close() {
httpClient.dispatcher().executorService().shutdown();
httpClient.connectionPool().evictAll();
}

private static okhttp3.Response addUserAgent(Interceptor.Chain chain) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private void schedule(Runnable action, Duration delay) {

@Override
public void stop() {
ndwClient.close();
executor.shutdownNow();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ public final class RunNdwClient {
*/
public static void main(String[] args) throws IOException {
NdwConfig config = new NdwConfig();
NdwClient client = NdwClient.create(config);
FileResponse response = client.getShapeFile("");
byte[] contents = response.getContents();
String etag = response.getEtag();
LOG.info("Got file, {} bytes, etag: {}", contents.length, etag);
File file = new File(INdwApi.TRAFFIC_SPEED_SHAPEFILE);
Files.write(file.toPath(), contents);
try (NdwClient client = NdwClient.create(config)) {
FileResponse response = client.getShapeFile("");
byte[] contents = response.getContents();
String etag = response.getEtag();
LOG.info("Got file, {} bytes, etag: {}", contents.length, etag);
File file = new File(INdwApi.TRAFFIC_SPEED_SHAPEFILE);
Files.write(file.toPath(), contents);

// get again, expect HTTP code xxx
FileResponse nextResponse = client.getShapeFile(etag);
LOG.info("Next response: {}", nextResponse);
// get again, expect HTTP code xxx
FileResponse nextResponse = client.getShapeFile(etag);
LOG.info("Next response: {}", nextResponse);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ public final class RunShapeFileDownloader {

public static void main(String[] args) throws IOException {
NdwConfig ndwConfig = new NdwConfig();
NdwClient ndwClient = NdwClient.create(ndwConfig);
File folder = new File("shapefile");
folder.mkdir();
ShapeFileDownloader downloader = new ShapeFileDownloader(folder, ndwClient);
if (downloader.download()) {
FeatureCollection featureCollection = downloader.getFeatureCollection();
try (FileOutputStream fos = new FileOutputStream("shapefile.json")) {
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(fos, featureCollection);
try (NdwClient ndwClient = NdwClient.create(ndwConfig)) {
File folder = new File("shapefile");
folder.mkdir();
ShapeFileDownloader downloader = new ShapeFileDownloader(folder, ndwClient);
if (downloader.download()) {
FeatureCollection featureCollection = downloader.getFeatureCollection();
try (FileOutputStream fos = new FileOutputStream("shapefile.json")) {
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(fos, featureCollection);
}
}
}
}
Expand Down

0 comments on commit e64a47e

Please sign in to comment.