-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from stelin/1.x
🎨fixed style
- Loading branch information
Showing
16 changed files
with
138 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package io.openjob.server.autoconfigure; | ||
|
||
import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
import io.openjob.server.httpclient.ApacheHttpClientConnectionManagerFactory; | ||
import io.openjob.server.httpclient.ApacheHttpClientFactory; | ||
import io.openjob.server.httpclient.DefaultApacheHttpClientConnectionManagerFactory; | ||
|
@@ -8,14 +9,17 @@ | |
import org.apache.http.conn.HttpClientConnectionManager; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.annotation.PreDestroy; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author stelin [email protected] | ||
|
@@ -24,10 +28,18 @@ | |
@Configuration | ||
@EnableConfigurationProperties(value = {HttpClientProperties.class}) | ||
public class HttpClientAutoConfiguration { | ||
private final Timer connectionManagerTimer = new Timer( | ||
"HttpClientAutoConfiguration.connectionManagerTimer", true); | ||
private final ScheduledExecutorService scheduledService; | ||
private CloseableHttpClient httpClient; | ||
|
||
@Autowired | ||
public HttpClientAutoConfiguration() { | ||
this.scheduledService = new ScheduledThreadPoolExecutor( | ||
1, | ||
new ThreadFactoryBuilder().setNameFormat("Openjob-http-client-manager").build(), | ||
new ThreadPoolExecutor.AbortPolicy() | ||
); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public ApacheHttpClientConnectionManagerFactory connManFactory() { | ||
|
@@ -47,6 +59,9 @@ public ApacheHttpClientFactory apacheHttpClientFactory( | |
return new DefaultApacheHttpClientFactory(builder); | ||
} | ||
|
||
/** | ||
* Connection manager | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean | ||
public HttpClientConnectionManager connectionManager( | ||
|
@@ -58,15 +73,17 @@ public HttpClientConnectionManager connectionManager( | |
httpClientProperties.getMaxConnectionsPerRoute(), | ||
httpClientProperties.getTimeToLive(), | ||
httpClientProperties.getTimeToLiveUnit()); | ||
this.connectionManagerTimer.schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
connectionManager.closeExpiredConnections(); | ||
} | ||
}, 30000, httpClientProperties.getConnectionTimerRepeat()); | ||
|
||
|
||
this.scheduledService.scheduleWithFixedDelay(connectionManager::closeExpiredConnections, 1000, | ||
httpClientProperties.getConnectionTimerRepeat(), TimeUnit.MILLISECONDS); | ||
|
||
return connectionManager; | ||
} | ||
|
||
/** | ||
* Http client | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean | ||
public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory, | ||
|
@@ -85,9 +102,12 @@ public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory, | |
return this.httpClient; | ||
} | ||
|
||
/** | ||
* Destroy | ||
*/ | ||
@PreDestroy | ||
public void destroy() throws Exception { | ||
this.connectionManagerTimer.cancel(); | ||
this.scheduledService.shutdown(); | ||
if (this.httpClient != null) { | ||
this.httpClient.close(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,8 @@ | |
* @author stelin [email protected] | ||
* @since 1.0.6 | ||
*/ | ||
public interface ApacheHttpClientConnectionManagerFactory {String HTTP_SCHEME = "http"; | ||
public interface ApacheHttpClientConnectionManagerFactory { | ||
String HTTP_SCHEME = "http"; | ||
|
||
/** | ||
* Scheme for HTTPS based communication. | ||
|
@@ -17,12 +18,13 @@ public interface ApacheHttpClientConnectionManagerFactory {String HTTP_SCHEME = | |
|
||
/** | ||
* Creates a new {@link HttpClientConnectionManager}. | ||
* @param disableSslValidation If true, SSL validation will be disabled. | ||
* @param maxTotalConnections The total number of connections. | ||
* | ||
* @param disableSslValidation If true, SSL validation will be disabled. | ||
* @param maxTotalConnections The total number of connections. | ||
* @param maxConnectionsPerRoute The total number of connections per route. | ||
* @param timeToLive The time a connection is allowed to exist. | ||
* @param timeUnit The time unit for the time-to-live value. | ||
* manager. | ||
* @param timeToLive The time a connection is allowed to exist. | ||
* @param timeUnit The time unit for the time-to-live value. | ||
* manager. | ||
* @return A new {@link HttpClientConnectionManager}. | ||
*/ | ||
HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ public interface TaskContainer { | |
|
||
/** | ||
* Stop | ||
* @param type type | ||
*/ | ||
void stop(Integer type); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,8 @@ public interface TaskMaster { | |
|
||
/** | ||
* Stop | ||
* | ||
* @param type type | ||
*/ | ||
void stop(Integer type); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters