Skip to content

Commit

Permalink
When using a Spring factory, it could be better to use InitializingBean
Browse files Browse the repository at this point in the history
and DisposableBean interfaces to create and destroy bean. BTW, when
destroying a bean, we must shutdown http connector.
  • Loading branch information
dadoonet committed Feb 23, 2012
1 parent 1a443d1 commit c49cbe2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 45 deletions.
Expand Up @@ -12,7 +12,9 @@
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
/** /**
* FactoryBean that produces a HttpClient. * FactoryBean that produces a HttpClient.
* Configuration parameters are set through @Value annotations. * Configuration parameters are set through @Value annotations.
Expand All @@ -24,10 +26,12 @@
* @author henrik lundgren * @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); private final static Logger LOG = LoggerFactory.getLogger(HttpClientFactoryBean.class);


protected HttpClient client;

public String url; public String url;
public String host = "localhost"; public String host = "localhost";
public int port = 5984; public int port = 5984;
Expand All @@ -49,48 +53,8 @@ public class HttpClientFactoryBean implements FactoryBean<HttpClient> {


private Properties couchDBProperties; private Properties couchDBProperties;


@Override
public HttpClient getObject() throws Exception { 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; return client;
} }


Expand All @@ -109,10 +73,12 @@ private void testConnect(HttpClient client) {
} }
} }


@Override
public Class<? extends HttpClient> getObjectType() { public Class<? extends HttpClient> getObjectType() {
return HttpClient.class; return HttpClient.class;
} }


@Override
public boolean isSingleton() { public boolean isSingleton() {
return true; return true;
} }
Expand Down Expand Up @@ -188,5 +154,63 @@ public void setUrl(String url) {
public void setProperties(Properties p) { public void setProperties(Properties p) {
this.couchDBProperties = 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();
}


} }
Expand Up @@ -4,25 +4,27 @@


import org.ektorp.CouchDbConnector; import org.ektorp.CouchDbConnector;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;




public class CouchDBNamespaceTest { public class CouchDBNamespaceTest {


@Test @Test
public void test_single_connector() { 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); CouchDbConnector db = ctx.getBean("testDatabase", CouchDbConnector.class);
assertNotNull(db); assertNotNull(db);
ctx.close();
} }


@Test @Test
public void test_dbInstance() throws Exception { public void test_dbInstance() throws Exception {
try { 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); CouchDbConnector db = ctx.getBean("testDatabase", CouchDbConnector.class);
assertNotNull(db); assertNotNull(db);
ctx.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw e; throw e;
Expand Down

0 comments on commit c49cbe2

Please sign in to comment.