public class TransportClientFactory implements FactoryBean<TransportClient>,InitializingBean,DisposableBean {
private String clusterName;
private String host;
private String xpacksecurityuser;
private int port;
private TransportClient client;
@Override
public void afterPropertiesSet() throws Exception {
Settings settings = Settings.builder()
.put("cluster.name", clusterName)
.put("xpack.security.user", xpacksecurityuser)
.put("client.transport.sniff", false)
.build();
client=new PreBuiltXPackTransportClient(settings).addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName(host),port));
}
@Override
public TransportClient getObject() throws Exception {
return client;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public void setHost(String host) {
this.host = host;
}
public void setXpacksecurityuser(String xpacksecurityuser) {
this.xpacksecurityuser = xpacksecurityuser;
}
public void setPort(int port) {
this.port = port;
}
@Override
public void destroy() throws Exception {
if(client!=null)
client.close();
}
@Override
public Class<?> getObjectType() {
return TransportClient.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
来源:https://www.cnblogs.com/xietianhua/p/11361864.html