Spring integration poller XML configuration with spring boot - java

I am working on a project in spring boot and I need to add Spring integration poller for polling files from a location and run spring batch on that file to process it.
I have used spring batch integration for this(Document Reference below.)
http://docs.spring.io/spring-batch/trunk/reference/html/springBatchIntegration.html
In spring boot, I have succesfully configured my poller in #Configuration file as below
#Bean
#InboundChannelAdapter(value = "fileInputChannel", poller = #Poller(
fixedRate = "1000"), autoStartup = "true")
public MessageSource<File> filesScanner() {
CompositeFileListFilter<File> filters = new CompositeFileListFilter<File>();
filters.addFilter(new SimplePatternFileListFilter("*.xml"));
filters.addFilter(new AcceptOnceFileListFilter<File>());
filters.addFilter(getLastModifiedFileFilter());
FileReadingMessageSource source = new FileReadingMessageSource();
source.setDirectory(new File("F:/DataInput/"));
source.setFilter(filters);
return source;
}
This poller is defined in java configuration whereas the channels are defined in xml as below.
<?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"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">
<int:channel id="ticketingResponse" />
<int:channel id="mailFailureTicketData" />
<int:channel id="moveSuccessTicketingFile" />
<int:channel id="moveFailureTicketingFile" />
<int:channel id="ticketingFileInput" />
<int:channel id="ticketingJobParameters" />
<!-- <int-file:inbound-channel-adapter id="filePoller"
channel="inboundFileChannel"
directory="file:/tmp/myfiles/"
filename-pattern="*.csv">
<int:poller fixed-rate="1000"/>
</int-file:inbound-channel-adapter> -->
<bean id="earliestTicketingFileSelecter" class="com.avios.integration.iqcx.FilesSortingComparator" />
<bean id="compositeFilesFilter"
class="org.springframework.integration.file.filters.CompositeFileListFilter">
<constructor-arg>
<list>
<bean
class="org.springframework.integration.file.filters.RegexPatternFileListFilter">
<constructor-arg name="pattern" value="${ticketing.input.file.pattern}" />
</bean>
<bean class="org.springframework.integration.file.filters." />
<bean
class="org.springframework.integration.file.filters.LastModifiedFileListFilter">
<property name="age" value="${ticketing.input.file.age}" />
</bean>
</list>
</constructor-arg>
</bean>
<bean id="ticketingFilesScanner"
class="org.springframework.integration.file.FileReadingMessageSource">
<property name="filter" value="compositeFilesFilter" />
<property name="directory" value="/tmp/myfiles/" />
</bean>
<int-file:inbound-channel-adapter id="filePoller"
channel="inboundFileChannel"
directory="file:/tmp/myfiles/"
filename-pattern="*.csv">
<int:poller fixed-rate="1000"/>
</int-file:inbound-channel-adapter><!-- <int-file:inbound-channel-adapter
directory="${ticketing.input.file.path}" channel="ticketingFileInput"
comparator="earliestTicketingFileSelecter" auto-startup="true" filter="compositeFilesFilter" >
<int:poller ></int:poller>
</int-file:inbound-channel-adapter> -->
<int:transformer id="iqcxFilesToJobParameters" ref="jobParameterTransformer"
input-channel="ticketingFileInput" method="addTicketingFileToJobParameter"
output-channel="ticketingJobParameters" />
<int:outbound-channel-adapter channel="ticketingJobParameters"
ref="iqcxJobLaunchingGateway" method="handleMessage" />
</beans>
I am getting the below error in my XML configuration file.
cvc-complex-type.3.2.2: Attribute 'fixed-rate' is not allowed to appear in element 'int:poller'.
I checked this on google and found only the below link which wasn't much of use as i am getting exact same error.
Using Spring Boot & Spring Integration with database backed Configuration
Attribute 'fixed-rate' is not allowed to appear in element 'int:poller'
Spring boot version i am using is as below.
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
Spring integration jar in library - Spring-integration-core-4.2.8.RELEASE.jar
I also tried excluding integration jar from batch-integration dependency and adding it separately as below but that didn't work either.
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-integration</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.integration/spring-integration-core -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
</dependency>
Also checked the XSD http://www.springframework.org/schema/integration/spring-integration.xsd and it has the attribute fixed-delay in poller. Any suggestions for resolving this?

Read the important note in the online schema:
+++++ IMPORTANT +++++
This schema is for the 1.0 version of Spring Integration Core. We cannot update it to the current schema
because that will break any applications using 1.0.3 or lower. For subsequent versions, the unversioned
schema is resolved from the classpath and obtained from the jar.
Please refer to github:
https://github.com/spring-projects/spring-integration/tree/master/spring-integration-core/src/main/resources/org/springframework/integration/config/xml
for the latest schema.
In the old schema, fixed-rate was part of a periodic trigger child element on the poller.
The 4.2 schema is here.
If this is just an IDE error, you can ignore it, or configure your IDE to be "spring aware".
Spring finds the actual schema on the classpath.
With STS, enable "spring nature" on the project.

sometimes it could be due to missing spring-boot-starter-integration & spring-integration-file dependency in pom.xml. So check if actually spring-integration-file dependency is available in your project when you are using spring integration.

Related

Swagger 2.0: swagger-ui page showing default api info instead of the custom api info which I am setting

I am using docket api to configure swagger in a spring mvc project. The swagger-ui is correctly showing all the paths. However, the custom API information like title, description, license, etc. are showing only the default values and not the values which I am setting.
Debugging:
In the method "apiDocket()" in the class "SwaggerConfiguration", I checked the value of the field "apiInfo" for the object "docket" just before the return statement (using reflection). The values are correctly getting set. So clearly the problem is elsewhere which I am not being able to detect.
I am using sprinfox-swagger version 2.9.2. I have tried with 2.9.1, 2.8.0, 2.7.0 and 2.6.1 just to check. I got the same results.
The problem which I am facing has been raised at Swagger doesn't pick up customized API Info and always shows default values and Spring boot swagger :custom information about Api not working but I have not got my solution.
#Configuration
#EnableSwagger2
public class SwaggerConfiguration {
#Bean
public Docket apiDocket() {
ApiInfo apiInfo = getApiInfo();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
docket.apiInfo(apiInfo);
return docket;
}
private ApiInfo getApiInfo() {
return new ApiInfo(
"TITLE",
"DESCIPRION",
"VERSION",
"TERMS OF SERVICE URL",
new Contact("NAME","URL","EMAIL"),
"LICENSE",
"LICENSE URL",
Collections.emptyList()
);
}
}
In the servlet config xml file, I have the following:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- The controllers are autodetected POJOs labeled with the #Controller
annotation. -->
<context:component-scan base-package="com.sample.package"
use-default-filters="false">
<context:include-filter
expression="org.springframework.stereotype.Controller" type="annotation" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
<!-- <context:include-filter type="regex" expression=".*SwaggerConfiguration.*"/> -->
</context:component-scan>
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<!-- Turns on support for mapping requests to Spring MVC #Controller
methods Also registers default Formatters and Validators for use across all
#Controllers -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:interceptors>
<bean class="com.sample.package.CustomRequestHandler"/>
</mvc:interceptors>
<!-- Enable scanning of spring #Configuration classes -->
<context:annotation-config />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<bean id="swagger2Config"
class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration">
</bean>
<mvc:resources order="1" location="/resources/"
mapping="/resources/**" />
<mvc:resources mapping="swagger-ui.html"
location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
location="classpath:/META-INF/resources/webjars/" />
</beans>
In my pom.xml I have
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
In the swagger-ui page, I expect the title to be "TITLE", description to be "DESCRIPTION, version to be "VERSION", as evident from the method "getApiInfo()" in the code snippet. However, I am getting title as "Api Documentation", description as "Api Documentation", and version as "1.0". These are all default values and the values which I am setting in the code are not getting reflected in the swagger-ui page.
Please help!
In the servlet config xml file, I needed to add the SwaggerConfiguration class as a bean. I added the following line:
<bean class="com.sample.package.SwaggerConfiguration"/>
And it worked.

Spring MVC - configuring Ehcache - Not caching

I am trying to integrate Ehcache with my Java Spring MVC Web Applications. I have followed the instructions from the following article:
https://dzone.com/articles/implementing-ehcache-using.
I have added the following dependency to my pom.xml file:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.9.0</version>
</dependency>
My ehcache.xml is as follows:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir" />
<cache name="swcmRestData"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
I have the following entries in my root-context.xml:
<!-- EhCache Configuration -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" p:shared="true"/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache"/>
And I have a method for which I want to enable ehCache:
#Cacheable(value="swcmRestData", key="url")
public <T> T getEntity(String url, java.lang.Class<T> gt) throws RestException
{
T t = restClientService.getEntity(url, gt);
return t;
}
I want the data to be retrieved from the ehCache if the same url is passed to the specified method. I do not get any errors while running the code. But looks like the caching is not working. Is there anything that I am missing here
Two things that could be the cause of the problem:
You are missing the Spring configuration so that there is a link between the defined beans and the caching annotation. Effectively that's point 2 in the article you link which you do not mention here.
As suggested in comments, the method you are caching is called from within the same class. That's a limitation of Spring AOP implementation when using proxies. If you configure Spring to do bytecode weaving it will work.
If none of the above are the source of error, please provide more information on your setup.

Unable to locate ldaptive schema in CAS 4.2.x

I am new to the CAS setup, started using CAS 4.2.x version. With help of the below , doing the setup.
[https://apereo.github.io/cas/4.2.x/installation/LDAP-Authentication.html][1]
After adding bean reference "http://www.ldaptive.org/schema/spring-ext" in the deployerConfigcontext.xml, getting the error
Configuration problem: Unable to locate Spring Namespace handler for xml schema namespace [http://www.ldaptive.org/schema/spring-ext]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ldaptive="http://www.ldaptive.org/schema/spring-ext"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd
http://www.ldaptive.org/schema/spring-ext http://www.ldaptive.org/schema/spring-ext.xsd">
I am using CAS 3.5.2 and was facing the same issue ..... the below workaround worked for me. You can give it a shot and see if it works for you as well.
Add the following dependency in your pom.xml file
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-support-ldap</artifactId>
<version>${cas.version}</version>
</dependency>
then remove the below lines from your deployerConfigcontext.xml
xmlns:ldaptive="http://www.ldaptive.org/schema/spring-ext"
http://www.ldaptive.org/schema/spring-ext http://www.ldaptive.org/schema/spring-ext.xsd
In your deployerConfigcontext.xml remove the ldaptive declaration and replace it with the below format.
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<!-- DO NOT enable JNDI pooling for context sources that perform LDAP bind operations. -->
<property name="pooled" value="false"/>
<!--
Although multiple URLs may defined, it's strongly recommended to avoid this configuration
since the implementation attempts hosts in sequence and requires a connection timeout
prior to attempting the next host, which incurs unacceptable latency on node failure.
A proper HA setup for LDAP directories should use a single virtual host that maps to multiple
real hosts using a hardware load balancer.
-->
<property name="url" value="your-ldap-url" />
<!--
Manager credentials are only required if your directory does not support anonymous searches.
Never provide these credentials for FastBindLdapAuthenticationHandler since the user's
credentials are used for the bind operation.
-->
<property name="userDn" value="your-userDn-info"/>
<property name="password" value="password-for-your-userDn"/>
<!-- Place JNDI environment properties here. -->
<property name="baseEnvironmentProperties">
<map>
<!-- Three seconds is an eternity to users. -->
<entry key="com.sun.jndi.ldap.connect.timeout" value="3000" />
<entry key="com.sun.jndi.ldap.read.timeout" value="3000" />
<!-- Explained at http://docs.oracle.com/javase/jndi/tutorial/ldap/security/auth.html -->
<entry key="java.naming.security.authentication" value="simple" />
</map>
</property>
</bean>
Also in your deployerConfigcontext.xml remove your ldapAuthenticationHandler bean and replace it with the following
<bean class="org.jasig.cas.adaptors.ldap.BindLdapAuthenticationHandler"
p:filter="your-ldap-filter"
p:searchBase="your-search-base"
p:contextSource-ref="contextSource"
p:ignorePartialResultException="true" />
Please note that i am using CAS 3.5.2 so you might have to make some more changes ...

Spring MVC MongoDB integration

I would like to create Spring MVC sample with mongoDB by this tutorial http://spring.io/guides/gs/accessing-data-mongodb/. I try #Autowired for CustomerRepository repository into my #Controller class, but I receive org.springframework.beans.factory.BeanCreationException for repository class member with exception message "....No matching bean of type [com.mvc.venko.CustomerRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency..."
My #Controller class and CustomerRepository are in same package.
I can't get the problem causing this exception.
PS: using springFramework 3.1.1.RELEASE, spring-data-mongodb 1.4.2.RELEASE
EDIT:
Complete conf.xml structure for resolving problem. Please switch to Spring 3.2.5
<?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:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<mongo:repositories base-package="com.mvc.venko" />
<mongo:mongo host="127.0.0.1" port="27017" />
<mongo:db-factory dbname="yourdb" write-concern="NONE" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
I think you don't configure a bean of CustomerRepository type.
You can do this with this line in your spring-config.xml:
<mongo:repositories base-package="/path to your repository package/" />
more docs here

How do I load a Camel Property into a Bean?

I have been reading the following page on Camel properties: http://camel.apache.org/using-propertyplaceholder.html and also reading the book "Camel In Action".
I found Chapter 6 of "Camel In Action" very helpful in defining Camel properties, and I can load the following three properties from my config.properties:
config.timeout=10000
config.numSamples=1000
config.defaultViz=a
When I run my Java code I'm able to see the following three values inside my camel route in my applicationContext.xml, as shown in the thread#0 messages below:
14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - printing values read from config.properties file
14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.timeout= 10000
14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.numSamples= 1000
14670 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.defaultViz= a
However, when I try to pass the variable {{config.defaultViz}} to a String called defaultViz in my SensorGenerator Java class, and print that string I get "{{config.defaultViz}}" on the console instead of the value contained within {{config.defaultViz}}.
In other words, here's what I see on the screen:
Returning List
defaultViz= {{config.defaultViz}}
But I really want to see this on the screen:
Returning List
defaultViz=a
So what am I doing wrong in my applicationContext.xml?
UPDATED: The issue was that I needed to add a Bridge between Spring and Camel as outlined in the link I referenced above.
Here's my UPDATED applicationContext.xml with the bridge:
<?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:camel="http://camel.apache.org/schema/spring"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.data.world2" />
<context:annotation-config />
<camel:camelContext id="HelloWorldContext">
<!-- Add Jackson library to render Java Map into JSON -->
<camel:dataFormats>
<camel:json id="jack" library="Jackson"/>
</camel:dataFormats>
<camel:route>
<!-- sends a request to the hello world JMS queue every 10 seconds -->
<camel:from
uri="timer://hello.world.request.timer?fixedRate=true&period={{config.timeout}}" />
<camel:to uri="log:hello.world.request?level=INFO&showAll=true" />
<camel:bean ref="helloWorld" />
<!-- now print out the map in JSON format -->
<camel:marshal ref ="jack"/>
<camel:convertBodyTo type="java.lang.String" />
<camel:log message="${body}"/>
<!-- print out values read from config.properties file -->
<camel:log message="printing values read from config.properties file"/>
<camel:log message="config.timeout= {{config.timeout}}"/>
<camel:log message="config.numSamples= {{config.numSamples}}"/>
<camel:log message="config.defaultViz= {{config.defaultViz}}"/>
<!-- now log the message -->
<camel:to uri="log:hello.world.response?level=INFO&showAll=true" />
</camel:route>
</camel:camelContext>
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="sensorProperties" location="classpath:/sensor.properties"/>
<!-- pass in sensor.properties and defaultViz from config.properties -->
<bean class="com.data.world2.SensorGenerator">
<property name="sourceProperties" ref="sensorProperties" />
<property name="defaultViz" value="${config.defaultViz}"/>
</bean>
<!-- declare a Spring bean to use the Camel Properties component in Spring XML -->
<bean id="properties"
class="org.apache.camel.component.properties.PropertiesComponent">
<property name="location" value="classpath:config.properties"/>
</bean>
<!-- bridge spring property placeholder with Camel -->
<!-- you must NOT use the <context:property-placeholder at the same time, only this bridge bean -->
<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="location" value="classpath:config.properties"/>
</bean>
</beans>
I found this question that is similar but not quite the same: Injecting property into bean
The {{}} notation just works inside the routes (ie inside the XML camel contexts). To use it in the bean I think you need to define the property placeholder bridge that camel provides but in your bean use the ${} notation. The explanation of how to use that bridge is in the link you have provided.

Categories

Resources