Permalink
Browse files

When using a Spring factory, it could be better to use InitializingBean

and DisposableBean interfaces to create and destroy bean. BTW, when
destroying a bean, we must shutdown http connector.
  • Loading branch information...
1 parent 1a443d1 commit c49cbe2f50ea9340ea2b5be754cbfbfd8be48b2e @dadoonet dadoonet committed Feb 23, 2012
@@ -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<HttpClient> {
+public class HttpClientFactoryBean implements FactoryBean<HttpClient>, 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 @@
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<? extends HttpClient> 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();
+ }
}
@@ -4,25 +4,27 @@
import org.ektorp.CouchDbConnector;
import org.junit.Test;
-import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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;

0 comments on commit c49cbe2

Please sign in to comment.