You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by hhenne <hh...@hhenne.dk> on 2012/11/08 09:02:38 UTC

ProducerTemplate.sendBody calls toString

Documentation and samples says, that ProducerTemplate.sendBody should send
any Object.
However in my setup, it seems to call toString on the object, so it's not
the real object that is sent - unless the object is a String.

Here is my test, which prints: *************process body: :
ProducerTemplateTest$A@b8deef - because my object does not have overrided
toString. If I override toString (uncommenting it) it will print 'hello' all
right.


import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class ProducerTemplateTest extends CamelTestSupport {
    
    @Test
    public void firstTest() throws Exception {
    	ProducerTemplate producer = context.createProducerTemplate();
        producer.sendBody("direct:start", new A());
    }

	@Override
	protected RouteBuilder createRouteBuilder() throws Exception {
		return new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				from("direct:start")
				.process(new Processor() {
					public void process(Exchange exchange) throws Exception {
	               		String body = exchange.getIn().getBody(String.class);
                		System.out.println("*************process body: : " +
body);                		
                		exchange.getIn().setBody(body);
  					}
					})
				.to("mock:result");
			}
		};
  }


    class A {
       String s = "hello";
       
//       public String toString(){
//    	   return s;
//       }
    } 
   
}


Best Regards
Hardy Henneberg




--
View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-sendBody-calls-toString-tp5722350.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: ProducerTemplate.sendBody calls toString

Posted by hhenne <hh...@hhenne.dk>.
OK,

Stupid mistake, when I inserted the Processor to debug my initial problem,
which was a missing TypeConverter, when I sent a serializalble Axis object
to a file endpoint. Now I found another message about the same problem - but
for now I will just send it to permanent queue in stead.

thanks for helping

Hardy



--
View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-sendBody-calls-toString-tp5722350p5722359.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: ProducerTemplate.sendBody calls toString

Posted by Claus Ibsen <cl...@gmail.com>.
No it does not.

Its you who convert the message to a String in the processor code.
 String body = exchange.getIn().getBody(String.class);

You can just do
Object body = exchange.getIn().getBody();



On Thu, Nov 8, 2012 at 9:02 AM, hhenne <hh...@hhenne.dk> wrote:
> Documentation and samples says, that ProducerTemplate.sendBody should send
> any Object.
> However in my setup, it seems to call toString on the object, so it's not
> the real object that is sent - unless the object is a String.
>
> Here is my test, which prints: *************process body: :
> ProducerTemplateTest$A@b8deef - because my object does not have overrided
> toString. If I override toString (uncommenting it) it will print 'hello' all
> right.
>
>
> import org.apache.camel.Exchange;
> import org.apache.camel.Processor;
> import org.apache.camel.ProducerTemplate;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.junit.Test;
>
> public class ProducerTemplateTest extends CamelTestSupport {
>
>     @Test
>     public void firstTest() throws Exception {
>         ProducerTemplate producer = context.createProducerTemplate();
>         producer.sendBody("direct:start", new A());
>     }
>
>         @Override
>         protected RouteBuilder createRouteBuilder() throws Exception {
>                 return new RouteBuilder() {
>                         @Override
>                         public void configure() throws Exception {
>                                 from("direct:start")
>                                 .process(new Processor() {
>                                         public void process(Exchange exchange) throws Exception {
>                                 String body = exchange.getIn().getBody(String.class);
>                                 System.out.println("*************process body: : " +
> body);
>                                 exchange.getIn().setBody(body);
>                                         }
>                                         })
>                                 .to("mock:result");
>                         }
>                 };
>   }
>
>
>     class A {
>        String s = "hello";
>
> //       public String toString(){
> //         return s;
> //       }
>     }
>
> }
>
>
> Best Regards
> Hardy Henneberg
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-sendBody-calls-toString-tp5722350.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen