Restore CLI startup performance
Change InitializrService to use a late binding CloseableHttpClient since the calling `HttpClientBuilder.create().build()` is slow. Fixes gh-1764
This commit is contained in:
@@ -22,7 +22,6 @@ import java.util.Arrays;
|
|||||||
import joptsimple.OptionSet;
|
import joptsimple.OptionSet;
|
||||||
import joptsimple.OptionSpec;
|
import joptsimple.OptionSpec;
|
||||||
|
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.springframework.boot.cli.command.Command;
|
import org.springframework.boot.cli.command.Command;
|
||||||
import org.springframework.boot.cli.command.OptionParsingCommand;
|
import org.springframework.boot.cli.command.OptionParsingCommand;
|
||||||
import org.springframework.boot.cli.command.options.OptionHandler;
|
import org.springframework.boot.cli.command.options.OptionHandler;
|
||||||
@@ -38,7 +37,7 @@ import org.springframework.boot.cli.util.Log;
|
|||||||
public class InitCommand extends OptionParsingCommand {
|
public class InitCommand extends OptionParsingCommand {
|
||||||
|
|
||||||
public InitCommand() {
|
public InitCommand() {
|
||||||
this(new InitOptionHandler(getInitializrService()));
|
this(new InitOptionHandler(new InitializrService()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public InitCommand(InitOptionHandler handler) {
|
public InitCommand(InitOptionHandler handler) {
|
||||||
@@ -46,10 +45,6 @@ public class InitCommand extends OptionParsingCommand {
|
|||||||
+ "Initialzr (start.spring.io)", handler);
|
+ "Initialzr (start.spring.io)", handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static InitializrService getInitializrService() {
|
|
||||||
return new InitializrService(HttpClientBuilder.create().build());
|
|
||||||
}
|
|
||||||
|
|
||||||
static class InitOptionHandler extends OptionHandler {
|
static class InitOptionHandler extends OptionHandler {
|
||||||
|
|
||||||
private final ServiceCapabilitiesReportGenerator serviceCapabilitiesReport;
|
private final ServiceCapabilitiesReportGenerator serviceCapabilitiesReport;
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import org.apache.http.client.methods.HttpGet;
|
|||||||
import org.apache.http.client.methods.HttpUriRequest;
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.apache.http.message.BasicHeader;
|
import org.apache.http.message.BasicHeader;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@@ -47,15 +48,25 @@ class InitializrService {
|
|||||||
|
|
||||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||||
|
|
||||||
private final CloseableHttpClient http;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance with the given {@link CloseableHttpClient HTTP client}.
|
* Late binding HTTP client.
|
||||||
*/
|
*/
|
||||||
public InitializrService(CloseableHttpClient http) {
|
private CloseableHttpClient http;
|
||||||
|
|
||||||
|
public InitializrService() {
|
||||||
|
}
|
||||||
|
|
||||||
|
InitializrService(CloseableHttpClient http) {
|
||||||
this.http = http;
|
this.http = http;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected CloseableHttpClient getHttp() {
|
||||||
|
if (this.http == null) {
|
||||||
|
this.http = HttpClientBuilder.create().build();
|
||||||
|
}
|
||||||
|
return this.http;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a project based on the specified {@link ProjectGenerationRequest}
|
* Generate a project based on the specified {@link ProjectGenerationRequest}
|
||||||
* @return an entity defining the project
|
* @return an entity defining the project
|
||||||
@@ -130,7 +141,7 @@ class InitializrService {
|
|||||||
private CloseableHttpResponse execute(HttpUriRequest request, Object url,
|
private CloseableHttpResponse execute(HttpUriRequest request, Object url,
|
||||||
String description) {
|
String description) {
|
||||||
try {
|
try {
|
||||||
return this.http.execute(request);
|
return getHttp().execute(request);
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
throw new ReportableException("Failed to " + description
|
throw new ReportableException("Failed to " + description
|
||||||
|
|||||||
Reference in New Issue
Block a user