Alternative to long URI to configure a Camel Endpoint with Spring beans? - spring

I tried to find a way to configure a Camel endpoint using a spring bean that is referenced from the endpoint declaration in route in a camel context, but it does not work.
For exemple, sometime defining an endpoint URI with many parameters is very horrible (!!), it would be lot more easier to configure the endpoint with a bean and its properties. (Or even better, when configuring a endpoint in XML, maybe the or elements should have sub-elements like a regular beans where we could configure the parameters of the endpoint).
The first approach below work well and is very standard and pretty simple. The second approach, is the one I would like to use instead, but it does not work. I tried with many variations, but without success! The third alternative below would just be an interesting proposal for Camel developers in fact, but it also illustrate my point.
In my example below, I only configured 3 parameters for the file endpoint, but imagine the URI with 10 parameters!! My question is how can I make my second approach working properly, I'm sure there is a simple solution!? I also tried using a factory-bean and a factory method, but it diid not work neither.
1) Standard way to configure a camel endpoint in XML (spring beans):
...
<camel:camelContext id="camelContext" >
<camel:route id="deviceDataLogsPoller" >
<camel:from uri="file://long/path/to/input?preMove=../inprogress&move=../done&moveFailed=../error" />
<camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />
</camel:route>
</camel:camelContext>
2) Alternative that I expected to be valide but that does not work (for me!):
<bean id="filePoller" class="org.apache.camel.component.file.FileEndpoint" >
<property name="camelContext" ref="camelContext" />
<property name="localWorkDirectory" value="/long/path/to/input" />
<property name="preMove" value="../inprogress" />
<property name="move" value="../done" />
<property name="moveFailed" value="../error" />
...
</bean>
...
<camel:camelContext id="camelContext" >
<camel:route id="deviceDataLogsPoller" >
<camel:from ref="filePoller" />
<camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />
</camel:route>
</camel:camelContext>
3) Alternative that would be interesting in the future (mixed between two alternatives above):
...
<camel:route id="deviceDataLogsPoller" >
<camel:from uri="file://long/path/to/input" >
<property name="preMove" value="../inprogress" />
<property name="move" value="../done" />
<property name="moveFailed" value="../error" />
...
</camel:from>
<camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />
</camel:route>
</camel:camelContext>

What did not work for you exactly?
Following setup did the job as expected:
<bean id="filePoller" class="org.apache.camel.component.file.FileEndpoint">
<property name="file" value="src/data/bean-ref" />
<property name="move" ref="moveExpression"/>
</bean>
<bean id="moveExpression" class="org.apache.camel.model.language.SimpleExpression">
<constructor-arg value="${file:parent}/.done/${file:onlyname}" />
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring" id="camelContext">
<route>
<from ref="filePoller" />
<log message="${body}" />
</route>
</camelContext>
Note:
The property file is mandatory
The properties move, moveFailed, and preMove are not of type java.lang.String but of type org.apache.camel.Expression and have to be initialized accordingly.
The property moveExpression needs a full file expression. If only .done is used instead of ${file:parent}/.done/${file:onlyname} then the file is renamed to .done and not moved to a directory named .done.

As stated in my last comment, I was able to make the bean configuration for an endpoint work (see comments above), but this approach is finally lot more complex and heavy than simply using the URIs after all!!
It would have been more interesting to have a way to configure the endpoints like I proposed in my 3rd alternative above. Maybe if I have time, I will try to create my own and tags that will wrap the existing ones by constructing the full URI from params elements...! I can also propose this to Camel developers.
See an example below of how it could be interesting to configure endpoints in the future (or with the XML wrapper I would like to code):
<camel:route id="deviceDataLogsPoller">
<camel:from uri="file://long/path/to/input" >
<param name="preMove" value="../inprogress" />
<param name="move" value="../done" />
<param name="moveFailed" value="../error" />
...
</camel:from>
...
</camel:route>
Unfortunately, the endpoint configuration as shown above is not possible for the moment, but it would be a nice to have I think! For the moment, the only way is either to specify all the parameters as params in a very long URI, or to configure the endpoint as a regular bean, with with all the complexity it implies (see comments above for details).

Related

Spring Batch: dynamic or rotate writer

I'm trying to make the next implementation.
Due to size reasons, I have to split my output file in, for example, 10k row chunks.
So, I need to dump 10k in file "out1.csv", the next 10k in file "out2.csv", and so on.
With one output file, the schema batch:chunk with reader-processor-writer is easy and direct.
The output stream is opened in batch:streams XML section inside the chunk, so I avoid the
"Writer must be open before it can be written to" exception.
I want to make an implementation avoiding this strict and preset solution:
<batch:chunk reader="reader" writer="compositeWriter" commit-interval="10000" processor-transactional="false">
<batch:streams>
<batch:stream ref="writer1" />
<batch:stream ref="writer2" />
<batch:stream ref="writer3" />
.
.
.<batch:stream ref="writer20" />
</batch:streams>
</batch:chunk>
<bean id="writer1" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="out1.csv" />
...
</bean>
<bean id="writer2" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="out2.csv" />
...
</bean>
...
<!-- writer 20 -->
Supposing that 20 writers are quite enough.
I'm looking for a solution to create output writers dynamically (maybe programatically), open them and avoid the above exception.
Due to size reasons, I have to split my output file in, for example, 10k row chunks. So, I need to dump 10k in file "out1.csv", the next 10k in file "out2.csv", and so on.
You seem to be using a CompositeItemWriter, but this is not the way to go. What you need to use is the MultiResourceItemWriter which allows you to split the output by item count. In your case, you would need to configure a MultiResourceItemWriter and set the itemCountLimitPerResource to 10.000. You can also provide a ResourceSuffixCreator to customize the output file names like out1.csv, out2.csv, etc.

spring-integration aggregate responses from outbound-gateway

I am looking to combine the responses from two webservice calls. the aggregation would happened based on a key value in the responses.
ws1,ws2(responses from ws1 and ws2 are passed to aggregator)--->aggregator--->response.
I return the following code. The aggregator is not being invoked, not sure what I am missing.
<int-ws:outbound-gateway id="marshallingGateway1" request-channel="RequestChannel1" reply-channel="replyChannel"
uri="https://abc:8080/" message-sender="messageSender"
marshaller="marshaller" unmarshaller="marshaller">
<int-ws:request-handler-advice-chain>
<ref bean="retryAdvice"/>
</int-ws:request-handler-advice-chain>
</int-ws:outbound-gateway>
<int-ws:outbound-gateway id="marshallingGateway2" request-channel="RequestChannel2" reply-channel="replyChannel"
uri="https://abc:8080/" message-sender="messageSender"
marshaller="marshaller" unmarshaller="marshaller">
<int-ws:request-handler-advice-chain>
<ref bean="retryAdvice"/>
</int-ws:request-handler-advice-chain>
</int-ws:outbound-gateway>
<int:aggregator id="responseAggregator" input-channel="replyChannel" ref="responseAggregator" message-store="messageStore" send-partial-result-on-expiry="true"/>
<bean id="messageStore" class="org.springframework.integration.store.SimpleMessageStore"/>
<bean id="responseAggregator" class="abc.cbd.ResponseAggregator"/>
You don't seem to be setting up any correlation between the two results.
By default, a header correlationId is used by the default correlation strategy.
You also need a ReleaseStrategy. It can be as simple as release-strategy-expression="size == 2".
The default release strategy uses sequenceSize and sequenceNumber headers.
If you are using a publish-subscribe channel to send the same message to the two gateways, set the apply-sequence property to true and then you don't need a custom release or correlation strategy.

Spring Integration - Move File After Xpath-splitter

i'm working with spring integration and i have the next case: i'm reading a XML file with an int-file:inbound-channel-adapter and i split the file with a int-xml:xpath-splitter, the thing is that i need to move the file after been splitted.
I want all features of int-xml:xpath-splitter plus moving the file, should i implement a custom splitter extending XPathMessageSplitter? or is there any other way to do that with an out-of-box components?
Thanks.
<int-xml:xpath-splitter id="salesTransSplitter"
input-channel="salesInputChannel"
output-channel="splitterOutChannel" order="1">
<int-xml:xpath-expression expression="/sales_transactions/trans"/>
</int-xml:xpath-splitter>
Something like this should work...
<int-file:inbound ... channel="foo" />
<int:publish-subscribe-channel id="foo" />
<int-xml:xpath-splitter input-channel="foo" ... order="1" />
<int-service-activator input-channel="foo" order="2"
expression="payload.renameTo(new java.io.File('/newDir/' + payload.name)" output-channel="nullChannel" />
If you want to test the rename was successful, send to some other channel other than nullChannel - boolean true means success.
EDIT
Sorry about that; order should be supported on every consuming endpoint, I will open a JIRA issue.
The order is not strictly necessary; if no order is present, the order they appear in the configuration will be used; I just prefer to make it explicit.
There are (at least) two work arounds:
Remvoe the order attribute from BOTH consumers and they will be invoked in the order they appear in the XML.
Configure the XPath splitter as a normal splitter, which does support order...
<int:splitter id="salesTransSplitter" order="1"
input-channel="salesInputChannel"
output-channel="splitterOutChannel" order="1">
<bean class="org.springframework.integration.xml.splitter.XPathMessageSplitter">
<constructor-arg value="/sales_transactions/trans" />
</bean>
</int-xml:xpath-splitter>

Spring uses wrong codification for customized validation messages

I have a Spring form wih some validations and all the personalized messeges from javax.validation.constraints appears to use the wrong encoding.
Lets take this as an example:
#NumberFormat(style=Style.NUMBER)
#NotNull
private BigDecimal maintenanceCosts;
With applicationContext.xml file containing
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8" />
<property name="basenames">
<list>
<value>classpath:messages</value>
<value>classpath:ValidationMessages</value>
</list>
</property>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
And a ValidationMessage_en.properties encoded in UTF-8 and marked so (in Eclipse with right click, properties) with the text:
javax.validation.constraints.NotNull.message=This field can't be empty
I want to show thouse messages in localized strings with the right codification so I added an UTF-8 file ValidationMessages_ru.properties with:
javax.validation.constraints.NotNull.message=Это поле не может быть
пустым
But the message shows this message:
Это поле не может быть пуÑтым
On the other hand I has able to customize the spring managed error messages with the right encoding. But JSR303 texts seems to behave differently.
I found that it was much more elegant to get rid of the ValidationMessages_XX.properties files. Just all the localized configuration in one file that supports UTF-8!
The answer is to set the ValidationMessageSource of your validator with your resourceBundle. For example:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:defaultEncoding="UTF-8"
p:basenames ="classpath:messages"/>
<!-- JSR-303 validation-->
<bean id ="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"
p:validationMessageSource-ref="messageSource"/>
All merit must be attributed to this blog post. Not being able to read thouse characters was annoying. No more: message=\u042D\u0442\u043E \u043F\u043E\u043B\u0435 \u043D\u0435 \u043C
Properties file in Java are per default ISO 8859-1. Characters which you cannot display this way need to use unicode escapes. To quote the javadocs of Properties:
...except the input/output stream is encoded in ISO 8859-1 character
encoding. Characters that cannot be directly represented in this
encoding can be written using Unicode escapes ; only a single 'u'
character is allowed in an escape sequence.
You will need to use unicode escapes. You can di it manually or use native2ascii.

Adapt dynamically outbound-gateway with load-balancing

I would like to modify (add/remove) the list of outbound-gateway using the loadbalancing of input channel. My code :
<int:channel id="mainRequestChannel" />
<int-http:outbound-gateway request-channel="mainRequestChannel" message-converters="messageConverters"
request-factory="httpRequestFactory" url="http://localhost:8100/batchfactory-slave/receiveGateway"
http-method="POST" expected-response-type="JobLaunchingResponse" order="1" reply-channel="finishedResponse" />
<int-http:outbound-gateway request-channel="mainRequestChannel" message-converters="messageConverters"
request-factory="httpRequestFactory" url="http://localhost:8090/batchfactory-slave/receiveGateway"
http-method="POST" expected-response-type="JobLaunchingResponse" order="2" reply-channel="finishedResponse" />
I modify this and I have now a dynamicRouter :
<int:channel id="mainRequestChannel" />
<int:router input-channel="mainRequestChannel" expression="#dynamicChannelResolver.resolve()"/>
But there is only 1 subscriber for the mainRequestChannel so the load balancer which is the default behaviour doesn't work.
Thanks for your help.
See the dynamic-ftp sample. You would essentially put the outbound gateway in its own context with parameters for the URL etc. However, unlike that example, you would need to make the context a child of the main context (so it will be able to reference the finishedResponsechannel).
The sample README has a link to some forum discussions where that technique is explained.
Alternatively, you can wire up the necessary classes yourself - you would need a EventDrivenConsumer with mainRequestChannel and a properly configured HttpRequestExecutingMessageHandler in its constructor.

Resources