【仅供内部供应商使用,不提供对外解答和培训】

Page tree

【仅供内部供应商使用,不提供对外解答和培训】

Skip to end of metadata
Go to start of metadata

 

1、服务类型接口代码

服务接口
public interface PluginServiceCreator{
	//提供启动服务的方法,具体服务类被加载的时候,会调用该方法
    public void startService();
	
	//提供销毁服务的方法,关闭设计器或者tomcat服务器,或者切换环境时会调用该方法
    public void destroyService();

	//提供从服务中获取数据的方法
    public String serviceAction(String req) throws Exception;

	//判断服务是否启动
    public boolean serviceStarted();

    //提供一个可以清除服务日志信息的接口,可以处理可能存在的日志信息文件
    public void cleanServiceLog();
}

 

2、phantom服务配置示例

phantom配置示例
<!--依赖类型可选择service-->
<!--文件类型的依赖的根目录从WebInfo开始-->
<!--如果想要开启多个服务,则在依赖中添加多个dependence节点>
<!--dependenceID : 服务ID号;dependenceType :依赖类型;dependenceDir:服务安装地址; class:具体服务类-->
<!--主意,服务是一种插件的依赖,必须寄存在插件的配置文件中-->
<dependence>
    <DependenceUnit dependenceID="plugin.phantomjs" dependenceType="service" dependenceDir="/resources/phantomjs" class="com.fr.plugin.chart.phantom.PhantomService"/>
</dependence>

 

3、phantom服务ID说明

     每个都有自己独特的ID,服务被加载时,会将ID和服务加入到Map管理起来。故而,要想使用某个服务,必须要知道该服务的ID。

3、phantom服务代码示例

phantom服务
package com.fr.plugin.chart.phantom;
import com.fr.base.FRContext;
import com.fr.plugin.chart.phantom.serverpool.PhantomServerFactory;
import com.fr.plugin.dependence.PluginServiceCreator;

public class PhantomService implements PluginServiceCreator {

 	public PhantomServerFactory phantomServerFactory = null;
    public static final String DEFAULT_EXE = "default server";
    public static final String LOG_PATH = FRContext.getCurrentEnv().getPath() + "/service";
    //配置服务器,后续可通过配置文件配置
    private String ip = "127.0.0.1";
    private int port = 8088;
    private int serverNum = 2;
    private String exe = DEFAULT_EXE;

	//启动服务
    @Override
    public void startService() {
        if (phantomServerFactory == null) {
            phantomServerFactory = new PhantomServerFactory();
            //初始化
            phantomServerFactory.init(ip, port, serverNum, exe);

            //启动服务器
            if (phantomServerFactory.hasServer()) {
                return;
            }
            phantomServerFactory.startServer();
        }
    }

    @Override
    public void destroyService() {
        if (phantomServerFactory != null) {
            phantomServerFactory.destroyServer();
        }
    }

    /**
     * 获取本地数据
     * @param req
     * @return
     * @throws Exception
     */
    @Override
    public  String serviceAction(String req) throws Exception {
        return phantomServerFactory.requestServer(req);
    }

    @Override
    public boolean serviceStarted() {
        return phantomServerFactory != null;
    }

    @Override
    public void cleanServiceLog() {
        phantomServerFactory.cleanServerLog();
    }

}

 

4、phantom服务调用示例:

调用
//获取相应服务,要想获得服务,必须要知道服务的ID
try {
	//这样就能获取服务提供回来的数据
    response = FRContext.getCurrentEnv().pluginServiceAction("plugin.phantomjs", json.toString());
} catch (Exception e) {
    response = null;
    FRContext.getLogger().info(e.getMessage());
}

 

 

  • No labels