Skip to content

Instantly share code, notes, and snippets.

@dougkeen
Created October 23, 2012 19:47
Show Gist options
  • Save dougkeen/3941117 to your computer and use it in GitHub Desktop.
Save dougkeen/3941117 to your computer and use it in GitHub Desktop.
AOP connection stuff
package com.agora.advantage;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.advantagecs.connection.WebServerConnection;
@Component
public class AdvantageConnectionPool {
private static Logger log = Logger.getLogger(AdvantageConnectionPool.class);
protected GenericObjectPool<WebServerConnection> pool;
protected String username;
protected String password;
protected String host;
protected int port;
@Autowired
public AdvantageConnectionPool(
@Value("#{mwProperties['advantage.host']}") String host,
@Value("#{mwProperties['advantage.port']}") int port,
@Value("#{mwProperties['advantage.username']}") String username,
@Value("#{mwProperties['advantage.password']}") String password,
@Value("#{mwProperties['advantage.max_connections']}") int maxConnections,
@Value("#{mwProperties['advantage.max_connection_blocking_time_millis']}") long maxWait) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
pool = new GenericObjectPool<WebServerConnection>(
new PoolableConnectionFactory());
pool.setMaxActive(maxConnections);
pool.setTestOnBorrow(true);
pool.setMaxWait(maxWait);
pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
}
protected AdvantageConnectionPool(
GenericObjectPool<WebServerConnection> pool) {
this.pool = pool;
}
public WebServerConnection borrowConnection() throws Exception {
return pool.borrowObject();
}
public void returnConnection(WebServerConnection connection)
throws Exception {
pool.returnObject(connection);
}
private final class PoolableConnectionFactory implements
PoolableObjectFactory<WebServerConnection> {
@Override
public void destroyObject(WebServerConnection connection)
throws Exception {
connection.close();
}
@Override
public WebServerConnection makeObject() throws Exception {
WebServerConnection webServerConnection;
if (username != null) {
webServerConnection = new WebServerConnection(username,
password);
} else {
webServerConnection = new WebServerConnection();
}
webServerConnection.connect(host, port);
return webServerConnection;
}
@Override
public void activateObject(WebServerConnection connection)
throws Exception {
// Nothing to do
}
@Override
public void passivateObject(WebServerConnection connection)
throws Exception {
// Nothing to do
}
@Override
public boolean validateObject(WebServerConnection connection) {
try {
if (!connection.ping()) {
destroyObject(connection);
return false;
}
} catch (Exception e) {
log.error("Advantage connection ping failed", e);
}
return true;
}
}
}
package com.agora.advantage;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.advantagecs.connection.WebServerConnection;
@Aspect
@Component
public class AdvantageManagedConnectionAdvice {
private final ThreadLocal<WebServerConnection> threadLocalConnection = new ThreadLocal<WebServerConnection>();
@Autowired
private AdvantageConnectionPool connectionPool;
@Pointcut("execution(public * com.agora..*.*(..)) && target(com.agora.advantage.AdvantageService)")
private void advantageServiceOperation() {
};
@Around("advantageServiceOperation()")
public Object injectManagedAdvantageConnection(ProceedingJoinPoint joinPoint)
throws Throwable {
final AdvantageService service = (AdvantageService) joinPoint.getTarget();
boolean outermostInvocation = false;
if (threadLocalConnection.get() == null) {
threadLocalConnection.set(connectionPool.borrowConnection());
outermostInvocation = true;
}
final WebServerConnection connection = threadLocalConnection.get();
service.setManagedAdvantageConnection(connection);
try {
return joinPoint.proceed();
} finally {
service.setManagedAdvantageConnection(null);
if (outermostInvocation) {
connectionPool.returnConnection(connection);
threadLocalConnection.set(null);
}
}
}
}
package com.agora.advantage;
import com.advantagecs.connection.WebServerConnection;
public interface AdvantageService {
WebServerConnection getManagedAdvantageConnection();
void setManagedAdvantageConnection(WebServerConnection connection);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment