You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Gert Villemos <gv...@yahoo.de> on 2011/04/12 23:25:12 UTC

Cometd component

I have problems understanding how the cometd component publish objects.

I have my own class called 'Parameter'. I want to publish instances of
'Parameter' through cometd to a website. I would expect the data arriving
through cometd to have a structure similar to my Parameter definition.

However the only thing that arrives is a single field, holding the a value
like
"org.hbird.business.simpleparametersimulator.ConstantParameter@3da03ade",
i.e. it seems the object is transformed to a string using a simple toString
call. All field information is lost. Not very useful.

Is there a way to transform any POJO to a format (Map?) that can be
transfered using cometd, keeping the information in the POJO?

My route looks like;

	
		
			
			
		
         

I have tried inserting a Xstream data transformer in the route. Indeed I
receive a different set of data, but its an array of 160 integers, not a
structure reflecting the structure of my 'Parameter' class as I had
expected.

I know that I can create bean that converts the 'Parameter' instance into a
Map and put this into the body prior to sending the data over cometd.
However I then have to create a 'formatter' per data type. I had hoped to
have a generic formatter.

What am I doing wrong?

--
View this message in context: http://camel.465427.n5.nabble.com/Cometd-component-tp4299174p4299174.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Cometd component

Posted by "Willem.Jiang" <wi...@gmail.com>.
Hi

Instead of using the customer processor, the route can use the convertBodyTo
directly. 



  
    
  
  
    
    
     
  


Willem


--
View this message in context: http://camel.465427.n5.nabble.com/Cometd-component-tp4299174p4300424.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Cometd component

Posted by Gert Villemos <gv...@yahoo.de>.
Thanks for the suggestion.

I have found the conceptual problem on my side; the XStream / Jackson
marshalling both created a Byte array as the body of the message based on
the Object, not a string formatted as "{[name]:[value]}". I thus expected to
receive something like 

"{value:123,name:"Foo",...}" but received {123,22,65,35,67,...}

But indeed its the same, the Byte array just had to be converted to a String
and set in the body before streaming through Cometd.

I now have a bean processor;

public class Bytearray2StringFormatter {
  public synchronized void process(Exchange exchange) {
    exchange.getIn().setBody(new String((byte[])
exchange.getIn().getBody()));
  }
}

And the route



  
    
  
  
    
    
     
  


Works smoothly. Ideas to improvements are however always welcome, i.e. is
there a smarter way of doing the formatting?


--
View this message in context: http://camel.465427.n5.nabble.com/Cometd-component-tp4299174p4300394.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Cometd component

Posted by Joshua Watkins <jo...@gamesys.co.uk>.
Have you tried using the Jackson library instead of Xstream?

There is Jackson support in camel greater than 2.0
(http://camel.apache.org/json.html). I haven't used the Camel Jackson
component but if it works anything like the regular jackson libraries it
should be straight forward with no annotations/config for most POJOs.
Alternatively, you could just serialize your POJOs to JSON using the
Jackson Library (http://jackson.codehaus.org/) and a custom processor
bean. And while you are looking at serialization you might want to check
out how the different serializers perform:
https://github.com/eishay/jvm-serializers/wiki/.

-josh

On 12/04/2011 22:25, "Gert Villemos" <gv...@yahoo.de> wrote:

>I have problems understanding how the cometd component publish objects.
>
>I have my own class called 'Parameter'. I want to publish instances of
>'Parameter' through cometd to a website. I would expect the data arriving
>through cometd to have a structure similar to my Parameter definition.
>
>However the only thing that arrives is a single field, holding the a value
>like
>"org.hbird.business.simpleparametersimulator.ConstantParameter@3da03ade",
>i.e. it seems the object is transformed to a string using a simple
>toString
>call. All field information is lost. Not very useful.
>
>Is there a way to transform any POJO to a format (Map?) that can be
>transfered using cometd, keeping the information in the POJO?
>
>My route looks like;
>
>    
>        
>            
>            
>        
>         
>
>I have tried inserting a Xstream data transformer in the route. Indeed I
>receive a different set of data, but its an array of 160 integers, not a
>structure reflecting the structure of my 'Parameter' class as I had
>expected.
>
>I know that I can create bean that converts the 'Parameter' instance into
>a
>Map and put this into the body prior to sending the data over cometd.
>However I then have to create a 'formatter' per data type. I had hoped to
>have a generic formatter.
>
>What am I doing wrong?
>
>--
>View this message in context:
>http://camel.465427.n5.nabble.com/Cometd-component-tp4299174p4299174.html
>Sent from the Camel - Users mailing list archive at Nabble.com.