How to set primary rabbit template and rabbit connection factory - spring-boot

I am facing some errors in a spring boot project where I am using spring integration to connect to RabbitMQ. I am doing the configuration for RabbitMQ in XML files like this:
<!-- RabbitMQ configuration -->
<rabbit:connection-factory
id="rabbitConnectionFactory_2"
host="${queuing.events.host}"
port="${queuing.events.port}"
username="${queuing.events.username}"
password="${queuing.events.password}"
virtual-host="${queuing.events.virtual-host}"
publisher-returns="true"/>
<rabbit:template id="amqpTemplate_2" connection-factory="rabbitConnectionFactory_2" />
<rabbit:admin id="rabbitAdmin_2" connection-factory="rabbitConnectionFactory_2"/>
<rabbit:listener-container connection-factory="rabbitConnectionFactory_2" auto-startup="true" requeue-rejected="false" />
<bean id="rabbitListenerContainerFactory_2" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="rabbitConnectionFactory_2"/>
</bean>
<!-- -->
But I am creating two of each component. How to set the primaries ones?
Now the problem comes here, I was using this version for spring cloud:
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
And everything was working fine, but if I update the version to:
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
This error is coming:
Description:
Parameter 0 of method rabbitSender in org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRabbitSenderConfiguration required a single bean, but 2 were found:
- rabbitConnectionFactory: defined in null
- rabbitConnectionFactory_2: defined in null
Action:
Consider marking one of the beans as #Primary, updating the consumer to accept multiple beans, or using #Qualifier to identify the bean that should be consumed
And the error comes because of this dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
If I remove this dependency the error is not coming.
You can find an example project to reproduce this scenario. In the pom file you'll see this:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version> <!-- it works with this version-->
<!-- <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> --> <!-- doesn't work with this version-->
</properties>
<dependencies>
<!-- SPRING BOOT -->
<!-- it fails because of this dependency in that we are using Edgware.RELEASE version -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
...
https://github.com/fjmpaez911/spring-integration-zipkin-cloud
So I need to know how to set a primary configuration for RabbitMQ and in addition I think that could be an issue because this error only comes if I use this version Edgware.RELEASE
Am I missing something?

fran, in Edgware.RELEASE the <artifactId>spring-cloud-sleuth-zipkin</artifactId> will resolve Zipkin 2 dependencies try using <artifactId> spring-cloud-starter-zipkin-legacy</artifactId> instead

To define the primary connection factory for RabbitMQ in XML files, you can do something like this:
<!-- Here the primary connection -->
<bean id="rabbitConnectionFactory" primary="true" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<constructor-arg value="${spring.rabbitmq.host}"/>
<property name="username" value="${spring.rabbitmq.username}"/>
<property name="password" value="${spring.rabbitmq.password}"/>
<property name="virtualHost" value="${spring.rabbitmq.virtual-host}"/>
</bean>
<!-- RabbitMQ configuration -->
<rabbit:connection-factory
id="rabbitConnectionFactory_2"
host="${queuing.events.host}"
port="${queuing.events.port}"
username="${queuing.events.username}"
password="${queuing.events.password}"
virtual-host="${queuing.events.virtual-host}"
publisher-returns="true"/>
<rabbit:template id="amqpTemplate_2" connection-factory="rabbitConnectionFactory_2" />
<rabbit:admin id="rabbitAdmin_2" connection-factory="rabbitConnectionFactory_2"/>
<rabbit:listener-container connection-factory="rabbitConnectionFactory_2" auto-startup="true" requeue-rejected="false" />
<bean id="rabbitListenerContainerFactory_2" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="rabbitConnectionFactory_2"/>
</bean>

Related

Spring Session JdbcHttpSessionConfiguration causes JPA bean not to start

I am trying to use Spring Session with PostgreSQL for storing http sessions.Followed https://docs.spring.io/spring-session/docs/2.0.4.RELEASE/reference/html5/guides/xml-jdbc.html#httpsession-jdbc-xml-sample
Here is my XML
<context:annotation-config/>
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"></property>
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/myapp"></property>
<property name="username" value="postgres"></property>
<property name="password" value=""></property>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
My application is already using Spring JPA for other DB operations. But Problem is after I added
<context:annotation-config/>
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>
my JPA beans are not loaded on the startup and hence application is not getting started.
I tried following steps.
Created two data sources beans. One was already there for JPA and one as shown in the XML with id 'dataSource'. JPA one is also with same bean properties but with different id.
Removed one in XML and renamed JPA one also to 'dataSource' as I read JdbcHttpSessionConfiguration looks for bean 'dataSource' and by assuming both JPA and session JDBC will use the same one
But both the cases result in the same error.
I was not getting any error in catalina.out. So put try catch in my application where it tries to get the JPA bean. I am getting the following error.
Cannot find class [org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration] for bean
with name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration#0'
defined in class path resource [applicationContext_Web.xml]; nested exception is java.lang.ClassNotFoundException:
org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration
Dependency added in pom
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
<version>2.0.4.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
I could solve it . Somehow maven was still picking spring-session-core jar instead of spring-session-jdbc. I just removed pom and again did a maven update. Not sure how it took the correct one then.

java.lang.NoClassDefFoundError: org/apache/cxf/jaxrs/model/doc/DocumentationProvider

I am trying to integrate swagger. I am following steps as given here. application context and pom.xml is configured same.
when restarting tomcat getting this error.
2018-04-30 14:01:33 ERROR ContextLoader:351 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productAPIServer': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/cxf/jaxrs/model/doc/DocumentationProvider
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
I have included
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-swagger</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.7</version>
</dependency>
Changes in context xml file:
<bean id="jacksonJsonProviderFasterxml" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
<bean id="swagger2Feature" class="org.apache.cxf.jaxrs.swagger.Swagger2Feature">
<property name="basePath" value="/staticdata-web-service"/>
<property name="license" value=""/>
<property name="licenseUrl" value=""/>
<property name="title" value="Static data web service"/>
</bean>
Added feature
<jaxrs:features>
<ref bean="swagger2Feature" />
</jaxrs:features>
You have a version mismatch. When you search on Maven for that class, you'll find that the package of the DocumentationProvider has changed from version 3.0.x to 3.1.x
org/apache/cxf/jaxrs/model/doc/DocumentationProvider
to
org/apache/cxf/jaxrs/model/wadl/DocumentationProvider
It's hard to tell where the mismatch is located without having insight into your entire build and package dependencies. But you may want to run
mvn dependency:tree -Dverbose
in your project folder to see which package versions come into question for raising your ClassNotFoundException.
use below dependency in your maven project to solve your issue,
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-json-basic -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-json-basic</artifactId>
<version>3.4.3</version>
<scope>provided</scope>
</dependency>

WebSphere Spring Apache CXF SOAP WebService Client timeout not working

I need to configure a SOAP WebService client with certain timeout values for connection attempts and service invocations.
The WS client is a jar dependency generated using the WSDL with Maven cxf-codegen-plugin. I use this client jar in my web app as a maven dependency and invoke the service operations.
So my webapp pom contains:
<dependency>
<groupId>my.web.service</groupId>
<artifactId>web-service-client-jar</artifactId>
<version>x.x.x</version>
</dependency>
along with the Apache CXF dependencies:
<!-- CXF dependencies -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
And I have defined below spring configuration to setup timeouts which does not work..
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<http-conf:conduit name="*.http-conduit">
<http-conf:client ConnectionTimeout="20000" ReceiveTimeout="10000" />
</http-conf:conduit>
<cxf:bus>
<cxf:outInterceptors>
<ref bean="fileuploadlogOutbound" />
</cxf:outInterceptors>
</cxf:bus>
<!-- Outbound Message Logging -->
<bean id="fileuploadlogOutbound" class="test.logging.MyLoggingOutInterceptor">
<property name="prettyLogging" value="true" />
</bean>
But to my confusion, the outInterceptors defined for pretty logging works fine. Hence, I doubt if my configuration has any errors or not. FYI I'm trying to get this working in WebSphere 8.5 environment.
This is how I instantiate the WS Client in spring:
<jaxws:client id="documentUploadServiceJaxwsClient"
serviceClass="org.tempuri.IDocumentUploadService" address="#serviceEndpointString" >
<jaxws:binding>
<soap:soapBinding version="1.2" mtomEnabled="true" />
</jaxws:binding>
</jaxws:client>
Is there any steps missing in Spring config or do I need to look into WebSphere 8.5 specific configuration which will enforce HTTP/SOAP connection & response timeouts?
I figured out that my CXF configuration mentioned above works fine but the WAS environment where the application deployed is actually overriding any config you define at the application level. Hence, I followed this link to copy and define custom HTTP Transport policy where you can set values for below timeouts.
Read timeout
Write timeout
Connection timeout

Spring Tiles Integration Error

I am trying to integrate Tiles 3.0.0 with Spring 3.1, I have all the tile jar files under my lib folder. When I run the web project I get an
java.lang.ClassNotFoundException: org.apache.tiles.startup.BasicTilesInitializer
error. I need some help understanding why I am getting this error. Here is my configuration to setup in my servlet.xml page.
<bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" />
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views/**/views.xml</value>
</list>
</property>
</bean>
Here is a question that's a lot like mine, link, but I've already got my tiles-core-3.0.0 in my build path. So I just need some help understanding how to get over this error.
Morgan
At the time of writing this, Spring still does not support Tiles 3. I'm using Spring 3.1.2 and had to downgrade Tiles back to version 2.2.2 in order for my application to run.
I've make a program working with Spring Framework version 3.2.13.RELEASE and Tiles-3 (version 3.0.5). You need to use following configuration
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/tiles-definitions.xml</value>
</list>
</property>
</bean>
with dependency
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.5</version>
</dependency>
<!-- Spring Web MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
I think this is what you're trying to achieve. It works fine in my case !

How to start a Maven Spring JSF 2.0 Richfaces project?

For some of you it might sound trvial, but for days I have been fighting with pom.xml files and facets in order to build a Spring JSF 2.0 Maven project (run on tomcat 7.0) with RichFaces, but with no success. Can someone please reference me to a full working example ,I am using STS (tutorial from scratch , I can download a fully working example but I want to learn how to create one ) ?
So after few days of work I came to the conclusion I have to do the dirty work therefore I will explain how I managed to start a project :
download sts
open template project , in my case it was spring integration with hibernate
right click on project-> preferences->facets
change to JSF 2.0 and web dynamic above 2.5(require above 1.5 java )
add to the maven dependencies ( pom.xml ):
<!-- spring hibernate 3 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
</dependency>
set up your
<property name="dataSource" ref="dataSource" />
the ref is pointed to this bean :
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:."/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
or any other you decide ...
add to your project pom :
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-impl</artifactId>
<version>3.2.2.GA</version>
</dependency>
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-ui</artifactId>
<version>3.2.2.GA</version>
</dependency>
start praying it will work :)

Resources