-
1. Re: Exchanging images using Camel
stlewis May 28, 2009 11:03 AM (in response to jamie3_james.archibald)Yes, camel is probably going to just try and write out the byte sequence as it doesn't know how to deal with this binary type. You'll probably have to write your own DataFormat class to deal with this filetype properly:
http://camel.apache.org/data-format.html
I would recommend grabbing the camel source and having a look at some of the existing DataFormat implementations, that should get you started in the right direction.
-
2. Re: Exchanging images using Camel
davsclaus May 29, 2009 12:32 AM (in response to jamie3_james.archibald)Yes Camel is payload agnostic. So basically Camel holds the payload as a java.lang.Object type.
So when Camel polls the AMQ topic and it receives a javax.jms.BytesMessage it will store this in a JmsExchange and keep the javax.jms.BytesMessage in the background.
So when you route this Exchange to a File endpoint, then the FileProducer will try to write the payload as a file.
The File producer will use the extensive and flexible Type Converter in Camel to automatic convert the payload to a InputStream that it uses to stream the payload to the java.io.File.
You can read more about Type Converters here:
http://camel.apache.org/type-converter.html
You can force Camel to use a specify type as payload in your route, so if you want it as (text instead of binary) you can use the convertBodyTo DSL.
from(x).convertBodyTo(String.class).to(y)
And a tip is to use the Tracer that logs how messages is routed in Camel and it can output the payload and what type it is:
-
3. Re: Exchanging images using Camel
jamie3_james.archibald May 29, 2009 6:32 AM (in response to davsclaus)I was able to create my own DataFormat implementation which simply provides marshalling the bytes to an output stream
@Override
public void marshal(Exchange exg, Object obj, OutputStream out) throws Exception {
Message msg = exg.getIn();
Object body = msg.getBody();
byte[] bytes = (byte[])body;
// Get the file bytes
for(int i=0;i<bytes.length;i++){
out.write(bytes[i]);
}
}
@Override
public Object unmarshal(Exchange exg, InputStream in) throws Exception {
return null;
}
This worked.
I will try doing the same with the convertBodyTo(String.class)
Thanks again.
-
4. Re: Exchanging images using Camel
jamie3_james.archibald May 29, 2009 7:15 AM (in response to jamie3_james.archibald)Converting to a string did not work.
I will keep my DataFormat implementation.
Thanks again
-
5. Re: Exchanging images using Camel
davsclaus May 29, 2009 7:34 AM (in response to jamie3_james.archibald)String is not suitable for Images as its text based.
For binary formats such as images you can use: byte[].class as the type
.convertBodyTo(byte[].class)
-
6. Re: Exchanging images using Camel
jamie3_james.archibald May 29, 2009 9:13 AM (in response to davsclaus)Sounds good.
What would be the spring equivalent of
convertBodyTo(byte[].class)
??
-
7. Re: Exchanging images using Camel
davsclaus May 29, 2009 1:02 PM (in response to jamie3_james.archibald)You can use "java.lang.byte[].class" as the classname.
The bug was tracked at CAMEL-1573
http://issues.apache.org/activemq/browse/CAMEL-1573
Edited by: davsclaus on May 29, 2009 5:01 PM