What is the best way to disable ehcache in JHipster - java

I want EHCache in production, but not in development as I tend to fiddle with database records in development.
The only thing I came across after searching is this JVM option to disable cache completely -Dnet.sf.ehcache.disabled=true, but somehow this does not appear to be the ideal solution.
Best would be to have an option in application-dev.yml. I tinkered with timeToLiveSeconds and kept it low (1), but it did not seem to have any effect.
I tried to add an api in my REST service like this, but it did not seem to have any effect either:
/**
* GET /clearCache -> Clear Cache
*/
#RequestMapping(value = "/clearCache",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
#Timed
public ResponseEntity<String> clearCache() {
CacheManager.getInstance().clearAll();
return new ResponseEntity<String>(HttpStatus.OK);
}
Also, a corollary to this same question, I put a debug breakpoint in CacheConfiguration.java here, but it never seems to get hit.
#PreDestroy
public void destroy() {
Any help is highly appreciated!
Here is my full log on startup with fast boot:
[INFO] mypackage.Application - Starting Application on Lenovo-PC with PID 7408 (D:\projects\app\target\classes started by lenovo in D:\projects\app)
[DEBUG] mypackage.Application - Running with Spring Boot v1.2.4.RELEASE, Spring v4.1.6.RELEASE
[DEBUG] org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider
[DEBUG] mypackage.config.DatabaseConfiguration - Configuring Datasource
[WARN] org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003: Could not find a specific ehcache configuration for cache named [mypackage.Org]; using defaults.
[WARN] org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003: Could not find a specific ehcache configuration for cache named [mypackage.Workflow]; using defaults.
[WARN] org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003: Could not find a specific ehcache configuration for cache named [mypackage.Department]; using defaults.
...
...
...
[DEBUG] mypackage.config.MailConfiguration - Configuring mail server
[INFO] mypackage.Application - Running with Spring profile(s) : [dev, fast]

If you look at the CacheConfiguration class, you'll see:
#Profile("!" + Constants.SPRING_PROFILE_FAST)
public class CacheConfiguration {
So if you use fast profile, caching is disabled, so either you use this profile or you change annotation to enable caching only in prod profile.
#Profile(Constants.SPRING_PROFILE_PRODUCTION)
public class CacheConfiguration {
You're right, the ehcache code where the WARN log is issued creates a cache anyway and it does it for your 3 entities: Org, Workflow and Department.
if ( cache == null ) {
LOG.unableToFindEhCacheConfiguration( name );
manager.addCache( name );
cache = manager.getEhcache( name );
LOG.debug( "started EHCache region: " + name );
}
I'm not sure the cache is used for entities as our configuration is not run but anyway even the warning is not satisfying, there's probably something missing in our configuration to avoid it.
Could you please open an issue ?

Related

Why ${spring.embedded.kafka.brokers} not populated in application-test.yaml although i see the spring.embedded.kafka.brokers existing?

We're using #EmbeddedKafka, and in application-test.yml we have
kafka:
confluentBootstrapAddress: ${spring.embedded.kafka.brokers}
I can see that confluentBootstrapAddress is null.
However, if in debug mode i check this
this.resolveEmbeddedValue("${spring.embedded.kafka.brokers}") = "127.0.0.1:51020"
then i can see that the value exists, and that the embedded Kafka was indeed started.
What could be the issue? A lifecycle / moment mismatch between the moment that application.yml is evaluated and when the embedded broker is started?
Actually there was an error in the test setup that was using #Profile instead of #ActiveProfiles. Changing that to ActiveProfiles made that Spring Test take into account the "spring-profile.properties".

Can't config Java Spring Boot data session Mongodb

I've been using this guide to set up spring session data with mongodb
https://docs.spring.io/spring-session-data-mongodb/docs/2.1.1.RELEASE/reference/htmlsingle/#introduction
However I am having problems with configuration. I'm using Mongodb with Spring boot and I'm trying to config my session time and session name for Spring boot web application, but it keeps defaulting to 30 minutes and the collection name in mongodb is still 'sessions'
These are what I have tried:
Added these to application.properties:
server.session.timeout=1
spring.session.mongodb.collection-name=TestSESSIONS
and this
server.servlet.session.timeout=60s
spring.session.mongodb.collection-name=TestSESSIONS
none of those config work
I've looked over this URL for spring common application properties for mongodb but none of it help to config the session time and collection name for mongodb.
After doing hours of research it seems like spring boot uses some kind of autoconfig with this "org.springframework.boot.autoconfigure"
so then I added this in my application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
to disable the autoconfigure.
but now it just give me this error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method mongoSessionRepository in org.springframework.session.data.mongo.config.annotation.web.http.MongoHttpSessionConfiguration required a bean of type 'org.springframework.data.mongodb.core.MongoOperations' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'mongoTemplate' in 'MongoDataAutoConfiguration' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on MongoDataAutoConfiguration.AnyMongoClientAvailable.FallbackClientAvailable #ConditionalOnBean (types: com.mongodb.client.MongoClient; SearchStrategy: all) did not find any beans of type com.mongodb.client.MongoClient; NestedCondition on MongoDataAutoConfiguration.AnyMongoClientAvailable.PreferredClientAvailable #ConditionalOnBean (types: com.mongodb.MongoClient; SearchStrategy: all) did not find any beans of type com.mongodb.MongoClient
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.data.mongodb.core.MongoOperations' in your configuration.
this is the #bean from the spring.io guide 'mongoSessionConverter' from above link
this is the java file MongoHttpSessionConfiguration from spring that that's autoconfig by spring; I have tried extending "MongoHttpSessionConfiguration" and overriding the setter methods my self. Such as the "setMaxInactiveIntervalInSeconds" for sessionTime and
"setCollectionName" for mongododb database collection name.
but I've have this error:
Description:
The bean 'mongoSessionRepository', defined in class path resource [com/khatpass/app/config/SessionListenerConfig.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.class] and overriding is disabled.
I am stuck on trying to configure spring boot session with Mongodb. The session always defaulting to 30 minutes and the collection name is always 'sessions' in mongodb collections. Not sure how to change that serverSelectionTimeout='30000 ms' and mongodb collections name "sessions" I don't know what to do, need help.
2019-02-24 13:39:54.501 INFO 36113 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
After doing so much research and then finally, going through the source code, I found the solution:
#EnableMongoHttpSession(maxInactiveIntervalInSeconds = 24 * 60 * 60)
public class SessionConfiguration {}
To override the default collection name, there is another annotation attribute collectionName.
This is working for Spring Boot 2.1.1
After looking over the class MongoOperationsSessionRepository from org.springframework.session.data.mongo it seems like it can't be config through application.properties because the class is using static final values
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
and
public static final String DEFAULT_COLLECTION_NAME = "sessions";
only way to change the value is intercept the object before it gets saved. No getters or setters for those fields, it can't be change in an easy way, what a joke!

Quartz 2.2.3 JobStore Property Being Overriden by Default Settings - Spring Boot, Liquibase, Oracle

I am trying to implement a Quartz scheduler in my Spring Boot application; however, I continue to receive the following error after startup:
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "SCHED_NAME": invalid identifier.
In the startup logs before the error, I see:
liquibase : Successfully acquired change log lock
liquibase : Reading from PARTSVOICE_APP.DATABASECHANGELOG
liquibase : Successfully released change log lock
o.q.i.StdSchedulerFactory : Using default implementation for ThreadExecutor
o.q.c.SchedulerSignalerImpl : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
o.q.c.QuartzScheduler : Quartz Scheduler v.2.2.3 created.
o.s.s.q.LocalDataSourceJobStore : Using db table-based data access locking (synchronization).
o.s.s.q.LocalDataSourceJobStore : JobStoreCMT initialized.
o.q.c.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.2.3) 'scheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 25 threads.
Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is not clustered.
This message is concerning because in my application.yml, my setup is the following:
org:
quartz:
scheduler:
instanceName: cdp-scheduler
instanceId: AUTO
threadPool:
threadCount: 25
class: org.quartz.simpl.SimpleThreadPool
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
tablePrefix: c_
I read these properties and put them within a SchedulerFactoryBean here:
#Bean
public SchedulerFactoryBean scheduler(DataSource ds, SpringLiquibase dependent) throws SchedulerException, ConnectionException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
Properties quartzProps = new Properties();
quartzProps.setProperty("org.quartz.scheduler.instanceId", quartzConfig.getInstanceId());
quartzProps.setProperty("org.quartz.scheduler.instanceName", quartzConfig.getInstanceName());
quartzProps.setProperty("org.quartz.threadPool.threadCount", quartzConfig.getThreadCount());
quartzProps.setProperty("org.quartz.jobStore.class", quartzConfig.getJobStoreClass());
quartzProps.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.oracle.OracleDelegate");
quartzProps.setProperty("org.quartz.threadPool.class", quartzConfig.getThreadPoolClass());
factory.setOverwriteExistingJobs(true);
factory.setAutoStartup(false);
factory.setDataSource(ds);
factory.setQuartzProperties(quartzProps);
factory.setExposeSchedulerInRepository(true);
factory.setAutoStartup(true);
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
factory.setJobFactory(jobFactory);
return factory;
}
I am trying to use the org.quartz.impl.jdbcjobstore.JobStoreTX class so that I can store the Quartz information in our database. However, I believe this is getting overridden from what I can see from the log message above. A quick Google search has shown me that if you provide a datasource, then the JobStoreCMT class is automatically implemented but that doesn't make sense to me.
I'm also using Liquibase to execute a SQL script that creates the quartz tables, this is being executed fine, it drops and creates the tables I need. I've also tried using Flybase but I get the same error, so it definitely is related to my quartz setup.
Has anybody had a similar experience? Any suggestions? Please let me know if you think I should supply more information. Thanks.
For the error I can infer that the tables aren't properly created (either not at all or using the wrong scripts). For Quartz, you need to create the tables using the db scripts for you database type, which you can find in the quartz download.
Here is a similar case: https://groups.google.com/forum/#!topic/axonframework/IlWZ0UHK2hk
If you think that the script is OK, please try to create the table directly in your database.
In case that it still does not work you can go to the next step and check the configuration. Maybe the tablePrefix that you are using is not well interpreted. Try to use the default QRTZ_.
Here is an setup example with a lower version of Quartz.
http://www.opencodez.com/java/quartz-scheduler-with-spring-boot.htm
Let me know if it works!

How to use Hibernate with multiple databases on a single server

Our application allows our customer to have multiple databases, all running on one instance of the database server.
For example, databases might be dbcommon, dbLive, dbStaging, dbUAT, dbDev, dbSandbox. The common database and the Production database always exists, but the others are optional (and there is no limit). In dbcommon there is a table that tells us all the databases....so that's where I would need to start. The tables in common are different from the others, and the others all have the same schema (subscriber data)
Using Hibernate, how can I dynamically create/use a connection to either Live or Staging (or any of the others)? I am using Spring if that helps.
I have come across answers that suggest creating different connections in configuration, but because the number of subscriber databases can vary (per install, not while the app is running), this isn't an option for me.
As I discovered after posting this question, and as the user manish suggested, Hibernate's Multi Tenancy support (using the Database MultiTenancyStrategy) works for me. I had to piece together a solution using various resources (listed below).
http://www.ticnfae.co.uk/blog/2014/07/16/hibernate-multi-tenancy-with-spring/
Setting up a MultiTenantConnectionProvider using Hibernate 4.2 and Spring 3.1.1
Multi-Tenancy with Spring + Hibernate: "SessionFactory configured for multi-tenancy, but no tenant identifier specified"
I'm still looking for a way to be able to reference the common (shared) database at the same time as tenant databases...and will try to add that to this answer when complete.
The simplest way I can see to do this is to manage everything via profiles in Spring.
I accomplished this by using application.yml. I'm also using a Hikari connection pool, but that doesn't effect the configuration too much.
Here is an example of a application.yml with 3 profiles listed, and I've defined two of them as an example.
spring:
profiles:
include: dev,test,production
active: dev
---
spring:
profiles: dev
oms:
omsDataSource:
driverClassName: com.informix.jdbc.IfxDriver
jdbcUrl: jdbc:informix-sqli://devdb:9000/hol:INFORMIXSERVER=m_tcp_1;client_deve=en_US.8859-1;db_deve=en_US.8859-1;LOBCACHE=-1
password: oms
username: oms
connectionTestQuery: select count(*) from systables
maximumPoolSize: 5
---
spring:
profiles: test
oms:
omsDataSource:
driverClassName: com.informix.jdbc.IfxDriver
jdbcUrl: jdbc:informix-sqli://testdb:9000/hol:INFORMIXSERVER=m_tcp_1;client_deve=en_US.8859-1;db_deve=en_US.8859-1;LOBCACHE=-1
password: oms
username: oms
connectionTestQuery: select count(*) from systables
maximumPoolSize: 5
In my DB config class, I set the JPA repositories, and tell it what entityManager to use. I also setup the configuration properties to pull frmo the application.yml . This means it will swap out the details based on the profile the app is using on launch.
#EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryOms",
transactionManagerRef = "transactionManagerOms",
basePackages= "persistence.oms")
#Configuration
#ConfigurationProperties(prefix = "oms.omsDataSource")
public class omsDbConfig extends HikariConfig {
//This will automatically fill in the required fields from the application.yml.
#Bean
public HikariDataSource orcaDataSource() throws SQLException {
return new HikariDataSource(this);
}
//I use that datasource to define my entityMangerFactory
#Bean(name = "entityManagerFactoryOms")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryOrca() throws SQLException {
JpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
Properties props = new Properties();
props.setProperty("hibernate.dialect","org.hibernate.dialect.InformixDialect");
LocalContainerEntityManagerFactoryBean emfb =
new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(orcaDataSource());
emfb.setPackagesToScan("persistence.oms");
emfb.setJpaProperties(props);
emfb.setJpaVendorAdapter(adapter);
return emfb;
}
}
The entities and repositories are defined normally, there's nothing special there. The DB will switch connection based on whatever profile I tell it to run.
I just switch out the active profile in the application.yml to whichever one I need.
Safety note: Define a production profile, don't have production as a default profile.

Set root logging level in application.yml

I used an application.properties with Spring Boot (1.3 M1) and started to translate it into a yaml file because it grew more and more complex.
But I have problems translating this into yaml:
logging.level.*=WARN
logging.level.com.filenet.wcm=ERROR
logging.level.de.mycompany=DEBUG
The last two lines are easily translated into this:
logging:
level:
com.filenet.wcm: ERROR
de.mycompany: DEBUG
But how to add the values for the root logging level ? These two approaches failed:
Failed approach 1:
logging:
level: WARN
com.filenet.wcm: ERROR
de.mycompany: DEBUG
Failed approach 2:
logging:
level:
star: WARN
com.filenet.wcm: ERROR
de.mycompany: DEBUG
I read the docs, searched stackoverflow and googled but did not find an example for a valid syntax.
You can use ROOT to configure the root logging level:
logging:
level:
ROOT: DEBUG
If you want level by package, you can use this syntax :
logging:
level:
org.springframework.web: DEBUG
guru.springframework.controllers: DEBUG
org.hibernate: DEBUG
org: INFO
You can even use your classname to configure logging level:
logging:
level:
com.yourorganization.Yourclass: DEBUG
It's an old question, but I just had this problem.
While setting
org.springframework.web: debug
or
org.hibernate: debug
works fine, if you want to do the same for your project files (setting level per package), you have to use wildcards. So, for the example in the question, it would be:
logging:
level:
root: WARN
com.filenet.wcm.*: ERROR
de.mycompany.*: DEBUG
Alternatively, you can set the logging level per class without using wildcards, as shown in torina's answer.
You can log incoming request by creating CommonsRequestLoggingFilter and adding
logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG
to application.properties file as explained in detail in this link - https://www.baeldung.com/spring-http-logging
#Configuration
public class RequestLoggingFilterConfig {
#Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter
= new CommonsRequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(10000);
filter.setIncludeHeaders(false);
filter.setAfterMessagePrefix("REQUEST DATA : ");
return filter;
}
}

Categories