configuring infinispan in jboss 7 programmatically - java

is there a way i can take away the configuration of infinispan completely from the standalone.xml and have the configuration like the following in my persistence.xml :
<property name="hibernate.cache.infinispan.entity.strategy" value= "LRU" />
<property name="hibernate.cache.infinispan.entity.eviction.max_entries" value= "1000"/>
<property name="hibernate.cache.infinispan.entity.eviction.strategy" value= "LRU"/>
<property name="hibernate.cache.infinispan.entity.eviction.wake_up_interval" value= "2000"/>
<property name="hibernate.cache.infinispan.entity.eviction.max_entries" value= "5000"/>
<property name="hibernate.cache.infinispan.entity.expiration.lifespan" value= "60000"/>
<property name="hibernate.cache.infinispan.entity.expiration.max_idle" value= "30000"/>
thanks in advance

I don't know your use case but there is a possibility to configure Infinispan CacheManagers and Caches programmatically using fluent builder API. It means no need of standalone.xml and even no need of configuration for Infinispan in persistence.xml.
For more information see: https://docs.jboss.org/author/display/ISPN/Configuring+cache+programmatically
In this tutorial I can see configuration of CacheManager like this (which can be confusing now):
EmbeddedCacheManager manager = new DefaultCacheManager("my-config-file.xml");
You can configure it entirely programmatically too without any input xml file, for example:
GlobalConfigurationBuilder global = new GlobalConfigurationBuilder();
global.transport().defaultTransport();
global.globalJmxStatistics().enable();
manager = new DefaultCacheManager(global.build());

Related

System property suffix for use in PropertyPlaceholderConfigurer

I having problems defining database properties in different environments. The properties file looks like this:
db.url-DEV=host1:port:con...
db.user-DEV=user1
db.url-PROD=host2:port:con...
db.user-PROD=user2
The suffixes (-DEV,-PROD) are set on server side using a system property. How may I configure my PropertyPlaceholderConfigurer to check for a system property to switch to the right configuration? There has to be a default value if a system property is not set.
I have tried something like this, but that did not work:
${#{'db.url'+${systemEnvironmentVar}}}
The PropertyConfigurer says 'property with name 'db.url'+${systemEnvironmentVar} not found.
Simply use nested expression, example from real project:
<property name="username" value="${${ENV_NAME}database.username}" />
<property name="password" value="${${ENV_NAME}database.password}" />
where ENV_NAME is system property. Note that you have to declare this variable in jvm parameters. You can also add default ENV_NAME to your properties:
ENV_NAME=dev
And configure propertyPlaceholder to override with system properties:
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
more about this here
But you can also use spring profiles
Create two properties files dev.properties and prod.properties with content
db.url=host1:port:con...
db.user=user1
This is common Spring pattern.

How to externalize links in a spring frame work

I have to externalize a couple of links in my application. The links have to be in a properties file which can be changed without the need for a build and deploy. I tried adding values in the server.properties of my jboss and using that variable in my controller but i am unable to get a value.
How do i go about this?
Using ResourceBundle
ResourceBundle bundle = ResourceBundle.getBundle("<myfile>");
String studentName = bundle.getString("<property-name>");
If you put the values in server.properties, then configure PropertyPlaceholderConfigurer in your applicationContext, something like this:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:server.properties</value>
</property>
</bean>
or the shorter Spring 3 version:
<context:property-placeholder location="classpath:server.properties"/>
and then just inject the values you need to your beans with
<bean id="someBean">
<property name="myProperty" value="${this.is.property.from.server.properties}" />
</bean>
or with #Value annotation like
#Value("${this.is.property.from.server.properties}")
private String myProperty;

Externarlize ehcache.xml to use properties from external properties file

I want to place property place holders in the ehcache.xml file (like ${}) so that the values can be replaced from external properties file (.properties) at runtime.
Something like:
ehcache.xml (in classpath):
<defaultCache
maxElementsInMemory="20000"
eternal="false"
timeToIdleSeconds="${default_TTI}"
timeToLiveSeconds="86400"
overflowToDisk="true"
... />
ehcache.properties (outside of the war/classpath):
...
default_TTI=21600
...
The purpose is to be able to change the cache configuration without the need to rebuild the app. Spring's PropertyPlaceHolder will only work with Spring bean definiton for ehcache which I dont want (need to keep the ehcache.xml as a file)
There are similar posts here but nothing got me to solution. I've been searching for a week now!!
Im using Spring 2.5.6,Hibernate 3.2.6 and Ehcache 2.4.6
Any help or idea is greatly Appriciated!!
Thanks a lot,
Tripti.
As a workaroud solution you can set property values to system scope (System.setProperty(...)). EhCahe uses these properties to resolve placeholders during parsing its configuration file.
I got the solution finally! Thanks to braveterry for pointing me in that direction.
I used this at context startup:
Inputstream = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
cacheManager = CacheManager.create(stream);
in conjuction with hibernate configuration:
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
This creates a singleton CacheManager from ehcache.xml file outside the context classpath. I was doing this same earlier but was accidently creating another CacheManager before this one using the default ehcache.xml in the classpath.
Thanks,
Tripti.
svaor, I follow what you mean, I define a bean like this:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System" />
<property name="targetMethod" value="setProperty" />
<property name="arguments">
<list>
<value>system.project_name</value>
<value>${system.project_name}</value>
</list>
</property>
</bean>
system.project_name define in system.properties file which locate in classpath
I also create a ehcache.xml in classpath, in ehcache.xml has the code like this:
<diskStore path="${java.io.tmpdir}/${system.project_name}/cache" />
but when I deploy my project, I find it cann't use the system.project_name define in system.properties, why?
If you just want to read the config in from disk at startup, you can do the following in EHCache 2.5:
InputStream fis =
new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try
{
CacheManager manager = new CacheManager(fis);
}
finally
{
fis.close();
}

using JDBC with persistence.xml

I am building a framework that manage the access to the database.
the framework getting tasks from the user and handle a connection pooling that manage the access to the database. the user just send me SQL commands.
One of the feature that i would like to support is working with JPA, in this case i will provide entity manager. in some cases i would like to provide JDBC access as well as JPA access. the arguments for the database are written in XML file.
so for JPA i need to write the property in persistence.xml so it will be not so smart to write again the same arguments for JDBC. do you know if i can get the arguments of the database from persistence.xml, do you know if there is a source code that do it. or should i parse persistence.xml by myself?
Do you know if I can get the arguments of the database from persistence.xml, do you know if there is a source code that do it. Or should I parse persistence.xml by myself?
I'm not aware of anything in the standard JPA API allowing to retrieve the driver class name, the jdbc url, the username and password.
Second problem, the keys for those properties are not standardized in JPA 1.0. For example, Hibernate will use:
<property name="hibernate.connection.driver_class" value=""/>
<property name="hibernate.connection.url" value=""/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
While EclipseLink will use:
<property name="eclipselink.jdbc.driver" value=""/>
<property name="eclipselink.jdbc.url" value=""/>
<property name="eclipselink.jdbc.user" value=""/>
<property name="eclipselink.jdbc.password" value=""/>
This may make the parsing fragile.
If this is an option, maybe you could use a properties file to store both the provider specific keys and the values (I'd recommend using the standardized JPA 2.0 properties as keys). For example:
# keys for JPA
javax.persistence.jdbc.driver = hibernate.connection.driver_class
javax.persistence.jdbc.url = hibernate.connection.url
javax.persistence.jdbc.user = hibernate.connection.username
javax.persistence.jdbc.password = hibernate.connection.password
# values common to JPA and JDBC
driver = org.h2.Driver
url = jdbc:h2:mem:test
username = scott
password = tiger
And use Persistence.createEntityManagerFactory(String, Map) and pass a Map that you'll feed with the properties from the file to create the EntityManagerFactory:
Map map = new HashMap();
map.put(...);
...
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPu", map);

Is it possible to have multiple PropertyPlaceHolderConfigurer in my applicationContext?

I need to load a specific applicationContext.xml file according to a given system property. This itself loads a file with the actual configuration. Therefore I need two PropertyPlaceHolderConfigurer, one which resolves the system param, and the other one within the actual configuration.
Any ideas how to do this?
Yes you can do more than one. Be sure to set ignoreUnresolvablePlaceholders so that the first will ignore any placeholders that it can't resolve.
<bean id="ppConfig1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath*:/my.properties</value>
</list>
</property>
</bean>
<bean id="ppConfig2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="locations">
<list>
<value>classpath*:/myOther.properties</value>
</list>
</property>
</bean>
Depending on your application, you should investigate systemPropertiesMode, it allows you to load properties from a file, but allow the system properties to override values in the property file if set.
Another solution is to use placeholderPrefix property of PropertyPlaceholderConfigurer. You specify it for the second (third, fourth...) configurer, and then prefix all your corresponding placeholders, thus there will be no conflict.
<bean id="mySecondConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:/myprops.properties"
p:placeholderPrefix="myprefix-"/>
<bean class="com.mycompany.MyClass" p:myprop="${myprefix-value.from.myprops}"/>
Beware -- there might be a bug related to multiple configurers. See http://jira.spring.io/browse/SPR-5719 for more details.
I'm unable to get multiple to work locally... but I'm not yet blaming anyone but myself.
On my own side, playing with PropertyPlaceholderConfigurer both properties :
order (should be lower for first accessed/parsed PPC)
ignoreUnresolvablePlaceholders ("false" for first accessed/parsed PPC, "true" for next one)
and also give 2 distinct id(s) to both PPC (to avoid one to be overwritten by the other)
works perfectly
Hope it helps
You can't do this directly, and this JIRA issue from Spring explains why (check the comment from Chris Beams for a detailed explanation):
https://jira.springsource.org/browse/SPR-6428
However, he does provide a workaround using Spring 3.1 or later, which is to use the PropertySourcesPlaceholderConfigurer class instead of PropertyPlaceholderConfigurer class.
You can download a Maven-based project that demonstrates the problem and the solution from the Spring framework issues github:
https://github.com/SpringSource/spring-framework-issues
Look for the issue number, SPR-6428, in the downloaded projects.
We have the following approach working:
<util:properties id="defaultProperties">
<prop key="stand.name">DEV</prop>
<prop key="host">localhost</prop>
</util:properties>
<context:property-placeholder
location="file:${app.properties.path:app.properties}"
properties-ref="defaultProperties"/>
System property app.properties.path can be used to override path to config file.
And application bundles some default values for placeholders that cannot be defined with defaults in common modules.
Just giving 2 distinct ids worked for me. I am using spring 3.0.4.
Hope that helps.
In case, you need to define two PPC's (like in my situation) and use them independently. By setting property placeholderPrefix, you can retrieve values from desired PPC. This will be handy when both set of PPC's properties has same keys, and if you don't use this the property of ppc2 will override ppc1.
Defining your xml:
<bean name="ppc1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="ref to your props1" />
<property name="placeholderPrefix" value="$prefix1-{" />
</bean>
<bean name="ppc2"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="ref to your props2" />
<property name="placeholderPrefix" value="$prefix2-{" />
</bean>
Retrieving during Run time:
#Value(value = "$prefix1-{name}")
private String myPropValue1;
#Value(value = "$prefix2-{name}")
private String myPropValue2;

Categories