7 Replies Latest reply on May 29, 2009 1:02 PM by davsclaus

    Exchanging images using Camel

    jamie3_james.archibald

      I am trying to exchange images from one system to another using IONA Fuse mediation framework.

       

      Here is my scenario:

       

      1) I have an application written in C++ which sends out images on ActiveMQ using the CMS library. The messages that are sent as ActiveMQBytesMessage

      2) I have a Java application using the IONA Fuse mediation framework to receive the messages from ActiveMQ and save the image data to a file

       

      The route is as follows:

       

       

       

      I have added debugging into this and I am trying to understand what camel is doing with the ActiveMQBytesMessage when the message is sent to a file. Does camel simply take the byte stream and write it to a file stream?

       

      Are there any examples online for dealing with exchanging binary data using camel?

       

      Much thanks.

        • 1. Re: Exchanging images using Camel
          stlewis

          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

            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:

            http://camel.apache.org/tracer

            • 3. Re: Exchanging images using Camel
              jamie3_james.archibald

              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

                Converting to a string did not work.

                 

                I will keep my DataFormat implementation.

                 

                Thanks again

                • 5. Re: Exchanging images using Camel
                  davsclaus

                  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

                    Sounds good.

                     

                    What would be the spring equivalent of

                     

                    convertBodyTo(byte[].class)

                     

                    ??

                    • 7. Re: Exchanging images using Camel
                      davsclaus

                      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