Skip to content

Commit 3c5ffc3

Browse files
committed
refactor try-catch-blocks
1 parent 6d6dbe3 commit 3c5ffc3

File tree

8 files changed

+51
-38
lines changed

8 files changed

+51
-38
lines changed

src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationPoller.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.annotations.VisibleForTesting;
44
import java.net.URISyntaxException;
5-
import java.util.concurrent.ExecutionException;
65
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
@@ -40,23 +39,22 @@ public void run() {
4039
*/
4140
@VisibleForTesting
4241
boolean pollConfiguration() {
42+
SimpleHttpRequest request = createConfigurationRequest();
43+
44+
return HttpRequestSender.send(request, new HttpConfigurationCallback());
45+
}
46+
47+
/**
48+
* @return the created polling request
49+
*/
50+
private SimpleHttpRequest createConfigurationRequest() {
4351
SimpleHttpRequest request = null;
44-
// TODO try-catch in eigene Methode auslagern
4552
try {
4653
request = HttpConfigurationFactory.createConfigurationRequest(serverUrl);
4754
} catch (URISyntaxException e) {
4855
log.error("Error building HTTP URI for configuration polling", e);
4956
}
50-
// TODO try-catch in eigene Methode auslagern
51-
try {
52-
return HttpRequestSender.send(request, new HttpConfigurationCallback());
53-
} catch (ExecutionException e) {
54-
log.error("Error executing configuration polling", e);
55-
} catch (InterruptedException e) {
56-
log.error("Configuration polling was interrupted", e);
57-
Thread.currentThread().interrupt();
58-
}
59-
return false;
57+
return request;
6058
}
6159

6260
@Override

src/main/java/rocks/inspectit/gepard/agent/instrumentation/filling/ClassDiscoveryService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public void run() {
4141

4242
/**
4343
* Checks all loaded classes of the Instrumentation API and adds newly discovered classes to our
44-
* {@code discoveredClasses}. Additionally, the {@link PendingClassesCache} will be filled with the newly
45-
* discovered classes.
44+
* {@code discoveredClasses}. Additionally, the {@link PendingClassesCache} will be filled with
45+
* the newly discovered classes.
4646
*/
4747
void discoverClasses() {
4848
Set<Class<?>> newClasses = new HashSet<>();

src/main/java/rocks/inspectit/gepard/agent/instrumentation/filling/ConfigurationReceiver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import rocks.inspectit.gepard.agent.internal.configuration.observer.ConfigurationReceivedObserver;
1111

1212
/**
13-
* Listens to {@link ConfigurationReceivedEvent}s and updates the {@link PendingClassesCache} afterward.
13+
* Listens to {@link ConfigurationReceivedEvent}s and updates the {@link PendingClassesCache}
14+
* afterward.
1415
*/
1516
public class ConfigurationReceiver implements ConfigurationReceivedObserver {
1617
private final Logger log = LoggerFactory.getLogger(ConfigurationReceiver.class);

src/main/java/rocks/inspectit/gepard/agent/instrumentation/processing/BatchInstrumenter.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ public void run() {
4141
}
4242

4343
/**
44-
* Retrieves the next batch out of {@link PendingClassesCache}.
45-
* Currently, the batch size is fixed to 1000.
44+
* Retrieves the next batch out of {@link PendingClassesCache}. Currently, the batch size is fixed
45+
* to 1000.
4646
*
4747
* @param batchSize the size of the next batch
48-
*
4948
* @return the batch of retrieved pending classes
5049
*/
5150
@VisibleForTesting

src/main/java/rocks/inspectit/gepard/agent/internal/configuration/model/InspectitConfiguration.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import rocks.inspectit.gepard.agent.internal.configuration.model.instrumentation.InstrumentationConfiguration;
55

6-
/**
7-
* Model of an inspectit gepard configuration.
8-
*/
6+
/** Model of an inspectit gepard configuration. */
97
public class InspectitConfiguration {
108

119
public InspectitConfiguration() {

src/main/java/rocks/inspectit/gepard/agent/internal/configuration/observer/ConfigurationReceivedSubject.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import rocks.inspectit.gepard.agent.internal.configuration.model.InspectitConfiguration;
77

88
/**
9-
* Observer pattern subject, which notifies all registered listeners about {@link ConfigurationReceivedEvent}s.
9+
* Observer pattern subject, which notifies all registered listeners about {@link
10+
* ConfigurationReceivedEvent}s.
1011
*/
1112
public class ConfigurationReceivedSubject {
1213

src/main/java/rocks/inspectit/gepard/agent/internal/http/HttpRequestSender.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
99
import org.apache.hc.core5.concurrent.FutureCallback;
1010
import org.apache.hc.core5.http.HttpResponse;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
1113

1214
/** Utility class to execute HTTP requests with callbacks */
1315
public class HttpRequestSender {
16+
private static final Logger log = LoggerFactory.getLogger(HttpRequestSender.class);
1417

1518
/**
1619
* Executes the provided HTTP request as well as the callback function.
@@ -19,12 +22,27 @@ public class HttpRequestSender {
1922
* @param callback the callback function
2023
* @return True, if the HTTP request returned the status code 200
2124
*/
22-
public static boolean send(SimpleHttpRequest request, FutureCallback<SimpleHttpResponse> callback)
23-
throws ExecutionException, InterruptedException {
25+
public static boolean send(
26+
SimpleHttpRequest request, FutureCallback<SimpleHttpResponse> callback) {
27+
if (Objects.isNull(request)) return false;
28+
2429
CloseableHttpAsyncClient client = HttpClientHolder.getClient();
2530
Future<SimpleHttpResponse> future = client.execute(request, callback);
26-
HttpResponse response = future.get();
2731

32+
HttpResponse response = handleFuture(future);
2833
return Objects.nonNull(response) && 200 == response.getCode();
2934
}
35+
36+
private static HttpResponse handleFuture(Future<SimpleHttpResponse> future) {
37+
HttpResponse response = null;
38+
try {
39+
response = future.get();
40+
} catch (ExecutionException e) {
41+
log.error("Error executing HTTP request", e);
42+
} catch (InterruptedException e) {
43+
log.error("Sending of HTTP request was interrupted", e);
44+
Thread.currentThread().interrupt();
45+
}
46+
return response;
47+
}
3048
}

src/main/java/rocks/inspectit/gepard/agent/notification/StartNotifier.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import java.net.URISyntaxException;
5-
import java.util.concurrent.ExecutionException;
65
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
@@ -20,25 +19,24 @@ public class StartNotifier {
2019
* @return true, if the notification was executed successfully
2120
*/
2221
public boolean sendNotification(String serverUrl) {
22+
SimpleHttpRequest notification = createStartNotification(serverUrl);
23+
24+
return HttpRequestSender.send(notification, new NotificationCallback());
25+
}
26+
27+
/**
28+
* @param serverUrl the url of the configuration server
29+
* @return the created start notification request
30+
*/
31+
private SimpleHttpRequest createStartNotification(String serverUrl) {
2332
SimpleHttpRequest notification = null;
24-
// TODO try-catch in eigene Methode auslagern
2533
try {
2634
notification = NotificationFactory.createStartNotification(serverUrl);
2735
} catch (URISyntaxException e) {
2836
log.error("Error building HTTP URI for configuration server notification", e);
2937
} catch (JsonProcessingException e) {
3038
log.error("Could not process agent information for configuration server notification", e);
3139
}
32-
33-
// TODO try-catch in eigene Methode auslagern
34-
try {
35-
return HttpRequestSender.send(notification, new NotificationCallback());
36-
} catch (ExecutionException e) {
37-
log.error("Error executing start notification for configuration server", e);
38-
} catch (InterruptedException e) {
39-
log.error("Start notification for configuration server was interrupted", e);
40-
Thread.currentThread().interrupt();
41-
}
42-
return false;
40+
return notification;
4341
}
4442
}

0 commit comments

Comments
 (0)