I'm trying to change cfg properties at runtime.
For example:
cfg.setProperty("hibernate.connection.url")
The problem is that it works only when this property is not defined in the cfg file itself,
meaning, it doesn't override.
Can it be done in some way?
when you run
Configuration cfg = new Configuration().configure();
the .configure() reads the data from the XML, and it has a higher priority over the programmatic configuration.
However, if you remove the .configure, all the configuration will be "read" from the settings that you will pass. For example:
Configuration configuration = new Configuration()
.setProperty( "hibernate.connection.driver_class", "org.postgresql.Driver" )
.setProperty( "hibernate.dialect","org.hibernate.dialect.PostgreSQLDialect")
[...snip...]
.addAnnotatedClass( com.myPackage.MyClass.class )
[...] ;
will set all the properties at runtime.
Related
Currently, our application uses Log4J 1.2 and configures it using either
File file = ...
PropertyConfigurator.configure(file.getAbsolutePath());
or
URL url = ...
PropertyConfigurator.configure(url);
I know that the property file format has changed from 1.2 to 2, but what would be a similar way to configure Log4J 2 using a property file at an arbitrary file or URL?
You can use PropertiesConfigurationBuilder as follows:
// Custom-loaded properties.
Properties props = ...
// Beware it should be org.apache.logging.log4j.core.LoggerContext class,
// not the one ins spi package!
// Not sure about the meaning of "false".
LoggerContext context = (LoggerContext)LogManager.getContext(false);
Configuration config = new PropertiesConfigurationBuilder()
.setConfigurationSource(ConfigurationSource.NULL_SOURCE)
.setRootProperties(props)
.setLoggerContext(context)
.build();
context.setConfiguration(config);
Configurator.initialize(config);
It's true that using the core classes looks like a hack but the author himself uses them in his tutotrial: https://logging.apache.org/log4j/log4j-2.3/manual/customconfig.html .
From Log4J 2's documentation:
// import org.apache.logging.log4j.core.LoggerContext;
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
// this will force a reconfiguration
context.setConfigLocation(file.toURI());
Make sure to refer to org.apache.logging.log4j.core.LoggerContext (defined in the log4j-core artifact, not the log4j-api one) and not to org.apache.logging.log4j.spi.LoggerContext.
I need to create a session factory for hibernate, but by default it looks for "hibernate.cfg.xml" file. I did not create this file. All hibernate config is in "applicationContext.xml" file of Spring MVC config. All I need to do is provide "/WEB-INF/applicationContext" file path to the "configure" method of Configuration class, but I don't know how find the relative path to this file in JAVA.
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure(HERE I NEED TO GET "/WEB-INF/applicationContext.xml FILE PATH");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory; }
Hibernate loads /WEB-INF/applicationContext.xml using ClassLoader.getResourceAsStream("/WEB-INF/applicationContext.xml")
ClassLoader tries to get access to the files in the root of the classpath — for the web-applications it is war/WEB-INF/classes/. You should put applicationContext.xml to the war/WEB-INF/classes/applicationContext.xml and load it using
sessionFactory = new Configuration().configure("applicationContext.xml")
.buildSessionFactory();
You can't specify /WEB-INF/applicationContext.xml, because of /WEB-INF/ is not in the class path.
How Hibernate loads resources
If you really want to get a configuration from /WEB-INF/applicationContext.xml you should get URL of applicationContext.xml using ServletContext: File path to resource in our war/WEB-INF folder?
And pass that URL to the Configuration or StandardServiceRegistryBuilder, it depends of Hibernate version.
An important notice
I think, above is senseless, because of applicationContext.xml should has a structure like hibernate.cfg.xml and it, obviously, doesn't.
The file should be in your classpath, so you should be able to reference it using the classpath variable: classpath:/WEB-INF/applicationContext.xml
If this won't work then you load the file using class loader and then "ask" for the full path ( this should not be necessary though... ):
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource( "WEB-INF/applicationContext.xml" );
String fullPath = resource.getPath()
I am able to load my hazelcast.xml file when using the FileSystemXmlConfig. However, I was only able to use that on the hazelcast server - I also need to run the hazelcast client. However, I could not figure out how to use the FileSystemXmlConfig for instantiating ClientConfig in Hazelcast.
Another thing I am trying is to set the system properties via code (not through the launcher). However, when I tried this, it does not appear to have picked my hazelcast.xml file since I can see that my store load were not called (they get called when I use the server setup with FileSystemXmlConfig copied below) but not when I try setting system properties (via System or via Properties). Any ideas on what I'm doing wrong?
Not working (System.setProperty with XML file name passed in)
Config config = new Config();
System.setProperty("hazelcast.config", "C:/Users/userName/workspace/HazelcastTest/config/hazelcast.xml");
HazelcastInstance hcast= Hazelcast.newHazelcastInstance(config);
Not working (System.setProperty with XML file location passed in)
Config config = new Config();
System.setProperty("hazelcast.config", "C:/Users/userName/workspace/HazelcastTest/config/");
HazelcastInstance hcast= Hazelcast.newHazelcastInstance(config);
Not Working (used via Properties)
Config config = new Config();
Properties props = System.getProperties();
props.setProperty("hazelcast.config", "C:/Users/userName/workspace/HazelcastTest/config/hazelcast.xml");
HazelcastInstance hcast= Hazelcast.newHazelcastInstance(config);
Works with FileSystemXmlConfig
Config config = new FileSystemXmlConfig("C:/Users/userName/workspace/HazelcastTest/config/hazelcast.xml");
HazelcastInstance hcast= Hazelcast.newHazelcastInstance(config);
You need to use the XmlClientConfigBuilder
ClientConfig config = new XmlClientConfigBuilder(yourfile).build();
HazelcastInstance client = HazelcastClient.newHazelcastClient(config)
Quartz is usually configured via quartz.properties on the classpath.
e.g.:
org.quartz.scheduler.instanceName = BagginsScheduler
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=5
org.quartz.threadPool.threadPriority=1
From within the same application that will run the Quartz jobs, I'd like to read out the properties.
Reading the scheduler name is easy:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
String name = scheduler.getSchedulerName();
But how can I read the `threadPriority' property?
The following does not work:
scheduler.getContext().getString("org.quartz.threadPool.threadPriority");
UPDATED Solution:
It seems that the property can't be read via Quartz API, you have to go via regular Properties:
Properties prop = new Properties();
prop.load(AnyClassUsedByJVM.class.getClassLoader().getResourceAsStream("quartz.properties"));
String prio = prop.getProperty("org.quartz.threadPool.threadPriority");
This works fine.
You can just add that property to your quartz.properties. For example:
org.quartz.threadPool.threadPriority=3
For more information, see here and configuration documentation
EDIT: To read properties at runtime, you can use Properties. Here's a sample snippet of code you can use:
Properties p = new Properties();
p.load("/tmp/quartz.properties"); // path to your properties file
System.out.println(p.getProperty("org.quartz.threadPool.threadPriority"); // prints 3
I have a source tree composed of source and test classes. When I run the tests, I'd like to use <property name="hbm2ddl.auto">create</property> while when running the code I'd like to use a validate value instead of create.
I thought to use two config files, one with all the properties and containing hbm2ddl.auto set to validate, and another with hbm2ddl.auto set to create. I hoped that the following code would have allowed me to read from the tests the basic file and override the only hbm2ddl.auto propery, but it doesn't work (the value of hbm2ddl.auto is still the one read from hibernate.cfg.xml.
Configuration configuration = new Configuration();
configuration = configuration.
configure("hibernate.cfg.xml").
addResource("hibernate-test.cfg.xml");
How can I have two different values for a property, without replicating the whole config file?
It seems to me that when you have only a few values to override, one simple approach is to load the xml config as usual, and then call setProperty programmatically, like this:
Configuration configuration = new Configuration();
configuration = configuration.configure("hibernate.cfg.xml");
configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop");
hbm.xml files don't allow to override values with addResource(...) as I tried to do, the values are only added and not overridden
I had that problem with trying to programmatically load another config. Work around i used was to have another hibernate.properties file (instead of xml config). You can set the alternate hibm2ddl value in this properties file and load it using following code:
Properties props = new Properties();
props.load(new FileInputStream(propFile));
configuration = new Configuration().setProperties(props);
Try and see if this works for you.
Imp: don't call configuration.configure().