Spring Data GemFire ​​- вызвано: java .lang.IllegalStateException: пул соединений "DEFAULT" не был создан - PullRequest
0 голосов
/ 22 февраля 2020

Я пытаюсь настроить spring-data-gemfire в моем проекте. Требование базовое c: мне просто нужно подключиться к серверу gemfire и получить данные. Пока что я выполнил следующую настройку / настройку данных в моем проекте.

build.gradle

dependencies {
    implementation (
            'org.springframework.boot:spring-boot-starter',
            'org.springframework.boot:spring-boot-starter-web',
            'org.springframework.boot:spring-boot-starter-actuator',
            'org.springframework.boot:spring-boot-starter-data-rest',
            'org.springframework.data:spring-data-gemfire:2.1.8.RELEASE',
            'org.springframework.boot:spring-boot-starter-data-gemfire:1.5.22.RELEASE',
            'com.gemstone.gemfire:gemfire:8.2.0',
            'com.gemstone.gemfire:gfSecurityImpl:8.1.0',
            'com.dish.mad:gemfire-util:0.0.10',
            "io.pivotal.spring.cloud:spring-cloud-services-starter-config-client:$configClientVersion",
            "io.springfox:springfox-swagger-ui:$springfoxSwaggerVersion",
            "io.springfox:springfox-swagger2:$springfoxSwaggerVersion",
            "org.projectlombok:lombok:$lombokVersion"
    )
    testImplementation (
            'org.springframework.boot:spring-boot-starter-test',
            "com.jayway.restassured:rest-assured:$restassuredVersion",
            "com.jayway.restassured:json-schema-validator:$restassuredVersion",
            "org.mock-server:mockserver-integration-testing:$mockserverVersion",
            "org.mock-server:mockserver-netty:$mockserverVersion",
            "org.hamcrest:hamcrest-library:$hamcrestVersion"
    )

    annotationProcessor "org.projectlombok:lombok:$lombokVersion"

}

configurations{
    implementation.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
}

GemfireConfigProperties. java

    @Bean
    public GemfireConnector gemfireConnector(GemfireConnectionCreator gemfireConnectionCreator) {
        return gemfireConnectionCreator.create(getLocatorList());
    }

    @Bean
    public GemfireConnectionCreator gemfireConnectionCreator(){
        return new GemfireConnectionCreator(gemfireUsername,gemfirePassword);
    }

    @Bean
    public GemFireCache gemFireCache(){
        return new ClientCacheCreation();
    }

    private List<Locator> getLocatorList() {
        List<String> locators = Arrays.asList(locatorString.split(","));
        return locators.stream()
                .map(this::parseLocator)
                .collect(toList());
    }

    private Locator parseLocator(String locator) {
        String regex = "^(([a-zA-Z0-9.-]+))\\[([0-9]+)\\]";
        Pattern pattern = Pattern.compile(regex);

        Matcher matcher = pattern.matcher(locator);
        if (!matcher.find()) {
            throw new RuntimeException("Invalid format for locator " + locator + ", should be of form <domainName>[<portNumber>]");
        }

        String host = matcher.group(1);
        if (!DomainValidator.getInstance().isValid(host) && !InetAddressValidator.getInstance().isValid(host)) {
            throw new RuntimeException("Invalid format for locator " + locator + ", should be of form <domainName>[<portNumber>]");
        }
        int port = Integer.valueOf(matcher.group(3));

        return new Locator(host, port);
    }

GemfireConnectionCreator. java

    private String userName;
    private String password;

    private List<Locator> currentLocators;
    private GemfireConnector currentConnector;

    public GemfireConnectionCreator(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public synchronized GemfireConnector create(List<Locator> locators) {
        if (currentLocators == null) {
            GemfireConnector gemfireConnector = createGemfireConnector(locators);
            this.currentLocators = locators;
            this.currentConnector = gemfireConnector;
            return gemfireConnector;
        } else {
            if (locators.stream().anyMatch(l -> currentLocators.contains(l))) {
                return currentConnector;
            } else {
                this.currentConnector.close();
                GemfireConnector gemfireConnector = createGemfireConnector(locators);
                this.currentLocators = locators;
                this.currentConnector = gemfireConnector;
                return gemfireConnector;
            }
        }
    }

    private GemfireConnector createGemfireConnector(List<Locator> locators) {
        System.out.println("Create GemfireConnector "+locators);
        return new GemfireConnector(locators, this.userName, this.password);
    }

GemfireConnector. java

    private static ClientCache clientCache;
    private static final ConcurrentHashMap<String, Region> regionCache = new ConcurrentHashMap<>();

    public GemfireConnector(List<Locator> locators, String username, String password) {
        clientCache = getClientCache(locators, username, password);
    }

    private ClientCache getClientCache(List<Locator> locators, String username, String password) {
        System.out.println("Connecting to the following locators: "+ locators);

        if (clientCache == null) {
            Properties gemfireProperties = new Properties();
            gemfireProperties.setProperty("name", "SpringGemFireCacheClient");
            gemfireProperties.setProperty("security-client-auth-init", "templates.security.UserPasswordAuthInit.create");
            gemfireProperties.setProperty("security-username", username);
            gemfireProperties.setProperty("security-password", password);

            ClientCacheFactory clientCacheFactory = new ClientCacheFactory(gemfireProperties);
            clientCacheFactory.setPoolFreeConnectionTimeout(1000);
            clientCacheFactory.setPoolReadTimeout(1000);
            clientCacheFactory.setPoolMinConnections(4);
            clientCacheFactory.setPoolRetryAttempts(10);

            locators.forEach(locator -> clientCacheFactory.addPoolLocator(locator.getHost(), locator.getPort()));
            clientCache = clientCacheFactory.create();
        }

        return clientCache;
    }

    public void close() {
        clientCache.close();
    }

    public <K, V> Region getRegionConnection(String regionName) {

        if (regionCache.containsKey(regionName)) {
            System.out.println(regionName + " found, so reusing the existing instance");
            return regionCache.get(regionName);
        } else {
            ClientRegionFactory<K, V> regionFactory = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY);
            Region<K, V> newRegion = regionFactory.create(regionName);
            regionCache.put(regionName, newRegion);
            System.out.println(regionName + " not found, creating new region " + regionName + "   " + newRegion.getName() + " " + newRegion.getAttributes());
            return newRegion;
        }
    }

В этой строке

regionFactory.create(regionName);

выдается это исключение:

java.lang.IllegalStateException: The connection pool "DEFAULT" has not been created

Не могли бы вы помочь мне разобраться с этой ошибкой?

1 Ответ

0 голосов
/ 27 февраля 2020

Оказывается, я завернул bean-компонент regionConnector в компонент вместо конфигурации. Теперь это исправлено.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...