All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.kamismile.stone.rpc.api.engine.StoneAppEngine Maven / Gradle / Ivy

package com.gitee.kamismile.stone.rpc.api.engine;

import com.gitee.kamismile.stone.rpc.api.IProtocol;
import com.gitee.kamismile.stone.rpc.api.invocation.support.StoneInvocation;
import com.gitee.kamismile.stone.rpc.common.engine.StoneAppEnum;
import com.gitee.kamismile.stone.rpc.common.errorenum.RpcErrorEnum;
import com.gitee.kamismile.stone.rpc.common.exception.RPCException;
import com.gitee.kamismile.stone.rpc.common.properties.StoneRpcConfigProperties;
import com.gitee.kamismile.stone.rpc.common.support.StoneURL;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;


/**
 * 容器启动初始化
 */
public class StoneAppEngine {
    protected final Logger logger = LogManager.getLogger(StoneAppEngine.class);

    private StoneRpcConfigProperties stoneRpcConfigProperties;

    private IProtocol iProtocol;

    public StoneAppEngine(StoneRpcConfigProperties stoneRpcConfigProperties, IProtocol iProtocol) {
        this.iProtocol = iProtocol;
        this.stoneRpcConfigProperties = stoneRpcConfigProperties;
    }

    private StoneAppEnum stoneAppEnum = StoneAppEnum.DOWN;


    public StoneAppEnum getStoneAppEnum() {
        return stoneAppEnum;
    }


    public void ensureStatusUp() {
        StoneAppEnum currentStatus = getStoneAppEnum();
        if (!StoneAppEnum.UP.equals(currentStatus)) {
            String message =
                    "The request is rejected. Cannot process the request due to STATUS = " + currentStatus;
            logger.warn(message);
            throw new RPCException(RpcErrorEnum.DOWNSYSTEM.getCode(),RpcErrorEnum.DOWNSYSTEM.getMessage());
        }
    }


    public void start() {
        stoneAppEnum = StoneAppEnum.STARTING;
        StoneInvocation invocation = new StoneInvocation();
        StoneURL stoneURL = new StoneURL();
        stoneURL.setHost(stoneRpcConfigProperties.getHost());
        stoneURL.setPort(stoneRpcConfigProperties.getPort());
        stoneURL.getParameters().putAll(stoneRpcConfigProperties.getUserAttributes());
        invocation.setStoneURL(stoneURL);
        Runnable runnable = iProtocol.export(invocation);
        if (Objects.isNull(runnable)) {
            stoneAppEnum = StoneAppEnum.DOWN;
            return;
        }
        iProtocol.start(runnable);
        stoneAppEnum = StoneAppEnum.UP;
    }


    /**
     * 是否是消费端
     * @return
     */
    public boolean isServer(){
        if (Objects.isNull(stoneRpcConfigProperties.getHost())){
            return false;
        }
        return true;
    }


    /**
     * 优雅关机 调用销毁接口
     */
    public void destroy() {
        stoneAppEnum = StoneAppEnum.STOPPING;
        iProtocol.destroy();
        stoneAppEnum = StoneAppEnum.DOWN;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy