Skip to content

Commit

Permalink
fix: initialize logic, which may cause spring beans management problems
Browse files Browse the repository at this point in the history
  • Loading branch information
emptyOVO committed Nov 8, 2024
1 parent 6bf84a2 commit 55c7d24
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ public class DolphinScheduleEngine implements ScheduleEngine {
@Value("${default.admin.password:inlong}")
private String password;

@Value("${inlong.schedule.dolphinscheduler.url:}")
@Value("${inlong.schedule.dolphinscheduler.url}")
private String dolphinUrl;

@Value("${inlong.schedule.dolphinscheduler.token:}")
@Value("${inlong.schedule.dolphinscheduler.token}")
private String token;

private long projectCode;
private final String projectBaseUrl;
private final DolphinScheduleUtils dsUtils = new DolphinScheduleUtils();
private final Map<Long, String> scheduledProcessMap = new ConcurrentHashMap<>();
private final DolphinScheduleUtils dsUtils;
private final Map<Long, String> scheduledProcessMap;

public DolphinScheduleEngine(String host, int port, String username, String password, String dolphinUrl,
String token) {
Expand All @@ -87,7 +87,8 @@ public DolphinScheduleEngine(String host, int port, String username, String pass
this.projectBaseUrl = dolphinUrl + DS_PROJECT_URL;
try {
LOGGER.info("Dolphin Scheduler engine http client initialized");
start();
this.dsUtils = new DolphinScheduleUtils();
this.scheduledProcessMap = new ConcurrentHashMap<>();
} catch (Exception e) {
throw new DolphinScheduleException("Failed to init dolphin scheduler ", e);
}
Expand All @@ -97,7 +98,8 @@ public DolphinScheduleEngine() {
this.projectBaseUrl = dolphinUrl + DS_PROJECT_URL;
try {
LOGGER.info("Dolphin Scheduler engine http client initialized");
start();
this.dsUtils = new DolphinScheduleUtils();
this.scheduledProcessMap = new ConcurrentHashMap<>();
} catch (Exception e) {
throw new DolphinScheduleException("Failed to init dolphin scheduler ", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,55 +474,51 @@ private Map<String, String> buildPageParam(String searchVal) {
private JsonObject executeHttpRequest(String url, String method, Map<String, String> queryParams,
Map<String, String> headers) throws IOException {
// build param
if (url != null && !url.isEmpty()) {
HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(url)).newBuilder();
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();

for (Map.Entry<String, String> entry : queryParams.entrySet()) {
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
}
HttpUrl httpUrl = urlBuilder.build();
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
}
HttpUrl httpUrl = urlBuilder.build();

// build request
Request.Builder requestBuilder = new Request.Builder()
.url(httpUrl);
// build request
Request.Builder requestBuilder = new Request.Builder()
.url(httpUrl);

// add header
for (Map.Entry<String, String> entry : headers.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
RequestBody body = RequestBody.create(MediaType.parse(CONTENT_TYPE), "");
// handle request method
switch (method.toUpperCase()) {
case HTTP_POST:
requestBuilder.post(body);
break;
case HTTP_GET:
requestBuilder.get();
break;
case HTTP_PUT:
requestBuilder.put(body);
break;
case HTTP_DELETE:
requestBuilder.delete(body);
break;
default:
throw new IllegalArgumentException("Unsupported request method: " + method);
}
// add header
for (Map.Entry<String, String> entry : headers.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
RequestBody body = RequestBody.create(MediaType.parse(CONTENT_TYPE), "");
// handle request method
switch (method.toUpperCase()) {
case HTTP_POST:
requestBuilder.post(body);
break;
case HTTP_GET:
requestBuilder.get();
break;
case HTTP_PUT:
requestBuilder.put(body);
break;
case HTTP_DELETE:
requestBuilder.delete(body);
break;
default:
throw new IllegalArgumentException("Unsupported request method: " + method);
}

Request request = requestBuilder.build();
Request request = requestBuilder.build();

// get response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
String responseBody = response.body().string();
return JsonParser.parseString(responseBody).getAsJsonObject();
} else {
throw new IOException("Unexpected http response code " + response);
}
// get response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
String responseBody = response.body().string();
return JsonParser.parseString(responseBody).getAsJsonObject();
} else {
throw new IOException("Unexpected http response code " + response);
}
}
LOGGER.error("Fail to send http request, NP exception in url");
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static void initDolphinSchedulerEngine() throws Exception {

dolphinScheduleEngine = new DolphinScheduleEngine(INLONG_DS_TEST_ADDRESS, INLONG_DS_TEST_PORT,
INLONG_DS_TEST_USERNAME, INLONG_DS_TEST_PASSWORD, DS_URL, DS_TOKEN);

dolphinScheduleEngine.start();
}

@Test
Expand Down

0 comments on commit 55c7d24

Please sign in to comment.