0

I'm trying to integrate redis cache to JHipster generator following this pull request on Github: https://github.com/jhipster/generator-jhipster/pull/10057/commits/cd2f2865d35dfd77624dd3a38ed32822e895539d#

I receive this error while building my project:

[ERROR]   symbol:   method getRedis()
[ERROR]   location: class io.github.jhipster.config.JHipsterProperties.Cache
[ERROR] ../config/CacheConfiguration.java:[61,139] cannot find symbol

The method getRedis() is undefined for the type JHipsterProperties.CacheJava(67108964)

Where is getRedis() defined?

CacheConfiguration method in CacheConfiguration.java:

private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;

            public CacheConfiguration(JHipsterProperties jHipsterProperties) {
                MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();
                Config config = new Config();
                config.useSingleServer()
                .setAddress(jHipsterProperties.getCache().getRedis().getServer())
                .setSubscriptionConnectionMinimumIdleSize(1)
                .setSubscriptionConnectionPoolSize(50)
                .setConnectionMinimumIdleSize(24)
                .setConnectionPoolSize(64)
                .setDnsMonitoringInterval(5000)
                .setIdleConnectionTimeout(10000)
                .setConnectTimeout(10000)
                .setTimeout(3000)
                .setRetryAttempts(3)
                .setRetryInterval(1500)
                .setDatabase(0)
                .setPassword(null)
                .setSubscriptionsPerConnection(5)
                .setClientName(null)
                .setSslEnableEndpointIdentification(true)
                .setSslProvider(SslProvider.JDK)
                .setSslTruststore(null)
                .setSslTruststorePassword(null)
                .setSslKeystore(null)
                .setSslKeystorePassword(null)
                .setPingConnectionInterval(0)
                .setKeepAlive(false)
                .setTcpNoDelay(false);
            jcacheConfig.setStatisticsEnabled(true);
            jcacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, jHipsterProperties.getCache().getRedis().getExpiration())));
            jcacheConfiguration = RedissonConfiguration.fromInstance(Redisson.create(config), jcacheConfig);
        }

Am I missing some dependencies for getRedis()?

Note: I left out this in build.gradle.ejs; would this be causing the problem?

  <%_ if (cacheProvider === 'redis') { _%>
    implementation "org.redisson:redisson"
        <%_ if (enableHibernateCache) { _%>
    implementation "org.hibernate:hibernate-jcache"
        <%_ } _%>
    <%_ } _%>

Solution?:

ApplicationProperties.java:

@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
public class ApplicationProperties {

     private final Redis redis = new Redis();

     public Redis getRedis() {
          return redis;
      }

      public static class Redis {
          private String server = JHipsterDefaults.Cache.Redis.server;
          private int expiration = JHipsterDefaults.Cache.Redis.expiration;

          public String getServer() {
              return server;
          }

          public void setServer(String server) {
              this.server = server;
          }

          public int getExpiration() {
              return expiration;
          }

          public void setExpiration(int expiration) {
              this.expiration = expiration;
          }
      }
}

CacheConfiguration.java

 <%_ if (cacheProvider === 'redis') { _%>
        private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;

            public CacheConfiguration(JHipsterProperties jHipsterProperties, ApplicationProperties applicationProperties) {

            MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();
            Config config = new Config();
            config.useSingleServer()
                .setAddress(applicationProperties.getRedis().getServer());
                .setSubscriptionConnectionMinimumIdleSize(1)
                .setSubscriptionConnectionPoolSize(50)
                .setConnectionMinimumIdleSize(24)
                .setConnectionPoolSize(64)
                .setDnsMonitoringInterval(5000)
                .setIdleConnectionTimeout(10000)
                .setConnectTimeout(10000)
                .setTimeout(3000)
                .setRetryAttempts(3)
                .setRetryInterval(1500)
                .setDatabase(0)
                .setPassword(null)
                .setSubscriptionsPerConnection(5)
                .setClientName(null)
                .setSslEnableEndpointIdentification(true)
                .setSslProvider(SslProvider.JDK)
                .setSslTruststore(null)
                .setSslTruststorePassword(null)
                .setSslKeystore(null)
                .setSslKeystorePassword(null)
                .setPingConnectionInterval(0)
                .setKeepAlive(false)
                .setTcpNoDelay(false);
            jcacheConfig.setStatisticsEnabled(true);
            jcacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, applicationProperties.getRedis().getExpiration())));
            jcacheConfiguration = RedissonConfiguration.fromInstance(Redisson.create(config), jcacheConfig);
        }

application.yml.ejs

# ===================================================================
# Application specific properties
# Add your own application properties here, see the ApplicationProperties class
# to have type-safe configuration, like in the JHipsterProperties above
#
# More documentation is available at:
# https://www.jhipster.tech/common-application-properties/
# ===================================================================

# application:
    application.redis.server: redis://localhost:6379
    application.redis.expiration: 300
jche
  • 129
  • 2
  • 16

1 Answers1

1

You are missing the respective changes in the JHipster library which are not released yet (located in this pull request).

My advice (until it's released) would be to copy the changes (the Redis class and values) from JhipsterProperties.java to your ApplicationProperties.java.

Then if you need to configure the values to a non-default value, you can do so in your application.yml under the application: key.

Lastly add ApplicationProperties applicationProperties to the constructor in CacheConfiguration.java next to JhipsterProperties and reference getRedis() from there.

I believe the reddison dependency is also needed.

Jon Ruddell
  • 6,244
  • 1
  • 22
  • 40
  • Hello Jon, do I copy ApplicationProperties.java into config folder? What values do I add in application.xml under application: key? "then add ApplicationProperties to the constructor next to JhipsterProperties and reference getRedis() from there." sorry, i'm very confused. – jche Jul 16 '19 at 19:34
  • All I want to do is integrate redis cache onto https://github.com/jhipster/generator-jhipster, so I could run it locally. – jche Jul 16 '19 at 19:37
  • I tried to update the answer to be more clear. You already have an ApplicationProperties.java file, copy the changes to JHipsterProperties in there. Then if you need to customize the values, you can do so in application.yml. Another option is just hardcoding the config values instead of loading them from the properties class, but that is less-configurable – Jon Ruddell Jul 16 '19 at 19:49
  • What do you mean by "Then if you need to configure the values to a non-default value, you can do so in your application.yml under the application: key."? What does this do? – jche Jul 16 '19 at 19:58
  • Would you mind looking at my solution posted above to see if I made any mistakes please? – jche Jul 16 '19 at 20:04
  • You don't need the `@Test` methods, and I guess you would need to configure the values as they are only set to default for JHipsterProperties. In application.yml, add `application.redis.server: redis://localhost:6379` and `application.redis.expiration: 300` – Jon Ruddell Jul 16 '19 at 20:11
  • After I set "ApplicationProperties applicationProperties" as an additional parameter to CacheConfiguration(), do I change .setAddress(jHipsterProperties.getCache().getRedis().getServer()) -> .setAddress(applicationProperties.getRedis().getServer()); – jche Jul 16 '19 at 21:39
  • ^ I do not have getCache() in ApplicationProperties though? – jche Jul 16 '19 at 21:40
  • Hi Jon, getRedis() is fixed. But, now it says Redis cannot be resolved or is not a fieldJava for server and expiration variables in ApplicationProperties. Any thoughts? :: private String server = JHipsterDefaults.Cache.Redis.server; private int expiration = JHipsterDefaults.Cache.Redis.expiration; – jche Jul 16 '19 at 22:24
  • Rep;ace `JHipsterDefaults.Cache.Redis.server` with `redis://localhost:6379` and `JHipsterDefaults.Cache.Redis.expiration` with 300, they are default values. The updated JHipsterDefaults is not released yet – Jon Ruddell Jul 16 '19 at 22:43
  • Ok. Build success finally. But, "The elements [jhipster.cache.redis.expiration] were left unbound.". redis expiration is already set to 3600 in application-dev.yml, why is it unbound? – jche Jul 16 '19 at 22:49
  • Could we continue our conversation in this thread: https://stackoverflow.com/questions/57066355/jhipster-redis-integration-invalid-field, also so I could give credit for your answer. – jche Jul 16 '19 at 22:50