setting config property for hazelcast - java

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)

Related

How do I define the initial context factory in shiro.ini?

In my Shiro application, I am using the JndiRealmFactory to define my realms. I Register my realms like this:
Hashtable<String, String> args = new Hashtable<>();
args.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
InitialContext ctx = new InitialContext(args);
ctx.bind("realms/MyRealm", myRealm);
However, Shiro cannot find realms/MyRealm. I think the reason is, that I Need to define the JndiRealmFactory's jndiEnvironment, which contains the Context.INITIAL_CONTEXT_FACTORY property. However, I don't know how to set this property in my shiro.ini:
realmFactory.jndiEnvironment = ?
I am running my application on TomEE.
My current shiro.ini looks like this:
realmFactory = org.apache.shiro.realm.jndi.JndiRealmFactory
realmFactory.jndiNames = realms/MyRealm
you are not supposed to bind at runtime anything in the EE context. You can probably define it in tomcat as a resource or resources.xml (tomee specific) then just use a standard lookup to read the value (no special properties passed to InitialContext.

Quartz Scheduler: How to dynamically read a quartz property from Java via API?

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

How to access EJB on remote server?

I am using a GlassFish-3.1.2 server running in my subnet (192.168.1.3:3700). I already deployed an enterprise app including an EJB in which i defined a business method. Now I want to remotely access the EJB from my java application client. How do i have to setup the JNDI resp. the InitialContext object for doing the lookup of the EJB ? How do I need to define the properties? Btw. I had to run "asadmin enabled-secure-admin" in order to make the GlassFish server work on the LAN. Probably I also need to send my credentials with the properties ?
Here's my current "solution", which seems to be completley wrong :
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.3");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);
TestentityFacadeRemote tfr = (TestentityFacadeRemote)ctx.lookup("java:global/TestEE/TestEE-ejb/TestentityFacadeRemote");
When I run this programm, it just waits infinitely...
Any help highly appreciated!
I solved the problem by setting the host and port directy by System.setProperty() and using the default constructor for initializing the InitialContext(). Note that the following lines should be the very first in your program / main method:
public static void main(String[] args) {
System.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.3");
System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext();
TestentityFacadeRemote tfr = (TestentityFacadeRemote)ctx.lookup("java:global/TestEE/TestEE-ejb/TestentityFacadeRemote!com.acme.remote.TestentityFacade");
}
Hope this helps ...

hibernate changing cfg properties at runtime

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.

Hibernate: overriding xml config file with another file

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().

Categories