2017-06-05 85 views
1

我有傳入的消息有字符串有效負載,但數據庫,(我不控制),將這些數據記錄爲BLOB。有沒有辦法讓Spring Integration將一個字符串按到CLOB或BLOB中?有沒有辦法使用Spring Integration的jdbc編寫BLOB:oubound-channel-adapter?

我有一個出站通道適配器做一個數據庫插入,這(簡體)看起來像:

<int-jdbc:outbound-channel-adapter channel="inboundTraffic" 
            datasource="localDataSource" 
            query="insert into MESSAGES (DATA, SAVE_DATE) 
              values (:payload, :saveDate)" 
            sql-parameter-source-factory="spelSource" /> 

<bean id="spelSource" class="o.s.i.j.ExpressionEvaluatingSqlParameterSourceFactory"> 
    <property name="parameterExpressions"> 
     <map> 
       <entry key="payload" value="payload" /> 
       <entry key="saveDate" value="new java.util.Date()" /> 
     </map> 
    </property> 
</bean> 

但我得到了有效載荷的SQLException:

ORA-01461可綁定LONG值僅用於插入到LONG列中。

Ch。 18篇文檔中有一個blob示例,整個事件被重寫爲服務激活器,而不是oubound-channel-adapter,以及從文件中流式傳輸......這看起來像是過度殺毒,但是BLOB和CLOBS應該是這樣的方式在框架中完成?

感謝您的幫助!

回答

1

如何:

<entry key="payload" value="new org.springframework.jdbc.core.support.SqlLobValue(payload)" /> 

看到它的JavaDoc:

* Object to represent an SQL BLOB/CLOB value parameter. BLOBs can either be an 
* InputStream or a byte array. CLOBs can be in the form of a Reader, InputStream 
* or String. Each CLOB/BLOB value will be stored together with its length. 
* The type is based on which constructor is used. Objects of this class are 
* immutable except for the LobCreator reference. Use them and discard them. 

UPDATE

「SqlLobValue僅支持SQL類型BLOB和CLOB」,從SqlLobValue.setTypeValue(...)方法起源。

貌似沒有爲SqlLobValue沒有默認sqlType,所以我們應該做手工的東西在SqlParameterSource水平,因爲正是一個提供getSqlType()合同。

我們可以簡單地做到這一點與ExpressionEvaluatingSqlParameterSourceFactory擴展:

public class MyExpressionEvaluatingSqlParameterSourceFactory extends ExpressionEvaluatingSqlParameterSourceFactory { 
    @Override 
    public SqlParameterSource createParameterSource(Object input) { 
     AbstractSqlParameterSource parameterSource = 
       (AbstractSqlParameterSource) super.createParameterSource(input); 
     parameterSource.registerSqlType("payload", Types.BLOB); 
     return parameterSource; 
    } 
} 

隨意提出一個JIRAsetSqlTypes()支持添加到ExpressionEvaluatingSqlParameterSourceFactory

+0

感謝您的回覆!不幸的是,它似乎並沒有工作 - 你會得到一個錯誤的IllegalArgumentException「SqlLobValue只支持SQL類型BLOB和CLOB」,它來源於SqlLobValue.setTypeValue(...)方法。 如果沒有設置sqlType,即BLOB或CLOB,就會出現這種情況。向後追溯,我沒有看到一種方法,如果沒有準備好聲明已將其注入到bean中... – Decker

+0

好!在我的答案中查看** UPDATE **。 –

+0

是的,修復它,謝謝阿爾喬姆,我欣賞它! – Decker

相關問題