Spring Integration, change udp-outbound-channel port

383 Views Asked by At

I'm using spring integration to send line of files in UDP. Here is what I'm doing:

<int-file:inbound-channel-adapter
    prevent-duplicates="false" id="filesIn" directory="file:input"
    channel="inputFiles">
    <int:poller default="true" fixed-rate="1000" />
</int-file:inbound-channel-adapter>

<int:splitter input-channel="inputFiles" output-channel="output">
    <bean class="fr.spring.demo.FileSplitter">
        <property name="commentPrefix" value="#" />
    </bean>
</int:splitter>

<int:transformer input-channel="output" expression="payload"
    output-channel="exampleChannel" />

<int:channel id="exampleChannel" />

<int-ip:udp-outbound-channel-adapter
    id="udpOut" channel="exampleChannel" host="192.168.0.1" port="11111">
</int-ip:udp-outbound-channel-adapter>

So it takes a list of files from a repertory, splits the file in lines and sends the line on port 11111.

What I would like to do, is to send the lines on a predefined port depending on the extension of the file:

  • All the lines of *.txt files will be sent on port 11111
  • All the lines of *.csv files will be sent on port 11112
  • And so on

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

It is not currently possible to select the port based on the message; please feel free to open a new feature JIRA Issue.

In the meantime, you can declare two adapters, one with each port, and add a router upstream to route to one or the other based on the file extension.

0
On

Thanks Gary!

Here is how I proceed:

<int-file:inbound-channel-adapter
    prevent-duplicates="false" id="filesIn" directory="file:input"
    channel="inputFiles">
    <int:poller default="true" fixed-rate="1000" />
</int-file:inbound-channel-adapter>


<int:chain input-channel="inputFiles">
    <int:header-enricher>
        <int:header name="extension"
            expression="payload.getName().substring(payload.getName().lastIndexOf('.'))" />
    </int:header-enricher>
    <int:splitter>
        <bean class="fr.spring.demo.FileSplitter">
            <property name="commentPrefix" value="#" />
        </bean>
    </int:splitter>
    <int:router expression="headers.extension">
        <int:mapping value=".gps" channel="udpChannel_11111" />
        <int:mapping value=".ths" channel="udpChannel_11112" />
    </int:router>
</int:chain>

<int:channel id="udpChannel_11111" />
<int:channel id="udpChannel_11112" />
<int-ip:udp-outbound-channel-adapter
    channel="udpChannel_11111" host="192.168.0.1" port="11111" />
<int-ip:udp-outbound-channel-adapter
    channel="udpChannel_11112" host="192.168.0.1" port="11112" />