ScalingContext.java 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

18
package org.apache.shardingsphere.scaling.core.config;
19

20
import com.google.common.base.Strings;
21
import lombok.AccessLevel;
22
import lombok.Getter;
23
import lombok.NoArgsConstructor;
24 25 26
import org.apache.shardingsphere.governance.core.yaml.swapper.GovernanceCenterConfigurationYamlSwapper;
import org.apache.shardingsphere.scaling.core.execute.engine.ShardingScalingExecuteEngine;
import org.apache.shardingsphere.scaling.core.spi.ElasticJobEntryLoader;
27

28
/**
kimmking's avatar
kimmking 已提交
29
 * ShardingSphere-Scaling context.
30
 */
31
@NoArgsConstructor(access = AccessLevel.PRIVATE)
32
@Getter
33
public final class ScalingContext {
34
    
35
    private static final ScalingContext INSTANCE = new ScalingContext();
36
    
37
    private ServerConfiguration serverConfiguration;
38
    
39 40 41
    private ShardingScalingExecuteEngine taskExecuteEngine;
    
    private ShardingScalingExecuteEngine importerExecuteEngine;
42
    
43
    /**
kimmking's avatar
kimmking 已提交
44
     * Get instance of ShardingSphere-Scaling's context.
45
     *
kimmking's avatar
kimmking 已提交
46
     * @return instance of ShardingSphere-Scaling's context.
47
     */
48 49 50
    public static ScalingContext getInstance() {
        return INSTANCE;
    }
51
    
52
    /**
kimmking's avatar
kimmking 已提交
53
     * Initialize ShardingSphere-Scaling context.
54
     *
55 56
     * @param serverConfiguration serverConfiguration
     */
57
    public void init(final ServerConfiguration serverConfiguration) {
58
        this.serverConfiguration = serverConfiguration;
L
Liang Zhang 已提交
59 60
        taskExecuteEngine = new ShardingScalingExecuteEngine(serverConfiguration.getWorkerThread());
        importerExecuteEngine = new ShardingScalingExecuteEngine(serverConfiguration.getWorkerThread());
61 62 63 64 65 66 67
        initElasticJobEntry(serverConfiguration);
    }
    
    private void initElasticJobEntry(final ServerConfiguration serverConfiguration) {
        if (!Strings.isNullOrEmpty(serverConfiguration.getName()) && null != serverConfiguration.getRegistryCenter()) {
            ElasticJobEntryLoader.init(serverConfiguration.getName(), new GovernanceCenterConfigurationYamlSwapper().swapToObject(serverConfiguration.getRegistryCenter()));
        }
68 69
    }
}