Jersey and Spring profiles - spring

I should to create Jersey endpoint that will be available only for some spring profiles. I tried to do it in following way:
<beans profile="teamcity">
<bean id="impactRadiusEndpoint" class="com.jingit.api.service.ImpactRadiusEndpoint">
<constructor-arg ref="impactRadiusQueueProcessor"/>
<constructor-arg ref="messagesReceiver"/>
</bean>
</beans>
But when I start up application with some other profiles and call this endpoint, Jersey ignores spring profiles and trying to start up. I find following output in logs:
SEVERE: Missing dependency for constructor public com.jingit.api.service.ImpactRadiusEndpoint(com.jingit.commons.queue.service.QueueProcessor,com.jingit.commons.queue.service.QueueMessagesReceiver) at parameter index 0
SEVERE: Missing dependency for constructor public com.jingit.api.service.ImpactRadiusEndpoint(com.jingit.commons.queue.service.QueueProcessor,com.jingit.commons.queue.service.QueueMessagesReceiver) at parameter index 1
Also this issue brokes other endpoints. Does anybody have a idea how to fix it?

Related

Xml configuration for JdbcChannelMessageStore in a Spring Boot project with Spring Data JPA

I am very new to Spring Integration and would like to use a queue channel which is backed by a JdbcChannelMessageStore.
As our project uses Spring Boot with Spring Data JPA, I would like to have an integration-context.xml configuration where the existing data base connection is reused. However I struggle to make it work.
Unfortunately I cannot find any example projects where JdbcChannelMessageStore is used. Could anyone provide some good example implementations for this?
Thanks a lot in advance.
P.S.: Here is my last integration-context.xml version:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<int:channel id="outgoingChannel">
<int:queue message-store="outgoingMessageChannelStore"/>
</int:channel>
<bean id="dp"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${spring.datasource.password}" />
<property name="username" value="${spring.datasource.username}" />
<property name="password" value="${spring.datasource.password}" />
</bean>
<bean id="outgoingMessageChannelStore" class="org.springframework.integration.jdbc.store.JdbcChannelMessageStore">
<property name="dataSource" ref="dp"/>
<property name="channelMessageStoreQueryProvider" ref="jdbcChannelMessageStoreQueryProvider"/>
<property name="region" value="TX_TIMEOUT"/>
<property name="usingIdCache" value="true"/>
</bean>
<bean id="jdbcChannelMessageStoreQueryProvider" class="org.springframework.integration.jdbc.store.channel.OracleChannelMessageStoreQueryProvider" />
<int:transaction-synchronization-factory id="jdbcChannelMessageStoreFactory">
<int:after-commit expression="#jdbcChannelMessageStore.removeFromIdCache(headers.id.toString())" />
<int:after-rollback expression="#jdbcChannelMessageStore.removeFromIdCache(headers.id.toString())" />
</int:transaction-synchronization-factory>
</beans>
With this I am getting the following Exception at startup:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
...
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Well, since you say that you also use Spring Boot, you probably miss the fact that it auto-configure a DataSource bean for us. Having that dp bean in your config it just neglects that auto-configuration and tries to apply it everywhere you need DataSource, like that Hibernate auto-configuration.
What you really need is exactly opposite - you need to reuse an auto-configured DataSource for this Spring Integration config. Of course, if your JdbcChannelMessageStore is going to rely on the same data base as menitoned Spring Data JPA.
So, what you need is just remove that dp bean definition and use a dataSource name for the <property name="dataSource" in the outgoingMessageChannelStore bean definition.
Some remarks:
We don't need usingIdCache with Oracle and therefore no need in that jdbcChannelMessageStoreFactory to deal with the cache. And that even covered in JavaDocs:
* <p>If using the provided
* {#link org.springframework.integration.jdbc.store.channel.OracleChannelMessageStoreQueryProvider},
* don't set {#link #usingIdCache}
* to true, as the Oracle query will ignore locked rows.</p>
Try to configure Spring Integration with Java & Annotation Configuration (or even Java DSL). This way you won't be tied with a bean name (like that dataSource) and just will have a bean method argument injection for plain DataSource type and Spring container will inject for you an auto-configured bean.

Another CacheManager with same name 'cacheManager' already exists in the same VM

I receive this error when starting tomcat with ehcache and Spring.
Another CacheManager with same name 'cacheManager' already exists in the same VM. Please
provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same
CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
Spring 3.1
ehcache 2.9.0
No test context using this.
This is a web JSF app.
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true"
name="ehcacheManager"
>
....
</ehcache>
cache-context.xml
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" scope="singleton">
<property name="shared" value="true"/>
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
I do have and old dependency to hibernate-ehcache, that unfortunately I can not delete. Could this be the issue?
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>3.5.0-Final</version>
</dependency>
Any suggestions?
Thank you!
Some solutions are discussed here here
and you might need to provide a ehcache.xml or set the cache name otherwise as you can see here
Edit : ApplicationContext loaded twice
If you hit the breakpoint, go up the stacktrace and you may discover why spring is loading the context twice.
There are two possiblites.
someother application installed on the semesever with same cache name
Application may deployed twice when you start the server due to ContextLoaderListener. So remove ContextLoaderListener from your web.xml.
Following will solve the problem:
hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
hibernate.cache.provider_class = net.sf.ehcache.hibernate.SingletonEhCacheProvider

Spring Lazy-init not working with #Resource injection

I have an interface with real and mock implementation. for obvious reasons the mock implementation is not in the production classpath.
I inject the bean using:
#Resource (name="${myClient}")
I am using Spring MVC and injecting this into a #Controller.
In external configuration I set the actual bean name to use and bind it to 'myClient' parameter. The binding works and it tries to load the real implementation but also fails on ClassNotFound on my mock although marked as lazy-init=true.
I am using Spring 4.0.0.
I know this is expected when suing #Autowire, but with #Resource I don't expect it to try and instantiate all beans in spring xml.
Any ideas wha't going on?
Here is the stack trace:
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MyMock] for bean with name 'myClientMock' defined in URL [file:/C:/myProject/target/classes/META-INF/springContext.xml]; nested exception is java.lang.ClassNotFoundException: MyMock1
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1327)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1396)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:382)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:361)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:347)
at org.springframework.context.support.AbstractApplicationContext.getBeanNamesForType(AbstractApplicationContext.java:1051)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:105)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:89)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:163)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
I dont know if you´re using Spring profiles, but it´s what we use here to initialize different beans per environment.
From Tomcat we spcecify which profile use, and in Spring we have configure something like this.
<beans profile="deployed-local">
<util:properties id="propertyConfigurer" location="classpath:app.deployed-performance.properties"/>
<context:property-placeholder location="classpath:app.deployed-performance.properties,classpath:app.constants.properties"/>
<import resource="spring/jdbc-config-test.xml"/>
<import resource="spring/contacts-config-deployed.xml"/>
<import resource="spring/security-config-local.xml"/>
<import resource="spring/clamAV-service.xml"/>
<import resource="spring/document-service.xml"/>
</beans>
<beans profile="deployed-prod">
<import resource="spring/jdbc-config.xml"/>
<import resource="spring/contacts-config-deployed.xml"/>
<import resource="spring/security-config.xml"/>
<import resource="spring/clamAV-service.xml"/>
<import resource="spring/document-service.xml"/>
</beans>

Unit Testing based on JNDI , ejb and spring

In my application I am injecting some of services based on EJB with use of Spring IOC through JndiObjectFactoryBean like below mentioned so during run the junit I am getting this exception "java.lang.IllegalArgumentException: This JNDI operation is not implemented by the JNDI provider."
Could some please let me know how I'll configure for Junit.
<bean id="xxxMenuItemService" class="xxxMenuItemServiceyyy">
<property name="xxxMenuItemDelegator" ref="xxxMenuItemDelegator" />
</bean>
<bean id="approveMenuItemServiceRemote"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="ejb/XXXXXXXX" />
have a look at the SimpleNamingContextBuilder from org.springframework.mock as it provides a full context builder where you can bind mock or other objects for use by Spring's JNDI lookup.
One thing to do though is to make sure you build the SimpleNamingContextBuilder in the static #BeforeClass of JUnit 4. this means that it is all initialized and waiting before the Spring Application Context is started and you won't have any JNDI lookup failures.

Spring does not autowire beans with JAX-WS webservice end points

I am trying to consume a JAX-WS webservice written by me. I always get nullPointerException for spring autowired annotated beans. However, everything works fine within serverSide over web, but accessing beans through JAX-WS webservice.
I have tried by extending SpringBeanAutowiringSupport, but still no luck. How can I do this.
regards, Rohit
I had no experience extending SpringBeanAutowiringSupport but had successfuly used this approach:
Annotate webService class such a way :
#Component("yourWebService")
#WebService(endpointInterface ="your.package.YourServicePort")
Create new spring-context xml for webService and define JAX-WS endpoint :
<jaxws:endpoint
id="yourServiceEndpoint"
implementor="#yourWebService"
address="${yourWebService.wsdl.url}"> //load url from properties file
</jaxws:endpoint>
I suppose you know how to use props in spring, but will explain just in case. You should also create yourWebService.properties file and define it in spring context to use this construction ${yourWebService.wsdl.url} :
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>yourWebService.properties</value>
</list>
</property>
Using this approach I had successfuly used JAX with Spring

Resources