11package com .dynatrace .oneagent .sdk .samples .inprocesslinking ;
22
3+ import java .io .BufferedReader ;
4+
35/*
46 * Copyright 2018 Dynatrace LLC
57 *
1719 */
1820
1921import java .io .IOException ;
20- import java .sql .ResultSet ;
22+ import java .io .InputStream ;
23+ import java .io .InputStreamReader ;
24+ import java .net .URL ;
2125import java .sql .SQLException ;
26+ import java .util .List ;
27+ import java .util .Map ;
28+ import java .util .Map .Entry ;
2229import java .util .concurrent .ArrayBlockingQueue ;
2330import java .util .concurrent .BlockingQueue ;
2431
32+ import javax .net .ssl .HttpsURLConnection ;
33+
2534import com .dynatrace .oneagent .sdk .OneAgentSDKFactory ;
2635import com .dynatrace .oneagent .sdk .api .OneAgentSDK ;
2736
@@ -35,8 +44,7 @@ public class InProcessLinkingApp {
3544
3645 private final OneAgentSDK oneAgentSdk ;
3746
38- private BlockingQueue <AsyncDatabaseWorkItem > blockingQueue = new ArrayBlockingQueue <AsyncDatabaseWorkItem >(5 );
39- private NoopStatement dbStatement = new NoopStatement ();
47+ private BlockingQueue <UrlDownloadItem > blockingQueue = new ArrayBlockingQueue <UrlDownloadItem >(5 );
4048
4149 private InProcessLinkingApp () {
4250 oneAgentSdk = OneAgentSDKFactory .createInstance ();
@@ -65,12 +73,12 @@ public static void main(String args[]) {
6573 System .out .println ("*************************************************************" );
6674 try {
6775 InProcessLinkingApp app = new InProcessLinkingApp ();
68- new DatabaseWorkerThread ( app . blockingQueue ). start ();
69- app .startAsyncOperation ();
70- // System.out.println("remote call server stopped. sleeping a while, so OneAgent
71- // is able to send data to server ..." );
72- Thread . sleep ( 15000 ); // we have to wait - so OneAgent is able to send data to
73- // server.
76+ // start worker thread, responsible for downloading files in background. thread waits on provided blocking queue.
77+ new UrlDownloaderThread ( app .blockingQueue ). start ();
78+ String latestVersion = app . startAsyncOperation ();
79+ System . err . println ( "Found latest OneAgent SDK for Java: " + latestVersion );
80+ System . out . println ( "sample application stopped. sleeping a while, so OneAgent is able to send data to server ..." );
81+ Thread . sleep ( 15000 ); // we have to wait - so OneAgent is able to send data to server
7482 app .end ();
7583 } catch (Exception e ) {
7684 System .err .println ("in-process-linking sample failed: " + e .getMessage ());
@@ -81,23 +89,37 @@ public static void main(String args[]) {
8189
8290 /**
8391 * TODO: add custom service for this method
92+ * check for latest version and start downloading them asynchronously.
8493 */
85- private void startAsyncOperation () throws IOException , ClassNotFoundException , InterruptedException , SQLException {
94+ private String startAsyncOperation () throws IOException , ClassNotFoundException , InterruptedException , SQLException {
8695 System .out .println ("Executor started ..." );
87- // process synchronous db work:
88- ResultSet bookingRs = dbStatement .executeQuery ("Select * from bookings where bookingId = 'AB01022'" );
89- // do some sync work ...
90- bookingRs .close ();
96+
97+ // do some sync work - eg. check for latest version:
98+ HttpsURLConnection url = (HttpsURLConnection ) new URL ("https://github.com/Dynatrace/OneAgent-SDK-for-Java/releases/latest" ).openConnection ();
9199
92- // process DB execution, where no result is needed async in background:
93- AsyncDatabaseWorkItem asyncWorkItem = new AsyncDatabaseWorkItem (
94- "update bookings set paymentState='FINISHED' where bookingId = 'AB01022'" ,
100+ // we expect redirect to latest release url:
101+ url .setInstanceFollowRedirects (false );
102+ int responseStatus = url .getResponseCode ();
103+ if (responseStatus != 302 ) {
104+ System .out .println ("no redirect retrieved!" );
105+ return null ;
106+ }
107+
108+ // e. g.: https://github.com/Dynatrace/OneAgent-SDK-for-Java/releases/tag/v1.0.3
109+ String location = url .getHeaderField ("Location" );
110+ String latestVersion = location .substring (location .lastIndexOf ('/' )+1 );
111+
112+ // download the big release archive asynchronously ...
113+ UrlDownloadItem asyncWorkItem = new UrlDownloadItem (
114+ "https://github.com/Dynatrace/OneAgent-SDK-for-Java/archive/" + latestVersion + ".zip" ,
95115 oneAgentSdk .createInProcessLink ());
96116 blockingQueue .put (asyncWorkItem );
97- }
117+
118+ return latestVersion ;
119+ }
98120
99121 private void end () {
100- blockingQueue .offer (AsyncDatabaseWorkItem .END );
122+ blockingQueue .offer (UrlDownloadItem .END );
101123 }
102124
103125}
0 commit comments