Created
January 17, 2012 17:20
-
-
Save thesurlydev/1627584 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TransportClientFactory implements FactoryBean<TransportClient>, InitializingBean, DisposableBean { | |
private static final Logger log = Logger.getLogger(TransportClientFactory.class); | |
private boolean disabled; | |
private Map<String, String> settings; | |
private Map<String, Integer> transportAddresses; | |
private TransportClient client; | |
public void setTransportAddresses(final Map<String, Integer> transportAddresses) { | |
this.transportAddresses = transportAddresses; | |
} | |
public void setSettings(final Map<String, String> settings) { | |
this.settings = settings; | |
} | |
public void setDisabled(boolean disabled) { | |
this.disabled = disabled; | |
} | |
@Override public void afterPropertiesSet() throws Exception { | |
if (disabled) { | |
log.info("disabled; skipping TransportClient init"); | |
return; | |
} | |
log.info("getting client"); | |
if (this.settings != null && !this.settings.isEmpty()) { | |
Settings s = ImmutableSettings.settingsBuilder() | |
.put(this.settings) | |
.build(); | |
client = new TransportClient(s); | |
} else { | |
client = new TransportClient(); | |
} | |
if (null != transportAddresses) { | |
for (final Map.Entry<String, Integer> address : transportAddresses.entrySet()) { | |
if (log.isInfoEnabled()) { | |
log.info("Adding transport address: " + address.getKey() + " port: " + address.getValue()); | |
} | |
client.addTransportAddress(new InetSocketTransportAddress(address.getKey(), address.getValue())); | |
} | |
} | |
} | |
@Override public void destroy() throws Exception { | |
if (client != null) { | |
log.info("closing TransportClient"); | |
client.close(); | |
} | |
} | |
@Override public TransportClient getObject() throws Exception { | |
return client; | |
} | |
@Override public Class<?> getObjectType() { | |
return TransportClient.class; | |
} | |
@Override public boolean isSingleton() { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment