Skip to content

Commit 139e572

Browse files
committed
switch in process linking sample to use webrequests
1 parent 516354f commit 139e572

File tree

10 files changed

+141
-1360
lines changed

10 files changed

+141
-1360
lines changed

samples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ This Application shows how to in-process-linking is being sued. To run this samp
3030
- ensure you have custom service for method `startAsyncOperation` in class `com.dynatrace.oneagent.sdk.samples.inprocesslinking.InProcessLinkingApp`
3131
- run sample: `mvn exec:exec`
3232

33-
Check your Dynatrace environment for newly created services.
34-
33+
Check your Dynatrace environment for newly created services like that:
34+
![in-process-linking-service](img/in-process-linking-service.png)
60.3 KB
Loading

samples/in-process-linking/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<agent.server>https://aoz69185.dev.dynatracelabs.com:443</agent.server>
4545
<agent.tenant>aoz69185</agent.tenant>
4646
<agent.tenantToken>kp1m2K4Il2KXwKIE</agent.tenantToken>
47+
<!-- <agent.options>,debugOneAgentSdkJava=true,debugHttpNative=true</agent.options> -->
4748
<agent.options>,debugOneAgentSdkJava=true</agent.options>
4849
<agent.agentpath>-agentpath:"${agent.lib}=name=SdkSample,server=${agent.server}"${agent.options},tenant=${agent.tenant},tenantToken=${agent.tenantToken}</agent.agentpath>
4950
</properties>

samples/in-process-linking/src/main/java/com/dynatrace/oneagent/sdk/samples/inprocesslinking/AsyncDatabaseWorkItem.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

samples/in-process-linking/src/main/java/com/dynatrace/oneagent/sdk/samples/inprocesslinking/DatabaseWorkerThread.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

samples/in-process-linking/src/main/java/com/dynatrace/oneagent/sdk/samples/inprocesslinking/InProcessLinkingApp.java

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.dynatrace.oneagent.sdk.samples.inprocesslinking;
22

3+
import java.io.BufferedReader;
4+
35
/*
46
* Copyright 2018 Dynatrace LLC
57
*
@@ -17,11 +19,18 @@
1719
*/
1820

1921
import java.io.IOException;
20-
import java.sql.ResultSet;
22+
import java.io.InputStream;
23+
import java.io.InputStreamReader;
24+
import java.net.URL;
2125
import java.sql.SQLException;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Map.Entry;
2229
import java.util.concurrent.ArrayBlockingQueue;
2330
import java.util.concurrent.BlockingQueue;
2431

32+
import javax.net.ssl.HttpsURLConnection;
33+
2534
import com.dynatrace.oneagent.sdk.OneAgentSDKFactory;
2635
import 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

Comments
 (0)