package org.elasticsearch.plugin.helloworld;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.rest.RestModule;
public class HelloWorldPlugin extends AbstractPlugin {
final ESLogger logger = Loggers.getLogger(getClass());
@Override
public String name() {
//插件名称
return "HelloWorld";
}
@Override
public String description() {
//插件描写叙述
return "Hello World Plugin";
}
//处理模块,由于系统中有非常多种Module,所以须要对其类型进行推断
@Override
public void processModule(Module module) {
if(module instanceof RestModule) {
((RestModule)module).addRestAction(HelloWorldHandler.class);
}
if(module instanceof HelloModule) {
logger.info("############## process hello module #####################");
}
}
@Override
public Collection<Module> modules(Settings settings) {
//创建自己的模块集合
//假设没有自己定义模块,则能够返回空
HelloModule helloModule = new HelloModule();
ArrayList<Module> list = new ArrayList<>();
list.add(helloModule);
Collections.unmodifiableList(list);
return list;
}
@SuppressWarnings("rawtypes")
@Override
public Collection<Class<? extends LifecycleComponent>> services() {
//创建自己的服务类集合,服务类须要继承自LifecycleComponent。ES会自己主动创建出服务类实例,并调用其start方法
//假设没有自己定义服务类。则能够返回空
Collection<Class<?
extends LifecycleComponent>> list = new ArrayList<>();
list.add(HelloService.class);
return list;
}
}
package org.elasticsearch.plugin.helloworld;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Scopes;
public class HelloModule extends AbstractModule {
@Override
protected void configure() {
//将InjectableService接口类型绑定到InjectableServiceImpl实现类
//在须要注入InjectableService的地方,就会使用InjectableServiceImpl实例
bind(InjectableService.class).to(InjectableServiceImpl.class);
//使HelloService为单例状态
bind(HelloService.class).in(Scopes.SINGLETON);
}
}
package org.elasticsearch.plugin.helloworld;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.RestRequest.Method;
import org.elasticsearch.rest.RestResponse;
public class HelloWorldHandler extends BaseRestHandler {
//注入对象
@Inject
protected HelloWorldHandler(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
//将该Handler绑定到某訪问路径
controller.registerHandler(Method.GET, "/hello/", this);
controller.registerHandler(Method.GET, "/hello/{name}", this);
}
//处理绑定路径的请求訪问
@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
logger.debug("HelloWorldAction.handleRequest called");
final String name = request.hasParam("name") ? request.param("name") : "world";
String content = "{\"success\":true, \"message\":\"hello " +name+ "\"}";
RestResponse response = new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, content);
channel.sendResponse(response);
}
}
plugin=org.elasticsearch.plugin.helloworld.HelloWorldPlugin
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有