2
votes

I am new to spring data gemfire/apache geode. I am getting the following error. Can anyone explain what is wrong with my configuration?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gemfireCache' defined in org.springframework.data.gemfire.config.annotation.ClientCacheConfiguration: Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:741) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE] at com.oreedoo.restful.RestfulGemFireApplication.main(RestfulGemFireApplication.java:32) [main/:na] Caused by: java.lang.ExceptionInInitializerError: null at org.apache.geode.distributed.internal.InternalDistributedSystem.(InternalDistributedSystem.java:126) ~[geode-core-1.2.1.jar:na] at org.springframework.data.gemfire.util.DistributedSystemUtils.getDistributedSystem(DistributedSystemUtils.java:86) ~[spring-data-geode-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.data.gemfire.client.ClientCacheFactoryBean.getDistributedSystem(ClientCacheFactoryBean.java:218) ~[spring-data-geode-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.data.gemfire.client.ClientCacheFactoryBean.resolveProperties(ClientCacheFactoryBean.java:197) ~[spring-data-geode-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.data.gemfire.CacheFactoryBean.afterPropertiesSet(CacheFactoryBean.java:174) ~[spring-data-geode-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] ... 16 common frames omitted Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLogger cannot be cast to org.apache.logging.log4j.core.Logger at org.apache.geode.internal.logging.LogService.init(LogService.java:81) ~[geode-core-1.2.1.jar:na] at org.apache.geode.internal.logging.LogService.(LogService.java:72) ~[geode-core-1.2.1.jar:na] ... 23 common frames omitted

Here is my configuration:

@SpringBootApplication
@ClientCacheApplication(name = "ClientGemFireApplication", logLevel = 
"config")
@EnableEntityDefinedRegions(basePackageClasses = Subscription.class,
clientRegionShortcut = ClientRegionShortcut.LOCAL)
@EnableGemfireRepositories
@Import(GemFireConfiguration.class)
public class RestfulGemFireApplication {

public static void main(String[] args) {
    SpringApplication.run(RestfulGemFireApplication.class, args);
}

@Bean
ApplicationRunner run(SubscriptionRepository subscriptionRepository) {

    return args -> {

        Subscription sub1 = new Subscription("Sub1", null, null, null, null, null, null, null, null, null, null);
        Subscription sub2 = new Subscription("Sub2", null, null, null, null, null, null, null, null, null, null);
        Subscription sub3 = new Subscription("Sub3", null, null, null, null, null, null, null, null, null, null);

        System.out.println("Before accessing data in GemFire...");

        asList(sub1, sub2, sub3).forEach(subscription -> System.out.println("\t" + subscription));

        System.out.println("Saving Sub1, Sub2 and Sub3 to GemFire...");

        subscriptionRepository.save(sub1);
        subscriptionRepository.save(sub2);
        subscriptionRepository.save(sub3);

        System.out.println("Lookup each subscription by accessId...");

        asList(sub1.getAccessMethodId(), sub2.getAccessMethodId(), 
sub3.getAccessMethodId())
          .forEach(accessMethodId -> System.out.println("\t" + 
subscriptionRepository.findSubscriptionByAccessMethodId(accessMethodId)));

    };
}
}


@Configuration
public class GemFireConfiguration {

public Properties gemfireProperties() {

    Properties gemfireProperties = new Properties();

    gemfireProperties.setProperty("name", 
RestfulGemFireApplication.class.getSimpleName());
    gemfireProperties.setProperty("mcast-port", "0");
    gemfireProperties.setProperty("log-level", "config");

    return gemfireProperties;
}

@Bean
public CacheFactoryBean gemfireCache() {

    CacheFactoryBean gemfireCache = new CacheFactoryBean();

    gemfireCache.setClose(true);
    gemfireCache.setProperties(gemfireProperties());

    return gemfireCache;
}

@Bean
ClientRegionFactoryBean<String, Subscription> subscriptionRegion(final 
 GemFireCache cache) {
    ClientRegionFactoryBean<String, Subscription> subscriptionRegion = new 
ClientRegionFactoryBean<>();
    subscriptionRegion.setCache(cache);
    subscriptionRegion.setName("Subscription");
    subscriptionRegion.setClose(false);
    subscriptionRegion.setShortcut(ClientRegionShortcut.PROXY);
    subscriptionRegion.setPersistent(false);
    return subscriptionRegion;
}
}

@Region("Subscription")
@Data
public class Subscription {

@Id @NonNull//FIXME primary key
String accessMethodId;

String activationDttm;

String customerAverage;

String customerId;

String marketSegmentation;

String subscriptionOffering;

String subscriberNum;

String subscriptionStatus;

String subscriptionType;

String terminatedDttm;

String usageSegmentation;


@PersistenceConstructor
public Subscription(String accessMethodId, String activationDttm, String 
customerAverage, String customerId,
        String marketSegmentation, String subscriptionOffering, String 
subscriberNum, String subscriptionStatus,
        String subscriptionType, String terminatedDttm, String 
usageSegmentation) {
    super();
    this.accessMethodId = accessMethodId;
    this.activationDttm = activationDttm;
    this.customerAverage = customerAverage;
    this.customerId = customerId;
    this.marketSegmentation = marketSegmentation;
    this.subscriptionOffering = subscriptionOffering;
    this.subscriberNum = subscriberNum;
    this.subscriptionStatus = subscriptionStatus;
    this.subscriptionType = subscriptionType;
    this.terminatedDttm = terminatedDttm;
    this.usageSegmentation = usageSegmentation;
}

}
2
Hi, I edited your question to get to the point a bit quicker.user3603486
Same issue here. As a workaround you can exclude spring-boot-starter-logging from the spring-boot-starter dependency, but you'll lose the logging. Any help is highly appreciatedsenape

2 Answers

1
votes

Spring Data GemFire is tightly coupled with log4j2. I've fixed the classcast exception with the following configuration:

pom.xml

Exclude the spring-boot-starter-logging from spring-boot-starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

(Optional) Add a log4j2.xml file under the src/maini/resources/folder

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} - [%15.15t] %-40.40c{1.} : %m%n%ex
        </Property>
    </Properties>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="ConsoleAppender" />
        </Root>
    </Loggers>
</Configuration>

Placing one (and only one) of slf4j-nop.jar slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar dependendcy

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
</dependency>
0
votes

i had the similar issue with

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gemfireCache' defined in ..... java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLogger cannot be cast to org.apache.logging.log4j.core.Logger at ..

i had this dependency in my build gradle

compile 'org.springframework.data:spring-data-geode:2.0.10.RELEASE'

Fixed issue after adding this ->

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