1
1
package com .dynatrace .oneagent .sdk .samples .inprocesslinking ;
2
2
3
+ import java .io .BufferedReader ;
4
+
3
5
/*
4
6
* Copyright 2018 Dynatrace LLC
5
7
*
17
19
*/
18
20
19
21
import java .io .IOException ;
20
- import java .sql .ResultSet ;
22
+ import java .io .InputStream ;
23
+ import java .io .InputStreamReader ;
24
+ import java .net .URL ;
21
25
import java .sql .SQLException ;
26
+ import java .util .List ;
27
+ import java .util .Map ;
28
+ import java .util .Map .Entry ;
22
29
import java .util .concurrent .ArrayBlockingQueue ;
23
30
import java .util .concurrent .BlockingQueue ;
24
31
32
+ import javax .net .ssl .HttpsURLConnection ;
33
+
25
34
import com .dynatrace .oneagent .sdk .OneAgentSDKFactory ;
26
35
import com .dynatrace .oneagent .sdk .api .OneAgentSDK ;
27
36
@@ -35,8 +44,7 @@ public class InProcessLinkingApp {
35
44
36
45
private final OneAgentSDK oneAgentSdk ;
37
46
38
- private BlockingQueue <AsyncDatabaseWorkItem > blockingQueue = new ArrayBlockingQueue <AsyncDatabaseWorkItem >(5 );
39
- private NoopStatement dbStatement = new NoopStatement ();
47
+ private BlockingQueue <UrlDownloadItem > blockingQueue = new ArrayBlockingQueue <UrlDownloadItem >(5 );
40
48
41
49
private InProcessLinkingApp () {
42
50
oneAgentSdk = OneAgentSDKFactory .createInstance ();
@@ -65,12 +73,12 @@ public static void main(String args[]) {
65
73
System .out .println ("*************************************************************" );
66
74
try {
67
75
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
74
82
app .end ();
75
83
} catch (Exception e ) {
76
84
System .err .println ("in-process-linking sample failed: " + e .getMessage ());
@@ -81,23 +89,37 @@ public static void main(String args[]) {
81
89
82
90
/**
83
91
* TODO: add custom service for this method
92
+ * check for latest version and start downloading them asynchronously.
84
93
*/
85
- private void startAsyncOperation () throws IOException , ClassNotFoundException , InterruptedException , SQLException {
94
+ private String startAsyncOperation () throws IOException , ClassNotFoundException , InterruptedException , SQLException {
86
95
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 ();
91
99
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" ,
95
115
oneAgentSdk .createInProcessLink ());
96
116
blockingQueue .put (asyncWorkItem );
97
- }
117
+
118
+ return latestVersion ;
119
+ }
98
120
99
121
private void end () {
100
- blockingQueue .offer (AsyncDatabaseWorkItem .END );
122
+ blockingQueue .offer (UrlDownloadItem .END );
101
123
}
102
124
103
125
}
0 commit comments