Spring Integration Kafka message-driven-channel-adapter receive message - java

For spring-integration-kafka version 2.1.0.RELEASE, documentation seems to be outdated
The example in the doc is incorrect as it doesn't match the constructor argument for KafkaMessageListenerContainer. Can somebody direct me how to create the bean correctly and corresponding Java code to process the message ?
<bean id="container1" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
<constructor-arg>
<bean class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
<constructor-arg>
<map>
<entry key="bootstrap.servers" value="localhost:9092" />
</map>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg name="topics" value="foo" />
</bean>

Sorry about that; we'll fix the docs; the correct documentation is in the quick start section.

Related

Spring SAML cant read property from properties file

So this one stumps me. I have been using properties files for years now, but I have never seen this.
I am using Spring MVC with SAML authentication. my context xml has this in it:
<context:property-placeholder location="file:/opt/saml.properties" />
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg value="file:/opt/mySamlKeystore.jks"/>
<constructor-arg type="java.lang.String" value="${keystore.password}"/>
<constructor-arg>
<map>
<entry key="${privateKey.alias}" value="${key.password}"/>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>
I am getting this error:
java.security.UnrecoverableKeyException: Cannot recover key
I so some SO research and they all say basically that I have the wrong password, which im sure I don't. So to test that it's reading the right file, I go and replace all the properties %{} and hard code them. Everything then works fine.
I am trying to figure this out, when I noticed that some of the other properties from that file are working! In fact, I can even do this:
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg value="file:/opt/myKeystore.jks"/>
<constructor-arg type="java.lang.String" value="${keystore.password}"/>
<constructor-arg>
<map>
<entry key="${privateKey.alias}" value="password"/>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>
So spring is getting ${keystore.password} and ${privateKey.alias} (Along with others needed like entityID, metadataProvider, etc...) from the properties file, but not ${key.password} !!!
here is the saml.properties
#keystore stuff
keystore.password=password
key.password=password
privateKey.alias=mysaml
#SP stuff (aka, my side of things)
entity.id=mycompany:me:me:me1
entity.base.url=https://mycompany.com
#IDP stuff (aka, the SAML server)
metadata.provider=https://saml.mycompany.com/FederationMetadata/2007-06/FederationMetadata.xml
This is all working when I hard coded the key password, but not when I use the ${key.password} property. What is going on here?
you have two more slash after file
For example
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg value="file:///opt/myKeystore.jks"/>
<constructor-arg type="java.lang.String" value="${keystore.password}"/>
<constructor-arg>
<map>
<entry key="${privateKey.alias}" value="password"/>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>

How to consume AWS SQS message with more than just String in header?

I have a Spring Integration project configured to consume messages from a AWS SQS and when a message with just String headers and a simple body is produced, the processor consumes it quickly.
But when I try to consume a message that have, let's suppose, a Binary.xml inside the header attributes, the message just got on the flight but it's never consumed. What's the matter with Spring Integration? Can someone help me?
I've try to use a wire tap to see depth logs (Enabling logging in spring integration utiliy) but nothing was shown.
<int:channel id="in">
<int:interceptors>
<int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>
<int:logging-channel-adapter id="logger" level="TRACE"/>
My connectionFactory bean:
<bean id="basicAWSCredential" class="com.amazonaws.auth.BasicAWSCredentials"
primary="true">
<constructor-arg value="${awsAccessKey}"/>
<constructor-arg value="${awsSecretKey}"/>
</bean>
<bean id="AWSCredentialProvider" class="com.amazonaws.auth.AWSStaticCredentialsProvider"
primary="true">
<constructor-arg index="0" ref="basicAWSCredential"></constructor-arg>
</bean>
<bean id="AWSClientBuilder" class="com.amazonaws.services.sqs.AmazonSQSClientBuilder"
factory-method="standard" primary="true">
<property name="region" value="${awsRegion}"/>
<property name="credentials" ref="AWSCredentialProvider"/>
</bean>
<bean id="SQSProviderConfiguration" class="com.amazon.sqs.javamessaging.ProviderConfiguration">
<property name="numberOfMessagesToPrefetch" value="20"/>
</bean>
<bean id="connectionFactory" class="com.amazon.sqs.javamessaging.SQSConnectionFactory">
<constructor-arg ref="SQSProviderConfiguration"/>
<constructor-arg ref="AWSClientBuilder"/>
</bean>
My consumer bean:
<int-jms:message-driven-channel-adapter
channel="RouteByFileCompressionChannel" connection-factory="connectionFactory"
destination-name="MyQueueName" concurrent-consumers="10"
acknowledge="client" max-concurrent-consumers="20" id="SqsInboundAdapter" />
I found out that the problem was a custom type in one of the headers.
The Binary.xml custom type couldn't be parsed by the Spring Integration JMS Adapter, so nothing happen when I produce a message with this type. I don't know who produced such header but now I fixed all the producers along the application.
Thanks a lot for those who helped me here.

Re Read file when using int-sftp:inbound-channel-adapter

I have a int-sftp:inbound-channel-adapter which uses SftpPersistentAcceptOnceFileListFilter as part of a composite filter. Reading the documentation/ source code it should accept a file to be read again if the modified datetime has changed, but I cant get it to work, it only reads is the once. Im using redis as the store.
Any ideas what is wrong with the configuration, Im using spring integration 4.3.5
<int-sftp:inbound-channel-adapter id="sftpInboundAdapterCensus"
channel="sftpInboundCensus"
session-factory="sftpSessionFactory"
local-directory="${sftp.localdirectory}/census-local"
filter="censusCompositeFilter"
remote-file-separator="/"
remote-directory="${sftp.directory.census}">
<int:poller cron="${sftp.cron}" max-messages-per-poll="1" error-channel="pollerErrorChannel"/>
</int-sftp:inbound-channel-adapter>
<bean id="censusCompositeFilter"
class="org.springframework.integration.file.filters.CompositeFileListFilter">
<constructor-arg>
<list>
<bean class="org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter">
<constructor-arg value="*.xml" />
</bean>
<bean id="SftpPersistentAcceptOnceFileListFilter" class="org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter">
<constructor-arg ref="metadataStore" />
<constructor-arg value="censusSampleLock_" />
</bean>
</list>
</constructor-arg>
</bean>
The SftpPersistentAcceptOnceFileListFilter only controls what we fetch from the server. You also need a FileSystemPersistentAcceptOnceFileListFilter in the local-filter (which determines which files that have been fetched end up being emitted as messages). The local filter is an AcceptOnceFileListFilter by default.

Why does Spring report constructor arg as ambiguous

I'm trying to init this map bean:
<bean id="totalEventCountStore" class="java.util.concurrent.ConcurrentHashMap">
<constructor-arg type="java.util.Map">
<map key-type="com.company.EventType" value-type="java.util.concurrent.atomic.AtomicLong">
<entry key="ROUTED_REQUEST">
<bean class="java.util.concurrent.atomic.AtomicLong">
<constructor-arg index="0" type="long" value="0"/>
</bean>
</entry>
.... more entries .....
</bean>
I get:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'totalEventCountStore' defined in class path resource [diagnostics.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [int]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
Does anyone know why this is happening ? ConcurrentHashMap has only one constructor that takes a Map argument.
Thanks.
I am not really sure of what your problem is, but I am sure what it is not : it is not about the ConcurrentHashMap initialization, nor about the AtomicLong.
Here is an extract of an applicationContext that is successfully loaded by Spring 3.2.4 :
<bean id="totalEventCountStore" class="java.util.concurrent.ConcurrentHashMap">
<constructor-arg type="java.util.Map">
<map key-type="java.lang.String" value-type="java.util.concurrent.atomic.AtomicLong">
<entry key="ROUTED_REQUEST">
<bean class="java.util.concurrent.atomic.AtomicLong">
<constructor-arg index="0" value="0"/>
</bean>
</entry>
</map>
</constructor-arg>
</bean>
I just took your code and replaced com.company.EventType I didn't have by String ... Maybe you should look at com.company.EventType because it is the only difference with my test.
This isn't actually an answer, but I found a way that works.
<bean id="totalEventCountStore" class="java.util.concurrent.ConcurrentHashMap">
<constructor-arg ref="eventCountInit"/>
</bean>
<util:map id="eventCountInit" map-class="java.util.HashMap" key-type="com.company.RouterDiagnosticEventType" value-type="java.util.concurrent.atomic.AtomicLong">
<entry key="ROUTED_REQUEST">
<bean class="java.util.concurrent.atomic.AtomicLong"/>
</entry>
<entry key="ROUTED_REQUEST_WITH_METADATA">
<bean class="java.util.concurrent.atomic.AtomicLong"/>
</entry>
... more entreies like these ...
</util:map>
Still, I don't really know why this way works while the other doesn't, so if someone finds out I'd be really grateful.
And even more puzzling, where does that [int] arg come from ?

AnnotationMethodHandlerExceptionResolver message converters

I want to configure AnnotationMethodHandlerExceptionResolver by adding jackson message converter. The problem is that I have already configured message converters in <mvc:message-converters> by creating a bean, and I can't reference it in AnnotationMethodHandlerExceptionResolver neither use ref in <mvc:message-conferters>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean id="mappingJackson2" class="pl.styall.scylla.json.config.CustomMappingJackson2">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="outboundExceptionAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2" />
</list>
</property>
</bean>
I could define two beans one for <mvc:message-converters> and AnnotationMethodHandlerExceptionResolver, but it is probably not the best idea. Is there other way to do this?
This is not supported yet, but it is on the 3.2 backlog. So it might be supported in 3.2.
See Add support for <ref> in addition to <bean> for <mvc:message-converters> for the Spring feature request.

Categories