我使用Apache Camel 2.9.2和Spring 3.0.6.RELEASE。我正在嘗試使用自定義的DataFormat來編組和解組駱駝消息。我想使用Spring將我的自定義DataFormat配置到我的一個路由中。定製Apache Camel數據格式的春天配置
Apache的駱駝的文件指出,爲了我的自定義數據格式掛鉤到春天我只需要聲明我的自定義DATAFORMAT作爲一個bean和引用它我的春節,路線內,像這樣的路線:
<marshal>
<custom ref="myCustomDataFormat"/>
</marshal>
http://camel.apache.org/custom-dataformat.html
所以,我有以下設置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="myCustomDataFormat" class="com.test.CustomDataFormat"/>
<!-- Camel Context -->
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:C:/test?initialDelay=4000&delay=1000"/>
<marshal>
<custom ref="myCustomDataFormat"/>
</marshal>
<to uri="file:C:/test2"/>
</route>
</camelContext>
</beans>
但是,當我嘗試啓動駱駝,我得到以下討厭埃羅r:
org.springframework.beans.ConversionNotSupportedException:未能將類型'com.test.CustomDataFormat'的值轉換爲所需類型'org.apache.camel.model.DataFormatDefinition';嵌套異常是java.lang.IllegalStateException:無法將[com.test.CustomDataFormat]類型的值轉換爲所需類型[org.apache.camel.model.DataFormatDefinition]:沒有找到匹配的編輯器或轉換策略
我的數據格式定義如下:
package com.test;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.camel.Exchange;
import org.apache.camel.spi.DataFormat;
public class CustomDataFormat implements DataFormat {
/* (non-Javadoc)
* @see org.apache.camel.spi.DataFormat#marshal(org.apache.camel.Exchange, java.lang.Object, java.io.OutputStream)
*/
@Override
public void marshal(Exchange exchange, Object graph, OutputStream stream)
throws Exception {
System.out.println("Marshal");
byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, graph);
stream.write(bytes);
}
/* (non-Javadoc)
* @see org.apache.camel.spi.DataFormat#unmarshal(org.apache.camel.Exchange, java.io.InputStream)
*/
@Override
public Object unmarshal(Exchange exchange, InputStream stream)
throws Exception {
System.out.println("Unmarshal");
byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, stream);
return bytes;
}
}
我知道我的CustomDataFormat實施是正確的,因爲我在Java中創建下面的測試路線和它的工作完美
package com.test;
import org.apache.camel.spring.SpringRouteBuilder;
public class TestFormatRoute extends SpringRouteBuilder {
/* (non-Javadoc)
* @see org.apache.camel.builder.RouteBuilder#configure()
*/
@Override
public void configure() throws Exception {
from("file:C:/test?initialDelay=4000&delay=1000").unmarshal(new CustomDataFormat()).to("file:C:/test2");
}
}
我缺少什麼?
感謝
更新
讓駱駝收到此錯誤,我發現我不相信我的自定義數據格式實際上在我創建的路線不工作後完全啓動後。我不確定哪個進程嘗試解析我的自定義數據格式並失敗,但顯然不是解析數據格式以放入我的路由的相同進程。
這解決了數據格式的功能要求,但它並不能解釋我爲什麼會收到此錯誤。
我也確認它不是我的數據格式(CustomDataFormat)的名稱導致該問題。將我的DataFormat重命名爲一個唯一的名稱(MerlinDataFormat)並未解決該錯誤。
我仍然想知道爲什麼我收到此錯誤,因爲我的控制檯和日誌文件中的大塊醜陋紅色錯誤並不完全吸引人。
再次感謝。
感謝您的回覆。我編輯了我的問題,包括我的DataFormat和Java Route來證明DataFormat實現是正確的。你可以發佈你的完整文件爲你的駱駝春季配置?我很好奇,看看我們的xml上下文或schemeLocations是不同的。謝謝。 – MerlinOfMines 2012-08-01 14:07:58
我添加了到ActiveMQ 5.6(conf/camel.xml)的路由,它應該是一個標準的spring xml。測試你的代碼,它似乎工作,檢查我更新的答案。 – 2012-08-01 19:35:14
Petter,請參閱我的問題中的更新2。我證實這不是我的DataFormat的名稱是問題。 – MerlinOfMines 2012-08-02 14:20:38