Eclipselink + Axon

Hello,
Has somebody working configuration of Axon + Eclipselink (+ spring boot)?

`

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jpaSagaRepository’ defined in class path resource [com/example/infrastructure/config/AxonConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.saga.repository.jpa.JpaSagaRepository]: Factory method ‘jpaSagaRepository’ threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy63 cannot be cast to org.eclipse.persistence.internal.jpa.QueryImpl
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at com.example.RestApplication.main(RestApplication.java:20) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.saga.repository.jpa.JpaSagaRepository]: Factory method ‘jpaSagaRepository’ threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy63 cannot be cast to org.eclipse.persistence.internal.jpa.QueryImpl
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
… 18 common frames omitted
Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy63 cannot be cast to org.eclipse.persistence.internal.jpa.QueryImpl
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.addNamedQuery(EntityManagerFactoryImpl.java:625) ~[eclipselink-2.6.2.jar:2.6.2.v20151217-774c696]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:388) ~[spring-orm-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:541) ~[spring-orm-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at com.sun.proxy.$Proxy51.addNamedQuery(Unknown Source) ~[na:na]
at org.axonframework.saga.repository.jpa.JpaSagaRepository.(JpaSagaRepository.java:95) ~[axon-core-2.4.3.jar:2.4.3]
at com.example.infrastructure.config.AxonConfiguration.jpaSagaRepository(AxonConfiguration.java:215) ~[infrastructure.jar:na]
at com.example.infrastructure.config.AxonConfiguration$$EnhancerBySpringCGLIB$$5b9c1fc2.CGLIB$jpaSagaRepository$21() ~[infrastructure.jar:na]
at com.example.infrastructure.config.AxonConfiguration$$EnhancerBySpringCGLIB$$5b9c1fc2$$FastClassBySpringCGLIB$$f7f07c42.invoke() ~[infrastructure.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:355) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at com.example.infrastructure.config.AxonConfiguration$$EnhancerBySpringCGLIB$$5b9c1fc2.jpaSagaRepository() ~[infrastructure.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
… 19 common frames omitted

`

`

package com.example.infrastructure.config;

import com.example.infrastructure.axon.audit.CommandMetadataAuditDataProvider;
import com.example.infrastructure.axon.interceptor.CommandMetaDataInterceptor;
import org.axonframework.auditing.AuditingInterceptor;
import org.axonframework.commandhandling.AsynchronousCommandBus;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.interceptors.BeanValidationInterceptor;
import org.axonframework.commandhandling.interceptors.LoggingInterceptor;
import org.axonframework.common.jpa.ContainerManagedEntityManagerProvider;
import org.axonframework.common.jpa.EntityManagerProvider;
import org.axonframework.eventhandling.ClusteringEventBus;
import org.axonframework.eventhandling.DefaultClusterSelector;
import org.axonframework.eventhandling.annotation.AnnotationEventListenerBeanPostProcessor;
import org.axonframework.eventhandling.async.AsynchronousCluster;
import org.axonframework.eventhandling.async.SequentialPerAggregatePolicy;
import org.axonframework.eventsourcing.EventCountSnapshotterTrigger;
import org.axonframework.eventsourcing.SnapshotterTrigger;
import org.axonframework.eventsourcing.SpringAggregateSnapshotter;
import org.axonframework.eventstore.jpa.JpaEventStore;
import org.axonframework.saga.GenericSagaFactory;
import org.axonframework.saga.repository.jpa.JpaSagaRepository;
import org.axonframework.saga.spring.SpringResourceInjector;
import org.axonframework.serializer.xml.XStreamSerializer;
import org.axonframework.unitofwork.SpringTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import java.util.Arrays;
import java.util.concurrent.ScheduledThreadPoolExecutor;

@Configuration
public class AxonConfiguration {

@Autowired
private JpaTransactionManager transactionManager;

@Autowired
private XStreamSerializer xStreamSerializer;

@Bean
public EntityManagerProvider entityManagerProvider() {
return new ContainerManagedEntityManagerProvider();
}

@Bean
public SpringTransactionManager axonTransactionManager() {
return new SpringTransactionManager(transactionManager);
}

@Bean
public ScheduledThreadPoolExecutor axonExecutor() {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
return executor;
}

//////////////
/// EVENTS ///
//////////////

@Bean
public SequentialPerAggregatePolicy sequentialPerAggregatePolicy() {
return new SequentialPerAggregatePolicy();
}

@Bean
public AsynchronousCluster asynchronousCluster() {
AsynchronousCluster asynchronousCluster = new AsynchronousCluster(“axon-async-cluster”, axonExecutor(), axonTransactionManager(), sequentialPerAggregatePolicy());
return asynchronousCluster;
}

@Bean
public DefaultClusterSelector defaultClusterSelector() {
return new DefaultClusterSelector(asynchronousCluster());
}

@Bean
public ClusteringEventBus eventBus() {
ClusteringEventBus eventBus = new ClusteringEventBus(defaultClusterSelector());
return eventBus;
}

@Bean
public AnnotationEventListenerBeanPostProcessor annotationEventListenerBeanPostProcessor() {
AnnotationEventListenerBeanPostProcessor annotationEventListenerBeanPostProcessor = new AnnotationEventListenerBeanPostProcessor();
annotationEventListenerBeanPostProcessor.setEventBus(eventBus());
return annotationEventListenerBeanPostProcessor;
}

@Bean
public JpaEventStore eventStore() {
// @TODO check serializer json/xstream
return new JpaEventStore(entityManagerProvider(), xStreamSerializer);
}

////////////////
/// COMMANDS ///
////////////////

@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}

@Bean
public BeanValidationInterceptor beanValidationInterceptor() {
return new BeanValidationInterceptor(localValidatorFactoryBean());
}

@Bean
public CommandMetadataAuditDataProvider commandMetadataAuditDataProvider() {
return new CommandMetadataAuditDataProvider();
}

@Bean
public AuditingInterceptor auditingInterceptor() {
AuditingInterceptor auditingInterceptor = new AuditingInterceptor();
auditingInterceptor.setAuditDataProvider(commandMetadataAuditDataProvider());
// TODO check auditingInterceptor.setAuditLogger();
return auditingInterceptor;
}

@Bean
public CommandMetaDataInterceptor commandMetaDataInterceptor() {
return new CommandMetaDataInterceptor();
}

@Bean
public LoggingInterceptor loggingInterceptor() {
return new LoggingInterceptor();
}

@Bean
public CommandBus commandBus() {
AsynchronousCommandBus commandBus = new AsynchronousCommandBus(axonExecutor());
commandBus.setTransactionManager(axonTransactionManager());
commandBus.setDispatchInterceptors(Arrays.asList(beanValidationInterceptor(), commandMetaDataInterceptor()));
commandBus.setHandlerInterceptors(Arrays.asList(auditingInterceptor(), loggingInterceptor()));
return commandBus;
}

/*
@Bean
public static CommandGatewayFactoryBean commandGatewayFactoryBean(CommandBus commandBus) {
CommandGatewayFactoryBean factoryBean = new CommandGatewayFactoryBean();
factoryBean.setCommandBus(commandBus);
factoryBean.setGatewayInterface(CommandGateway.class);
return factoryBean;
}
*/

/*
@Bean
public CommandGateway commandGateway(GatewayProxyFactory gatewayProxyFactory){
return gatewayProxyFactory.createGateway(CommandGateway.class);
}
*/

////////////////
/// SNAPSHOT ///
////////////////

@Bean
public JpaEventStore snapshotEventStore() {
// @TODO check serializer json/xstream
JpaEventStore jpaEventStore = new JpaEventStore(entityManagerProvider(), xStreamSerializer);
// TODO check jpaEventStore.setDataSource();
// jpaEventStore.setDataSource(dataSource);
return jpaEventStore;
}

@Bean
public SpringAggregateSnapshotter snapshotter() {
SpringAggregateSnapshotter snapshotter = new SpringAggregateSnapshotter();
snapshotter.setEventStore(snapshotEventStore());
snapshotter.setExecutor(axonExecutor());
return snapshotter;
}

@Bean
public SnapshotterTrigger snapshotterTrigger() {
EventCountSnapshotterTrigger snapshotterTrigger = new EventCountSnapshotterTrigger();
snapshotterTrigger.setSnapshotter(snapshotter());
snapshotterTrigger.setTrigger(10);
return snapshotterTrigger;
}

////////////
/// SAGA ///
////////////

@Bean
public SpringResourceInjector springResourceInjector() {
return new SpringResourceInjector();
}

@Bean
public GenericSagaFactory sagaFactory() {
GenericSagaFactory sagaFactory = new GenericSagaFactory();
sagaFactory.setResourceInjector(springResourceInjector());
return sagaFactory;
}

@Bean
public JpaSagaRepository jpaSagaRepository() {
JpaSagaRepository jpaSagaRepository = new JpaSagaRepository(entityManagerProvider());
jpaSagaRepository.setResourceInjector(springResourceInjector());
jpaSagaRepository.setUseExplicitFlush(true);
// TODO check jpaSagaRepository.setSerializer();
jpaSagaRepository.setSerializer(xStreamSerializer);
return jpaSagaRepository;
}
}

`

`

package com.example.infrastructure.config;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EntityScan(basePackages = {“org.axonframework”})
public class PersistenceConfiguration extends JpaBaseConfiguration {

@Bean
public JpaTransactionManager transactionManager() {
return new JpaTransactionManager();
}

@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
EclipseLinkJpaVendorAdapter adapter = new EclipseLinkJpaVendorAdapter();
return adapter;
}

@Override
protected Map<String, Object> getVendorProperties() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(PersistenceUnitProperties.WEAVING, detectWeavingMode());
return map;
}

private String detectWeavingMode() {
return InstrumentationLoadTimeWeaver.isInstrumentationAvailable() ? “true” : “static”;
}

}

`

Hi,

to be honest, I don’t think this is Axon related at all. There is a classcast exception in a Proxy (most likely your EntityManager) that cannot be cast to QueryImpl.
Make sure the JPA version and Eclipselink versions you use are compatible.

Cheers,

Allard

Hi Allard,
I don’t know if that’s issue of EclipseLink. It was working in version 2.4.2 now in version 2.4.3 I have this issues.
Look here on my testing project. It’s now configured with 2.4.3 if you invoke
mvn clean install spring-boot:run
then exceptions are raised. This can be resolved with annotation@EntityScan("org.axonframework")
in some configuration class (perhaps PersistenceConfiguration is the best one). But even after this fix, application is not started because of proxy issue.

If i downgrade axon version to 2.4.2 then I don’t need @EntityScan and also everything works.

Can you please look at this?

Many thanks

Lukas

Ok I’ve looked into the source code and I can see that there’s change only in constructor.
I’m going to investigate further this issue and will update this thread if I find something.

So I’ve created ticket in spring. We will see in next release if this works. Currently it’s not possible to use eclipselink with axon.

So apparantly, it’s actually a bug in Eclipselink, which has been solved (/ worked around) in Spring… Nice.