diff --git a/org.ektorp.spring/src/main/java/org/ektorp/spring/HttpClientFactoryBean.java b/org.ektorp.spring/src/main/java/org/ektorp/spring/HttpClientFactoryBean.java index 14693588..699d185a 100644 --- a/org.ektorp.spring/src/main/java/org/ektorp/spring/HttpClientFactoryBean.java +++ b/org.ektorp.spring/src/main/java/org/ektorp/spring/HttpClientFactoryBean.java @@ -12,7 +12,9 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; /** * FactoryBean that produces a HttpClient. * Configuration parameters are set through @Value annotations. @@ -24,10 +26,12 @@ * @author henrik lundgren * */ -public class HttpClientFactoryBean implements FactoryBean { +public class HttpClientFactoryBean implements FactoryBean, InitializingBean, DisposableBean { private final static Logger LOG = LoggerFactory.getLogger(HttpClientFactoryBean.class); + protected HttpClient client; + public String url; public String host = "localhost"; public int port = 5984; @@ -49,48 +53,8 @@ public class HttpClientFactoryBean implements FactoryBean { private Properties couchDBProperties; + @Override public HttpClient getObject() throws Exception { - if (couchDBProperties != null) { - new DirectFieldAccessor(this).setPropertyValues(couchDBProperties); - } - - LOG.debug("host: {}", host); - LOG.debug("port: {}", port); - LOG.debug("url: {}", url); - LOG.debug("maxConnections: {}", maxConnections); - LOG.debug("connectionTimeout: {}", connectionTimeout); - LOG.debug("socketTimeout: {}", socketTimeout); - LOG.debug("autoUpdateViewOnChange: {}", autoUpdateViewOnChange); - LOG.debug("testConnectionAtStartup: {}", testConnectionAtStartup); - LOG.debug("cleanupIdleConnections: {}", cleanupIdleConnections); - LOG.debug("enableSSL: {}", enableSSL); - LOG.debug("relaxedSSLSettings: {}", relaxedSSLSettings); - - HttpClient client = new StdHttpClient.Builder() - .host(host) - .port(port) - .maxConnections(maxConnections) - .connectionTimeout(connectionTimeout) - .socketTimeout(socketTimeout) - .username(username) - .password(password) - .cleanupIdleConnections(cleanupIdleConnections) - .enableSSL(enableSSL) - .relaxedSSLSettings(relaxedSSLSettings) - .sslSocketFactory(sslSocketFactory) - .caching(caching) - .maxCacheEntries(maxCacheEntries) - .maxObjectSizeBytes(maxObjectSizeBytes) - .url(url) - .build(); - - if (testConnectionAtStartup) { - testConnect(client); - } - - - - configureAutoUpdateViewOnChange(); return client; } @@ -109,10 +73,12 @@ private void testConnect(HttpClient client) { } } + @Override public Class getObjectType() { return HttpClient.class; } + @Override public boolean isSingleton() { return true; } @@ -188,5 +154,63 @@ public void setUrl(String url) { public void setProperties(Properties p) { this.couchDBProperties = p; } + + /** + * Shutdown the HTTP Client when destroying the bean + */ + @Override + public void destroy() throws Exception { + LOG.info("Stopping couchDb connector..."); + if (client != null) { + client.shutdown(); + } + } + + /** + * Create the couchDB connection when starting the bean factory + */ + @Override + public void afterPropertiesSet() throws Exception { + LOG.info("Starting couchDb connector on {}:{}...", new Object[]{host,port}); + if (couchDBProperties != null) { + new DirectFieldAccessor(this).setPropertyValues(couchDBProperties); + } + + LOG.debug("host: {}", host); + LOG.debug("port: {}", port); + LOG.debug("url: {}", url); + LOG.debug("maxConnections: {}", maxConnections); + LOG.debug("connectionTimeout: {}", connectionTimeout); + LOG.debug("socketTimeout: {}", socketTimeout); + LOG.debug("autoUpdateViewOnChange: {}", autoUpdateViewOnChange); + LOG.debug("testConnectionAtStartup: {}", testConnectionAtStartup); + LOG.debug("cleanupIdleConnections: {}", cleanupIdleConnections); + LOG.debug("enableSSL: {}", enableSSL); + LOG.debug("relaxedSSLSettings: {}", relaxedSSLSettings); + + client = new StdHttpClient.Builder() + .host(host) + .port(port) + .maxConnections(maxConnections) + .connectionTimeout(connectionTimeout) + .socketTimeout(socketTimeout) + .username(username) + .password(password) + .cleanupIdleConnections(cleanupIdleConnections) + .enableSSL(enableSSL) + .relaxedSSLSettings(relaxedSSLSettings) + .sslSocketFactory(sslSocketFactory) + .caching(caching) + .maxCacheEntries(maxCacheEntries) + .maxObjectSizeBytes(maxObjectSizeBytes) + .url(url) + .build(); + + if (testConnectionAtStartup) { + testConnect(client); + } + + configureAutoUpdateViewOnChange(); + } } diff --git a/org.ektorp.spring/src/test/java/org/ektorp/spring/xml/CouchDBNamespaceTest.java b/org.ektorp.spring/src/test/java/org/ektorp/spring/xml/CouchDBNamespaceTest.java index 6cc1e279..0c3fbcd5 100644 --- a/org.ektorp.spring/src/test/java/org/ektorp/spring/xml/CouchDBNamespaceTest.java +++ b/org.ektorp.spring/src/test/java/org/ektorp/spring/xml/CouchDBNamespaceTest.java @@ -4,7 +4,7 @@ import org.ektorp.CouchDbConnector; import org.junit.Test; -import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -12,17 +12,19 @@ public class CouchDBNamespaceTest { @Test public void test_single_connector() { - ApplicationContext ctx = new ClassPathXmlApplicationContext("org/ektorp/spring/xml/couchdb-namespace-test-context.xml");; + ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("org/ektorp/spring/xml/couchdb-namespace-test-context.xml");; CouchDbConnector db = ctx.getBean("testDatabase", CouchDbConnector.class); assertNotNull(db); + ctx.close(); } @Test public void test_dbInstance() throws Exception { try { - ApplicationContext ctx = new ClassPathXmlApplicationContext("org/ektorp/spring/xml/couchdb-instance-namespace-test-context.xml");; + ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("org/ektorp/spring/xml/couchdb-instance-namespace-test-context.xml");; CouchDbConnector db = ctx.getBean("testDatabase", CouchDbConnector.class); assertNotNull(db); + ctx.close(); } catch (Exception e) { e.printStackTrace(); throw e;