一、JestClient管理
package org.xwiki.activeinstalls.internal;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.xwiki.activeinstalls.ActiveInstallsConfiguration;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.phase.Disposable;
import org.xwiki.component.phase.Initializable;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
public class DefaultJestClientManagerimplements JestClientManager, Initializable, Disposable
{
@Inject
private Loggerlogger;
@Inject
private ActiveInstallsConfigurationconfiguration;
/**
* The Jest Client singleton instance to use to connect to the remote instance.
*/
private JestClientclient;
@Override
public void initialize()
{
String pingURL =this.configuration.getPingInstanceURL();
if (!StringUtils.isEmpty(pingURL)) {
HttpClientConfig clientConfig =new HttpClientConfig.Builder(pingURL).multiThreaded(true).build();
JestClientFactory factory =new XWikiJestClientFactory(this.configuration);
factory.setHttpClientConfig(clientConfig);
this.client = factory.getObject();
}
}
@Override
public void dispose()
{
if (this.client !=null) {
this.client.shutdownClient();
}
}
@Override
public JestClient getClient()
{
return this.client;
}
}
二、应用场景
探测服务器是否工作正常,模拟一个ping动作
package org.xwiki.activeinstalls.internal.client;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.xwiki.activeinstalls.internal.JestClientManager;
import org.xwiki.component.annotation.Component;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Index;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.mapping.PutMapping;
import net.sf.json.JSONObject;
public class DefaultPingSenderimplements PingSender
{
@Inject
private JestClientManagerjestClientManager;
@Inject
private Provider>pingDataProviderProvider;
@Override
public void sendPing()throws Exception
{
// Only send a ping if an ES client is available. Note that an empty ping URL disables the feature.
JestClient client =this.jestClientManager.getClient();
if (client !=null) {
// Step 1: Create index (if already exists then it'll just be ignored)
client.execute(new CreateIndex.Builder(JestClientManager.INDEX).build());
// Step 2: Create a mapping so that we can search distribution versions containing hyphens (otherwise they
// are removed by the default tokenizer/analyzer). If mapping already exists then it'll just be ignored.
PutMapping putMapping =
new PutMapping.Builder(JestClientManager.INDEX, JestClientManager.TYPE, constructJSONMapping()).build();
client.execute(putMapping);
// Step 3: Index the data
Index index =new Index.Builder(constructIndexJSON())
.index(JestClientManager.INDEX)
.type(JestClientManager.TYPE)
.build();
JestResult result = client.execute(index);
if (!result.isSucceeded()) {
throw new Exception(result.getErrorMessage());
}
}
}
private String constructJSONMapping()
{
Map jsonMap =new HashMap<>();
Map timestampMap =new HashMap<>();
timestampMap.put("enabled",true);
timestampMap.put("store",true);
Map propertiesMap =new HashMap<>();
for (PingDataProvider pingDataProvider :this.pingDataProviderProvider.get()) {
propertiesMap.putAll(pingDataProvider.provideMapping());
}
jsonMap.put("_timestamp", timestampMap);
jsonMap.put("properties", propertiesMap);
return JSONObject.fromObject(Collections.singletonMap(JestClientManager.TYPE, jsonMap)).toString();
}
private String constructIndexJSON()
{
Map jsonMap =new HashMap<>();
for (PingDataProvider pingDataProvider :this.pingDataProviderProvider.get()) {
jsonMap.putAll(pingDataProvider.provideData());
}
return JSONObject.fromObject(jsonMap).toString();
}
}