Ehcache statistics - caching

I would like to see all statistics for the Ehcache when I have running server.
In the documentation I have found objects such as "StatisticsGateway" and "SampledCache". I'm using ehcache 2.9.
By using StatisticsGateway gets incomplete statistics. When using the SampledCache object I get more statistics, but nowhere is described in some way to retrieve the object.
For example, getting the StatisticsGateway object is as follow:
Cache cache = cacheManager.getCache("name");
StatisticsGateway statistic = cache.getStatistics();
statistic.cacheHitCount() etc.
How to get the SampledCache object?
Thanks in advance!

Late answer :) it may help some one else.
You can use jconsole.exe from your java/bin directory. The Jconsole can show you the statistics.
You may need to add the JMX support to see the statistics in Jconsole
<!-- JMX for Ehcache -->
<bean id="managementService" class="net.sf.ehcache.management.ManagementService"
init-method="init" destroy-method="dispose">
<constructor-arg ref="ehcache" />
<constructor-arg ref="mbeanServer" />
<constructor-arg index="2" value="true" />
<constructor-arg index="3" value="true" />
<constructor-arg index="4" value="true" />
<constructor-arg index="5" value="true" />
</bean>

SampleCache acts as a decorator object. Basically you create an instance of SampledCache and pass a Cache instance as a backing cache. The backing Cache is the cache for which you need stats, in your case the cache instance. Something like
SampledCache sampledCache = new SampledCache(cache);
You can call methods on sampledCache to get desired stats. Created a simple example here http://www.ashishpaliwal.com/blog/2015/01/using-ehcache-sampledcache/

Related

How Spring manages map-merge='true' in case of same key in parent map?

I have following 2 configs :
config-1.xml
---- import config-2.xml -----
<bean id="map1" class="java.util.HashMap" parent="map2">
<constructor-arg>
<map merge="true">
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
</map>
</constructor-arg>
</bean>
config-2.xml
<bean id="map2" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="key1" value="new_value1"/>
<entry key="key3" value="value3"/>
</map>
</constructor-arg>
</bean>
How spring manages this merge ?
I will load bean through config-1.xml ( It has config-2.xml as import).
So what I want is key1=new_value1
Please note that I cant touch config-1.xml as its used by other code so I should load new value via config-2.xml which is specific to my code.
Wherever I refer map1 , it should have following for my code :
key1=new_value1
key2=value2
key3=value3
Looking at the Spring docs for Bean Definition Inheritance, it seems to me to pretty clearly say that anything specified in both child and parent will cause the child definition to override the parent definition:
https://docs.spring.io/spring-framework/docs/3.0.0.M4/reference/html/ch03s07.html
Maybe it could be a little more clear, but what the docs say seems to confirm what you're saying and what the testing you've cone confirms. Take this example:
<bean id="inheritedTestBean" abstract="true"
class="org.springframework.beans.TestBean">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithDifferentClass"
class="org.springframework.beans.DerivedTestBean"
parent="inheritedTestBean" init-method="initialize">
<property name="name" value="override"/>
<!-- the age property value of 1 will be inherited from parent -->
</bean>
The fact that nothing is said about what value the name property will take, but its value for the example is chosen as override, seems to pretty clearly suggest that override will be the resulting value of the child's name property. Further, the fact that it is mentioned explicitly that the age property will be inherited from the parent strongly suggests that the name property will not be, meaning that it will instead be overridden by the child definition. This seems like nothing but obvious behavior to me.

How to define a PropertyPlaceholderConfigurer local to a specific bean?

I've been using org.springframework.beans.factory.config.PropertyPlaceholderConfigurer and in my experience ("citation needed" LOL) it sets the property values globally.
Is there a way to specify different PropertyPlaceholderConfigurer instances for different beans within the same application context xml?
My current code is similar to
<bean id="a" class="X">
<property name="foo" value="bar"/>
<property name="many" value="more"/>
</bean>
<bean id="b" class="X">
<property name="foo" value="baz"/>
<property name="number_of_properties" value="a zillion"/>
</bean>
I would like to do something like (pseudo-code below):
<bean id="a" class="X">
... parse the contents of "a.properties" here ...
</bean>
<bean id="b" class="X">
... parse the contents of "b.properties" here ...
</bean>
The above is non-working pseudo code to illustrate the concept; the point being, I want a different properties file to feed each bean.
WHY?
I want to have those specific properties in separate properties file and not in XML.
I think the following link can br helpful to you.
Reference Link
where #Value("${my.property.name}") annotation is used to bind the property file to a variable of type Properties which will reside in your bean class where you intend to use that properties file.
and you can define multiplte proprtiesplaceholder as below:
<bean id="myProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:my.properties</value>
</list>
</property>
</bean>
and use the id as reference in your bean variable to initialize properties file to the bean.
And it will be handy to include with placeholder bean.
Kindly refer Importance of Unresolvable Placeholder link for detailed info regarding its usage.
Hope this was helpful.

How to avoid hardcoded names in DelimitedLineTokenizer names?

I am using DelimitedLineTokenizer to read from a txt file using FlatFileItemReader. However, is there a way to avoid hardcoding the "names" property of the fields ? Instead can we use any bean ?
<bean id="employeeReader" class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<property name="resource" value="#{jobParameters['input.file.name']}" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="empId,empName,empAge" />
</bean>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="targetType" value="com.example.Employee" />
</bean>
</property>
</bean>
</property>
</bean>
Currently there is not because the result of the LineTokenizer's work is a FieldSet. A FieldSet is like a ResultSet for a file. So just like in a ResultSet, we need to reference each "column" by something which in this case is the name. The LineTokenizer has no insight into where the data is ending up (what the object looks like) so we have no way to introspect it. If you wanted to take a more dynamic approach, you'd want to implement your own LineMapper that combines the two functions together allowing for that type of introspection.
As always, pull requests are appreciated!
In addition to Michael Minella's answer, here's what you can do :
You can use a value such as #{jobParameter['key']}, #{jobExecutionContext['key']} or #{stepExecutionContext['key']} in the <property name="names"> tag.
This means that you can have a step or a listener which does your business logic and save the result at any time in the ExecutionContext :
stepExecution.getJobExecution().getExecutionContext().put(key, value);
Keep in mind though, that the field names of a DelimitedLineTokenizer needs a String (well not really, but close enough), not a bean.

Bean configuration for Apache Extras Cassandra-JDBC

I looked around their site http://code.google.com/a/apache-extras.org/p/cassandra-jdbc/ but couldn't find any documentation for configuring a data source bean such as
<spring:bean id="mySqlDataSource"
class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource">
<spring:property name="url"
value="jdbc:mysql://${mysql.host}:${mysql.port}/${mysql.database}?user=${mysql.user}&password=${mysql.pwd}" />
</spring:bean>
Is there any documentation anywhere or does anyone know how to write one?
The CassandraDataSource doesn't seem to have a default constructor, so you'll need to pass everything in:
<spring:bean id="cassDataSource"
class="org.apache.cassandra.cql.jdbc.CassandraDataSource">
<constructor-arg value="host">
<constructor-arg type="int" value="port">
<constructor-arg value="keyspace">
<constructor-arg value="user">
<constructor-arg value="password">
<constructor-arg value="version">
</spring:bean>
(I haven't tested this, but it's basically what you'd need to do).
I found this on Google, and the info is slightly out of date, so I thought I'd update it. The arguments have changed. The best way I found to figure out what arguments it's expecting is to use the source, which you can get off of GitHub using an address like:
https://code.google.com/a/apache-extras.org/p/cassandra-jdbc/source/browse/src/main/java/org/apache/cassandra/cql/jdbc/CassandraDataSource.java?name=v1.2.5
There's a selector on that page where you can pick the version number you downloaded (that link brings you to 1.2.5). You need to satisfy all the constructor arguments.

Question on a specific use case on spring <util:map>

I'm just trying to explore one use case of using object as a value in a spring map. Here's my example
<util:map id="someSourceMap" map-class="java.util.HashMap">
<entry key="source1" value="testLine"/>
<entry key="source2" value="testLine2"/>
</util:map>
<bean id="testLine1" class="com.test.ProductLineMetadata" scope="prototype">
<constructor-arg value="PRODUCT_LINE_1"></constructor-arg>
<constructor-arg value="TYPE_1"></constructor-arg>
</bean>
<bean id="testLine2" class="com.test.ProductLineMetadata"scope="prototype">
<constructor-arg value="PRODUCT_LINE_2"></constructor-arg>
<constructor-arg value="TYPE_2"></constructor-arg>
</bean>
What I'm trying to achieve is to create a map in which the value will be a new instance of ProductLineMetadata object with different parameters set through constructor argument. I don't want to create a separate bean entry for each key with the desired constructor values. Is there a better way of doing this by somehow specifying the parameters inside the map declaration itself?
Any pointer will be highly appreciated.
Thanks
You mean something like this?
<util:map id="someSourceMap" map-class="java.util.HashMap">
<entry key="source1">
<bean class="com.test.ProductLineMetadata">
<constructor-arg value="PRODUCT_LINE_1"/>
<constructor-arg value="TYPE_1"/>
</bean>
</entry>
<entry key="source2">
<bean class="com.test.ProductLineMetadata">
<constructor-arg value="PRODUCT_LINE_2"/>
<constructor-arg value="TYPE_2"/>
</bean>
</entry>
</util:map>
If your testLines are just test data rather than regular beans you may use more lightweight approach to declare them, for example, Spring Expression Language (since Spring 3):
<util:map id="someSourceMap" map-class="java.util.HashMap">
<entry key="source1"
value="#{new com.test.ProductLineMetadata('PRODUCT_LINE_1', 'TYPE_1')}"/>
<entry key="source2"
value="#{new com.test.ProductLineMetadata('PRODUCT_LINE_2', 'TYPE_2')}"/>
</util:map>

Resources