Skip to content

Commit

Permalink
Revert "Check used connections every 30 seconds"
Browse files Browse the repository at this point in the history
This reverts commit a15eaa7.
  • Loading branch information
tomas-sexenian committed Oct 27, 2023
1 parent 2a396b9 commit 5d8ba14
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,15 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CustomPoolingHttpClientConnectionManager extends PoolingHttpClientConnectionManager {
private final List<IConnectionObserver> observers = new ArrayList<>();

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

HashSet<HttpRoute> activeRoutes = new HashSet<>(this.getRoutes());

public CustomPoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry){
super(socketFactoryRegistry);
initializePeriodicPoolCheck();
}

private void initializePeriodicPoolCheck() {
Runnable task = () -> periodicPoolCheck();
scheduler.scheduleAtFixedRate(task, 0, 30, TimeUnit.SECONDS);
}

private void periodicPoolCheck() {
if (activeRoutes.size() > this.getRoutes().size()){
activeRoutes.removeAll(this.getRoutes());
for (HttpRoute route : activeRoutes)
notifyConnectionDestroyed(route);
}
activeRoutes = new HashSet<>(this.getRoutes());
}

public void addObserver(IConnectionObserver observer) {
Expand All @@ -60,16 +40,51 @@ public boolean cancel() {
public HttpClientConnection get(long timeout, TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
HttpClientConnection connection = originalRequest.get(timeout, tunit);

if (connection != null && !connection.isOpen()){
if (connection != null && !connection.isOpen())
notifyConnectionCreated(route);
activeRoutes.add(route);
}

return connection;
}
};
}

@Override
public void closeExpiredConnections() {
Set<HttpRoute> beforeClosing = new HashSet<>();
Set<HttpRoute> afterClosing = new HashSet<>();

super.enumAvailable(entry -> {
if (entry.isExpired(System.currentTimeMillis())) {
beforeClosing.add(entry.getRoute());
}
});
super.closeExpiredConnections();
super.enumAvailable(entry -> afterClosing.add(entry.getRoute()));
beforeClosing.removeAll(afterClosing);

for (HttpRoute route : beforeClosing)
notifyConnectionDestroyed(route);
}

@Override
public void closeIdleConnections(long idletime, TimeUnit tunit) {
Set<HttpRoute> beforeClosing = new HashSet<>();
Set<HttpRoute> afterClosing = new HashSet<>();
long idleTimeoutMillis = tunit.toMillis(idletime);

super.enumAvailable(entry -> {
if (entry.getUpdated() + idleTimeoutMillis < System.currentTimeMillis()) {
beforeClosing.add(entry.getRoute());
}
});
super.closeIdleConnections(idletime, tunit);
super.enumAvailable(entry -> afterClosing.add(entry.getRoute()));
beforeClosing.removeAll(afterClosing);

for (HttpRoute route : beforeClosing)
notifyConnectionDestroyed(route);
}

private void notifyConnectionCreated(HttpRoute route) {
for (IConnectionObserver observer : observers)
observer.onConnectionCreated(route);
Expand Down
21 changes: 9 additions & 12 deletions java/src/main/java/com/genexus/internet/HttpClientJavaLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.*;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
Expand All @@ -12,6 +11,7 @@
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.*;
import java.net.URI;
import javax.net.ssl.SSLContext;
import org.apache.http.*;
import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -99,16 +99,6 @@ public void onConnectionDestroyed(HttpRoute route) {
HTTPConnectionJMX.DestroyHTTPConnectionJMX(route);
}

@Override
protected void finalize() {
this.closeOpenedStreams();
if (Application.isJMXEnabled()){
for (HttpRoute route : connManager.getRoutes())
HTTPConnectionJMX.DestroyHTTPConnectionJMX(route);
HTTPPoolJMX.DestroyHTTPPoolJMX(connManager);
}
}

private ConnectionKeepAliveStrategy generateKeepAliveStrategy() {
return new ConnectionKeepAliveStrategy() {
@Override
Expand Down Expand Up @@ -155,6 +145,7 @@ public void setTimeout(int timeout)
private static IniFile clientCfg = new ModelContext(ModelContext.getModelContextPackageClass()).getPreferences().getIniFile();
private static final String SET_COOKIE = "Set-Cookie";
private static final String COOKIE = "Cookie";

private java.util.Vector<InputStream> streamsToClose;

private void closeOpenedStreams()
Expand Down Expand Up @@ -312,7 +303,7 @@ private CookieStore setAllStoredCookies() {
CookieStore cookiesToSend = new BasicCookieStore();
if (!ModelContext.getModelContext().isNullHttpContext()) { // Caso de ejecucion de varias instancia de HttpClientJavaLib, por lo que se obtienen cookies desde sesion web del browser

String selfWebCookie = ((HttpContextWeb) ModelContext.getModelContext().getHttpContext()).getCookie("Set-Cookie");
String selfWebCookie = ((HttpContextWeb) ModelContext.getModelContext().getHttpContext()).getCookie(SET_COOKIE);
if (!selfWebCookie.isEmpty())
this.addHeader(COOKIE, selfWebCookie.replace("+",";"));

Expand Down Expand Up @@ -765,4 +756,10 @@ public void cleanup() {
resetErrorsAndConnParams();
}

@Override
protected void finalize()
{
this.closeOpenedStreams();
}

}
11 changes: 1 addition & 10 deletions java/src/main/java/com/genexus/management/HTTPPoolJMX.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,8 @@ static public void CreateHTTPPoolJMX(PoolingHttpClientConnectionManager httpConn
}
}

static public void DestroyHTTPPoolJMX(PoolingHttpClientConnectionManager httpConnectionPool) {
try {
MBeanUtils.destroyMBean(httpConnectionPool);
}
catch(Exception e) {
log.error("Cannot register HTTP connection pool MBean.", e);
}
}

public int getNumberOfConnectionsInUse(){
return connectionPool.getTotalStats().getLeased();
return connectionPool.getTotalStats().getAvailable();
}

public int getNumberOfRequestsWaiting(){
Expand Down
27 changes: 3 additions & 24 deletions java/src/main/java/com/genexus/management/MBeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
public class MBeanUtils {

private static MBeanServer mbs = null;
private static Vector<ObjectName> registeredObjects = new Vector<>();
private static Vector<ObjectName> registeredObjects = new Vector<ObjectName>();

public MBeanUtils() {}
public MBeanUtils() {
}

private static MBeanServer getMBeanServer()
{
Expand Down Expand Up @@ -280,28 +281,6 @@ public static void destroyMBean(HttpRoute httpRoute) {
System.out.println(e);
}
}

public static void destroyMBean(PoolingHttpClientConnectionManager httpConnectionPool) {
MBeanServer mbs = getMBeanServer();
if (mbs == null)
return;

try {
ObjectName name = new ObjectName("com.genexus.management:type=GeneXusApplicationServer.HTTPPool,ApplicationName=Http connection pool");
registeredObjects.removeElement(name);

mbs.unregisterMBean(name);
}
catch(javax.management.MalformedObjectNameException e) {
System.out.println(e);
}
catch(javax.management.InstanceNotFoundException e) {
System.out.println(e);
}
catch(javax.management.MBeanRegistrationException e) {
System.out.println(e);
}
}

public static void destroyMBeanCache()
{
Expand Down

0 comments on commit 5d8ba14

Please sign in to comment.