JestClient · 第三方库 · 访问Elasticsearch

一、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();

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,095评论 19 139
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 11,311评论 0 4
  • 公元:2019年11月28日19时42分农历:二零一九年 十一月 初三日 戌时干支:己亥乙亥己巳甲戌当月节气:立冬...
    石放阅读 11,822评论 0 2

友情链接更多精彩内容