通过之前的刻意练习一书,我进行了一番思考。软件工程师要怎么刻意练习呢?我类比棋类大师的刻意练习,通过不断和别人对弈并不能很快的提高水平,真正能够帮助他们提高的是阅读并思考大师的棋谱或者对战。那么程序员是不是也是如此呢,通过不断的埋头写代码,或者听讲座,读书是不是真正的能很快的提高代码水平呢?也不能说不是,但是我觉得更快的方式可能是研究别人的代码。一方面可以把理论和实践结合起来,比如Spring Integration,读书学习只是了解了理论和框架,没有实际的Case支持是不扎实的。另一方面,可以学习到别人代码的好的风格和技巧,这一点更好的应正在Spring Integration的学习中,别人写的flow可能包含书上没有的,不常用但是在某个场景下必要的设置,通过阅读别人的代码相当于是走了一条捷径吸收这些知识。
下面我就列一些我的学习笔记:
1. Channel Interceptor
<int:channel id="outputChannel">
<int:queue/>
<int:interceptors>
<int:wire-tap channel="logchannel"/>
</int:interceptors>
</int:channel>
Spring Integration provides an implementation of the Wire Tap pattern. It is a simple interceptor that sends the Message to another channel without otherwise altering the existing flow. It can be very useful for debugging and monitoring.
2. The transformer provided by Spring integration
Sample: <file:file-to-bytes-transformer id="fileToByteTransformer"/>
xmlns:file="http://www.springframework.org/schema/integration/file"
include “file” namespace. User can use basic transformer like file->bytes/string
3. The normal using xslt to transform xml handler
<si-xml:xslt-transformer xsl-resource="common/xslt/empty.xslt" id="xxxXsltTransformer/>
Only need provide xslt file as xsl-resource
xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
Need include si-xml namespace
4. Queue channel
<int:channel id="transByBChannel">
<int:queue/>
</int:channel>
It is an easy configuration. But should pay attention:
1)This config not set capacity and the default is unbounded. To avoid OOM issue.Can use set capacity
2) Queue message is store in memory and the message could be lost in the event of a system failure.
3) If config transactional persistent message store, the message will be remove after transaction finishing.
5. Email channel adapter:
<int-mail:inbound-channel-adapter id="mailAdapter"
store-uri="${store-uri}"
channel="xxxInboundChannel"
auto-startup="true"
should-delete-messages="${should-delete-messages}"
Specify whether mail messages should be deleted after retrieval
should-mark-messages-as-read="${should-mark-messages-as-read}"
Specify whether mail messages should be marked after retrieval
search-term-strategy="${search-term-strategy}"
The strategy of receiving email. For example, if set “unseenSearchTermStrategy”means you want to receive unread email.
java-mail-properties="javaMailProperties">
<int:poller max-messages-per-poll="${mail-max-messages-per-poll}" error-channel="xxxErrorChannel" fixed-rate="${mail-fixed-rate}"/>
</int-mail:inbound-channel-adapter>
I have added description in some configs. And if need use email inbound channel adapter need namespace:
xmlns:int-mail=http://www.springframework.org/schema/integration/mail
Another thing need attention is the difference of “fixed-rate” and “fixed-delay”
Fixed-rate : mission1 start time -> mission2 start time.
Fixed-delay : mission1 finish time -> mission2 start time.
6. Use xpath route messages
<si-xml:xpath-router id="messageRouter" default-output-channel="unHandleMessageChannel" resolution-required="false">
<si-xml:xpath-expression expression="//pdf/record[3]/column[1]"/>
<si-xml:mapping value="xxx/BOOKING" channel="transByAChannel"/>
<si-xml:mapping value="xxx/DELIVERY" channel="transByBChannel"/>
</si-xml:xpath-router>
It is useful and easy use to route the messages.
7. Use xpath add payload(xml) info into header
<si-xml:xpath-header-enricher>
<si-xml:header name="HEADERNAME" xpath-expression="//name" overwrite="true"/>
</si-xml:xpath-header-enricher>
8. Using SFTP send target message:
<int-sftp:outbound-channel-adapter
session-factory="sftpSessionFactory"
charset="UTF-8"
remote-file-separator="/"
Allows you to provide remote file/directory separator character. DEFAULT: '/'
remote-directory="${pointTime-remote-directory}"
Identifies the remote directory path
remote-filename-generator-expression="headers.BUSSINESSKEY+'-'+new java.text.SimpleDateFormat('yyyyMMddHHmmssSSS').format(new java.util.Date())+'-'+new java.util.Random(6)"
Write pattern -> Definite the target file name pattern.
chmod="${sftp-chmod}"
This command enables the read and execute privileges for group andothers. (777)
mode="REPLACE"/>
1) Need use namespace int-sftp
xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
2) Add description for some settings.