ehcache error serializing key for distributed config with terracotta server - ehcache

I'm trying to configure terracotta server to work with a spring/mybatis application and I'm getting the following error. I'm not sure if that means the key itself or the value returned from the key could not be serialized. The caching worked fine as a local cache, but now has the issue trying to work with the server. I need a clue why this is not able to be serialized. Thanks.
So I got a clue from this How do you serialize a Spring Bean (spring 3) that it may be something to do with lack of session scope. These errors happen when I am starting up Tomcat and the first webpage is loading. I added implements java.io.Serializable to the Site bean class and that moved me past that error to the next bean that was getting called. After adding this to many beans I'm wondering if A, is this the right thing to do and will there be any side effects from forcing the implements java.io.Serializable on these spring classes? And B: is there a better way do this as I have many beans in this application?
SEVERE: Servlet.service() for servlet [ezreg] in context with path [] threw exception [Request processing failed; nested exception is net.sf.ehcache.CacheException: The value com.trifecta.src.ezreg.beans.Site#655ad5d5 for key getSiteByHostname127.0.0.1 is not Serializable. Consider using Element.getObjectValue()] with root cause
net.sf.ehcache.CacheException: The value com.trifecta.src.ezreg.beans.Site#655ad5d5 for key getSiteByHostname127.0.0.1 is not Serializable. Consider using Element.getObjectValue()
at net.sf.ehcache.Element.getValue(Element.java:326)
at net.sf.ehcache.ElementData.<init>(ElementData.java:35)
at net.sf.ehcache.EternalElementData.<init>(EternalElementData.java:19)
at org.terracotta.modules.ehcache.store.ValueModeHandlerSerialization.createElementData(ValueModeHandlerSerialization.java:48)
at org.terracotta.modules.ehcache.store.ClusteredStore.doPut(ClusteredStore.java:745)
at org.terracotta.modules.ehcache.store.ClusteredStore.putInternal(ClusteredStore.java:291)
at org.terracotta.modules.ehcache.store.ClusteredStore.put(ClusteredStore.java:263)
at org.terracotta.modules.ehcache.store.ClusteredSafeStore.put(ClusteredSafeStore.java:247)
at org.terracotta.modules.ehcache.store.nonstop.NonStopStoreWrapper.put(NonStopStoreWrapper.java:820)
at net.sf.ehcache.Cache.putInternal(Cache.java:1617)
at net.sf.ehcache.Cache.put(Cache.java:1543)
at net.sf.ehcache.Cache.put(Cache.java:1508)
at org.springframework.cache.ehcache.EhCacheCache.put(EhCacheCache.java:121)
at org.springframework.cache.interceptor.AbstractCacheInvoker.doPut(AbstractCacheInvoker.java:85)
at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:784)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:417)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy56.getSiteByHostname(Unknown Source)
at com.trifecta.src.ezreg.daos.SiteDaoImpl.getSiteByHostname(SiteDaoImpl.java:35)
doa method:
public Site getSiteByHostname(String hostname) {
return getSiteMapper().getSiteByHostname(hostname);
}
mapper method:
#Cacheable(cacheNames="siteCache", key="#root.methodName.concat(#root.args)")
Site getSiteByHostname(String hostname);
Site bean returned:
package com.trifecta.src.ezreg.beans;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
#XmlRootElement
public class Site {
public static String ADMIN_CURRENT_SITE = "adminCurrentSite";
public static String _CURRENT_SITE = "currentSite";
#XmlAttribute
private Long id;
#XmlAttribute
private String name;
#XmlAttribute
private String supportphonenumber;
#XmlElement
private SitePreference sitePreference;
#XmlElement
private SiteInterfaceDevice siteInterfaceDevice;
#XmlElement
private SitePdfFormat sitePdfFormat;
#XmlAttribute
private boolean ecum;
#XmlTransient
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#XmlTransient
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#XmlTransient
public SiteInterfaceDevice getSiteInterfaceDevice() {
return siteInterfaceDevice;
}
public void setSiteInterfaceDevice(SiteInterfaceDevice siteInterfaceDevice) {
this.siteInterfaceDevice = siteInterfaceDevice;
}
#XmlTransient
public SitePdfFormat getSitePdfFormat() {
return sitePdfFormat;
}
public void setSitePdfFormat(SitePdfFormat sitePdfFormat) {
this.sitePdfFormat = sitePdfFormat;
}
#XmlTransient
public SitePreference getSitePreference() {
return sitePreference;
}
public void setSitePreference(SitePreference sitePreference) {
this.sitePreference = sitePreference;
}
#XmlTransient
public String getSupportphonenumber() {
return supportphonenumber;
}
public void setSupportphonenumber(String supportphonenumber) {
this.supportphonenumber = supportphonenumber;
}
#XmlTransient
public boolean getEcum() {
return ecum;
}
public void setEcum(boolean ecum) {
this.ecum = ecum;
}
}
public class Site implements java.io.Serializable{
public static String ADMIN_CURRENT_SITE = "adminCurrentSite";
public static String _CURRENT_SITE = "currentSite";

When caching locally, you can have a setup using heap memory only and thus there is no requirement on your keys / values stored in the cache.
However the moment you move to clustering, or actually any other caching tier than heap, then your keys and values must implement Serializable as this is the only way we can store / ship over the wire your mappings.
And so clearly in your case the type com.trifecta.src.ezreg.beans.Site does not implement Serializable.
You have two options:
Update the type definition to implement Serializable and make sure it is true - that is it only has Serializable fields
Convert the non serializable value in one that can be serialized.
Note that with Ehcache 3 you would have the option of specifying a custom serializer for your type that would not limit you to Java serialization.

Related

RefreshScoped bean creation

I have configuration class like this with validation on name field.
#Component
#ConfigurationProperties("person")
#RefreshScope
#Validated
public class PersonConfiguration {
#NotBlank
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
#RestController
class MessageRestController {
#Autowired
private PersonConfiguration personConfig;
#RequestMapping("/message")
String getMessage() {
return personConfig.getName();
}
}
person:
name: aaaa
I was able to successfully start the spring boot application with the above details.
When i change the configuration to this
person:
name:
with an empty name field i don't see a binding exception on PersonConfiguration because of the empty name field right away.
I am seeing the binding exception only when i do a rest call like this localhost:8080/message which is accessing PersonConfiguration. It's like i have to wait till something accesses PersonConfiguration to see binding excpetion.
Is it how it should work?
Is there a way to throw the binding exception instantly when there are git config changes that are not valid, without waiting for something to access configuration

#value not able to read from application.properties in springboot

I am trying to read value from properties file using #value as follows.
#Value("${abc}")
private String abc;
public List<Record> fetchRecords(String label, String predicate) {
System.out.println(abc);
}
but value of abc is coming as null. Whereas when I try to print the same using #PostConstruct, I am getting the expected value.
#PostConstruct
public void postconstruct() {
System.out.println(abc);
}
Any lead why I am not able to get the value in fetchRecords() method?
For reference, here goes the code
#Component
public class AuditRecord {
private String subject;
private String predicate;
private String oldObject;
private String newObject;
private String readOnlyAuthInfo;
#Value("${registry.system.base}")
private String registrySystemContext;
public void record(DatabaseProvider provider) throws AuditFailedException {
System.out.println("---registrySystemContext value showing null here---"+registrySystemContext);
...
}
#PostConstruct
public void postconstruct() {
System.out.println("---registrySystemContext value showing here as expected---"+registrySystemContext);
}
}
The way I am calling is as follows:
#Component
public class RegistryDaoImpl implements RegistryDao {
...
private void addOrUpdateVertexAndEdge(Vertex v, Vertex dbVertex, GraphTraversalSource dbGraph, String methodOrigin){
...
AuditRecord record = new AuditRecord();
record
.subject(dbVertex.label())
.predicate(e.label())
.oldObject(null)
.newObject(existingV.label())
.record(databaseProvider);
}
}
P.S. registry.system.base is in application.yml.
You need to autowire AuditRecord and not use new directly. Only that way you will have your class in Spring's context.
We don't know your exact usage of the class but you might be interested in Spring's FactoryBean.

Make #ConfigurationProperties properties partially mandatory

Given the following simple (not nested) configuration properties class:
#ConfigurationProperties("env")
public class MyServiceProperties {
private String anyProperty;
private Boolean anyOther;
...
}
How can I make sure that anyProperty is mandatory, i.e. env.any-property must be set to startup the application? Is there any difference for nested configuration property classes?
You can perform all kind of validations.
#Validated
#ConfigurationProperties("env")
public class MyServiceProperties {
#NotNull
#Min(5)
private String anyProperty;
// this is for nested objects
#Valid
#NotNull
private FooNested fooNested;
public static class FooNested{
#NotNull
private String someVal;
}
}
You could also perform manual validation in setter
#Validated
#ConfigurationProperties("env")
public class MyServiceProperties {
private String anyProperty;
public void setAnyProperty(String anyProp){
// just an example
if(anyProp.lenght < 6){
throw new RuntimeException();
}
this.anyProperty = anyProp;
}
}

Spring Data REST Custom Resource URI works for String but not Long

I have a model:
public class MyModel {
#Id private Long id;
private Long externalId;
// Getters, setters
}
I'd like to use externalId as my resource identifier:
#Configuration
static class RepositoryEntityLookupConfig extends RepositoryRestConfigurerAdapter {
#Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration configuration) {
configuration
.withEntityLookup()
.forRepository(MyRepository.class, MyModel::getExternalId, MyRepository::findByExternalId);
}
}
If externalId is a String, this works fine. But since it's a number (Long)
public interface MyRepository extends JpaRepository<MyModel, Long> {
Optional<MyModel> findByExternalId(#Param("externalId") Long externalId);
}
when invoking: /myModels/1 I get:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at org.springframework.data.rest.core.config.EntityLookupConfiguration$RepositoriesEntityLookup.lookupEntity(EntityLookupConfiguration.java:213) ~[spring-data-rest-core-2.6.4.RELEASE.jar:na]
at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.invokeFindOne(UnwrappingRepositoryInvokerFactory.java:130) ~[spring-data-rest-core-2.6.4.RELEASE.jar:na]
at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:524) ~[spring-data-rest-webmvc-2.6.4.RELEASE.jar:na]
at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:335) ~[spring-data-rest-webmvc-2.6.4.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
...
A separate custom EntityLookupSupport<MyModel> component class works.
Am I missing something to get it working for Long using method references in my RepositoryRestConfigurerAdapter?
Try to add this to your RepositoryEntityLookupConfig class:
#Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(String.class, Long.class, Long::parseLong);
super.configureConversionService(conversionService);
}
Do you really need to set configuration by yourself ? You could try to use spring-boot auto-configuration by adding #RepositoryRestResource annotation
#RepositoryRestResource(collectionResourceRel = "myModels", path = "myModels")
public interface MyRepository extends JpaRepository<MyModel, Long> {
Optional<MyModel> findByExternalId(#Param("externalId") Long externalId);
}
Also add #Entity on your model class
#Entity
public class MyModel {
#Id
private Long id;
#Column(name = "EXTERNAL_ID")
// Column annotation is not required if you respect case-sensitive
private Long externalId;
// Getters, setters
}
Apparently, the default BackendIdConverter (see DefaultIdConverter) does nothing with ID conversion and on the other hand Spring Data Rest cannot use the repository ID type. So, you have to either convert it yourself or configure your custom ID converter bean, for example:
#Bean
public BackendIdConverter myModelBackendIdConverter() {
return new BackendIdConverter() {
#Override
public Serializable fromRequestId(final String id, final Class<?> entityType) {
return Optional.ofNullable(id).map(Long::parseLong).orElse(null);
}
#Override
public boolean supports(final Class<?> delimiter) {
return MyModel.class.isAssignableFrom(delimiter);
}
#Override
public String toRequestId(final Serializable id, final Class<?> entityType) {
return Optional.ofNullable(id).map(Object::toString).orElse(null);
}
};
}
See also:
BackendIdHandlerMethodArgumentResolver
#BackendId
The signature of the method you are trying to call seems to be:
forRepository(Class<R> type, Converter<T,ID> identifierMapping,
EntityLookupRegistrar.LookupRegistrar.Lookup<R,ID> lookup)
I don't see how MyModel::getExternalId can be doing the necessary conversion.
I would try something like the following:
#Configuration
static class RepositoryEntityLookupConfig extends RepositoryRestConfigurerAdapter {
#Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration configuration) {
configuration
.withEntityLookup()
.forRepository(MyRepository.class, Long::parseLong, MyRepository::findByExternalId);
}
}

How to use Java 8 Optional with Moxy and Jersey

Is it possible to use Jersey with Moxy to/from Json and Java 8 Optionals?
How to configure it?
You can declare following class:
public class OptionalAdapter<T> extends XmlAdapter<T, Optional<T>> {
#Override
public Optional<T> unmarshal(T value) throws Exception {
return Optional.ofNullable(value);
}
#Override
public T marshal(Optional<T> value) throws Exception {
return value.orElse(null);
}
}
And use like this:
#XmlRootElement
public class SampleRequest {
#XmlElement(type = Integer.class)
#XmlJavaTypeAdapter(value = OptionalAdapter.class)
private Optional<Integer> id;
#XmlElement(type = String.class)
#XmlJavaTypeAdapter(value = OptionalAdapter.class)
private Optional<String> text;
/* ... */
}
Or declare in package-info.java and remove #XmlJavaTypeAdapter from POJOs:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlJavaTypeAdapters({
#XmlJavaTypeAdapter(type = Optional.class, value = OptionalAdapter.class)
})
But here are some drawbacks:
Adapter above can only work with simple types like Integer, String, etc. that can be parsed by MOXY by default.
You have to specify #XmlElement(type = Integer.class) explicitly to tell the parser type are working with, otherwise null values would be passed to adapter's unmarshal method.
You miss the opportunity of using adapters for custom types, e.g. custom adapter for java.util.Date class based on some date format string. To overcome this you'll need to create adapter something like class OptionalDateAdapter<String> extends XmlAdapter<String, Optional<Date>>.
Also using Optional on field is not recommended, see this discussion for details.
Taking into account all the above, I would suggest just using Optional as return type for your POJOs:
#XmlRootElement
public class SampleRequest {
#XmlElement
private Integer id;
public Optional<Integer> getId() {
return Optional.ofNullable(id);
}
public void setId(Integer id) {
this.id = id;
}
}

Resources