camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject Re: Why does my JMS message type changes ?
Date Mon, 18 May 2009 13:51:35 GMT
On Mon, May 18, 2009 at 3:46 PM, uprooter <uprooter@gmail.com> wrote:
>
> I'm trying to implement what Willem suggested.
> I'm trying to avoid unmarshaling the payload while still processing messages
> according to an unmarshalled data.
> In order to do so I need to create a copy of the message, unmarshal it. And
> transfer it to a processor.
> It just don't see how it fits in Java DSL.
> Can you please demonstrate that part of creating a copy. unmarshalling and
> transferring to a processor ?

Maybe you can store a copy of the original payload and then route as before
and when you are ready restore the body with the original payload.

Something like this:

from(x)
// store the original payload in a exchange property so we can get it later
.setPropery("original", body())
// TODO: do the unmarshal and then content based router
// now when you are ready to use the original body to send to a topic
then you can restore it
.setBody(property("original")).to("activemq:topic:foo");


Or am I misunderstanding your use case?


BTW: In a Processor you have full power, so you can do the JAXB stuff there also

public void process(Exchange exchange) {
  JAXBDataFormat df = new JAXBDataFormat(....
  // and then use df to unmarshal
}

That gives you another option to try with.


>
> John.
>
>
> Claus Ibsen-2 wrote:
>>
>> On Mon, May 18, 2009 at 10:53 AM, uprooter <uprooter@gmail.com> wrote:
>>>
>>> Many thanks to your both.
>>> If I understand correctly, I need to set a header on my message and this
>>> header will be attached to my message.
>> No you should in fact not. Keep what you got from the JMS topic in the
>> body.
>>
>> What Willem suggested is a "trick" in case you need to store something
>> as a kind of 2nd copy / backup you can use headers for that.
>>
>>
>> When you use JMS as the transport then you cannot send any kind of
>> data over JMS. In fact one must follow the JMS spec that have a set of
>> limitations.
>>
>> A JMS message can contain
>> - properties (is mapped to Camel IN headers)
>> - payload (is mapped to Camel IN body)
>>
>> The JMS properties can only be simple or primitive types such as:
>> String, Integer, etc.
>>
>> The JMS payload can be
>> - TextMessage (mapped to String)
>> - BytesMessage (mapped to byte[] and other kinds)
>> - ObjectMessage (mapped to Serializable)
>> etc.
>>
>> So when you route something to a JMS topic with
>> (.to("activemq:topic:topic2") Camel will look at the IN body class
>> type and determine the best kinda of JMS message to use.
>>
>> So if you want to force that to be TextMessage then you IN body class
>> type must by java.lang.String.
>>
>> Using .convertBodyTo(String) before sending to the JMS topic you can
>> enfore that.
>>
>>
>>
>>>
>>> Unfortunately,After trying hard, I couldn't figure out how to add the
>>> unmarshaled payload as header, and how to make my bean read that header.
>>>
>>> I tried something like :
>>>    from("topic1").setHeader("Student").end()
>>>                         .choice()
>>>                        .when().method("mymethod","isstudent")
>>>                        .to("seda:a");
>>>
>>> But I can't find anywhere to put unmarshal() on the header.
>> No unmarshal works on BODY only. If you want to do that on a Header
>> then you must do than yourself from eg a Processor or regular POJO
>> class.
>>
>>
>>>
>>>
>>> Claus Ibsen-2 wrote:
>>>>
>>>> On Mon, May 18, 2009 at 12:41 AM, uprooter <uprooter@gmail.com> wrote:
>>>>>
>>>>> Hello All.
>>>>> I have this DSL that routes messages from topic1 to topic2.
>>>>> The  messages on topic1 are marshaled POJOs and I can happily unmarshal
>>>>> them.
>>>>>  from("topic1").unmarshal(jaxb).choice()
>>>>>
>>>>>  .when().method("mymethod","isStudent").to("topic2")
>>>>>
>>>>> The problem is that when I forward them to topic2 the payload is lost
>>>>> and
>>>>> instead of getting ActiveMQTextMessage I only get ActiveMQMessage
>>>>> without
>>>>> the text payload and I cannot cast them to TextMessage anymore.
>>>>> (like, if I do from("topic1").to("topic2") everything works great but
>>>>> unmashalling drops my payload somewhere)
>>>>>
>>>>> I don't want to marshal the data again. only to route them according
to
>>>>> my
>>>>> method.
>>>>> Can you please help me figure this out?
>>>>> Many thanks.
>>>>
>>>> As Willem pointed out you unmarshal the payload from topic1 to JAXB.
>>>> And JAXB is a Java Object (not text).
>>>> So when you send a JAXB object to a JMS destination (topic2) the JMS
>>>> API will use javax.jms.ObjectMessage as the payload type.
>>>>
>>>> If you want to store it as text then you must convert the payload back
>>>> to text. Either with marshal or convertBodyTo(String.class) etc.
>>>> Depending if the JAXB can reverse its operation.
>>>>
>>>>
>>>> Note: In Camel 2.0 we have added options to the JMS producer so you
>>>> can enfore a specify JMS type to use, eg.
>>>> .to("activemq:topic:topic2? jmsMessageType=Text");
>>>>
>>>> See more here from: Message Mapping between JMS and Camel
>>>> http://camel.apache.org/jms.html
>>>>
>>>>
>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> View this message in context:
>>>>> http://www.nabble.com/Why-does-my-JMS-message-type-changes---tp23588615p23588615.html
>>>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Claus Ibsen
>>>> Apache Camel Committer
>>>>
>>>> Open Source Integration: http://fusesource.com
>>>> Blog: http://davsclaus.blogspot.com/
>>>> Twitter: http://twitter.com/davsclaus
>>>>
>>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Why-does-my-JMS-message-type-changes---tp23588615p23593558.html
>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>
>>>
>>
>>
>>
>> --
>> Claus Ibsen
>> Apache Camel Committer
>>
>> Open Source Integration: http://fusesource.com
>> Blog: http://davsclaus.blogspot.com/
>> Twitter: http://twitter.com/davsclaus
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Why-does-my-JMS-message-type-changes---tp23588615p23595896.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Mime
View raw message