You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@servicemix.apache.org by arshadansari27 <ar...@gmail.com> on 2010/08/26 15:09:22 UTC

How to avoid loosing header between JBI endpoint and Camel SU

I'm trying to set a header information in a message and that header is there
till it gets lost, when sent to the JMS Provider SU.. I'm not sure why, but
I don't  see the header in JMS message itself..
Here is the flow of what I'm trying to do.

HTTP SU Consumer ===> Bean SU (Adding header here) ==> Validation SU ===>
JMS Provider SU ==> Topic ==> JMS Consumer SU (Message Selector is used -
which needs the header to do selection) ==> HTTP SU Provider.

All the '===>' means Camel is used for Routing from one Su to another..
Servicemix version is 3.3.2. Camel version is the one that comes with 3.3.2. 

Here's the Camel SU's DSL Code that is doing the Routing..
from("jbi:service:http://servicemix.in2m.com/operations/updateprofile/RoutingService")
       
.to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/MessageEnricherService?mep=in-out")
        .process(new HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,
true,false))

       
.to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/ValidateService?mep=in-out")

        .process(new
HeaderPropertyExchange(HeaderPropertyExchange.TO_JBI,false,false))

       
.to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/JMSProviderService?mep=in-only")
        .process(new
HeaderPropertyExchange(HeaderPropertyExchange.TO_JBI,false,true))

       
.to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/ResponseGeneratorService?mep=in-out");

       
       
from("jbi:service:http://servicemix.in2m.com/operations/updateprofile/DirectorConsumerService")

        .process(new
HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,true,false))
       
.to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/DirectorService?mep=in-out")

        .process(new
HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,false,false));
        
       
from("jbi:service:http://servicemix.in2m.com/operations/updateprofile/PortalConsumerService")
        .process(new
HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,true,false))
       
.to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/PortalService?mep=in-out")
        .process(new
HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,false,false));


The Delegate Processor used above is : HeaderPropertyExchange.java. I am
using this to copy the header to an from Properties of Message and its code
is as follows:
Just writing the process(Exchange exchange) code and its constructor code

public HeaderPropertyExchange(int type, boolean displayOnly, boolean inOnly)
{
		this.type = type;
		this.displayOnly = displayOnly;
		this.inOnly = inOnly;
	}

public void process(Exchange exchange) {
		try {
			
			if (type == TO_CAMEL && !(displayOnly)) {
				Message inMessage = exchange.getIn();
		if (inMessage.getHeaders() != null) {

			Map<String, Object> headers = inMessage.getHeaders();

			Iterator<Entry<String, Object>> it = headers.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Object> pairs = it.next();
				exchange.setProperty(pairs.getKey(), pairs.getValue());
			}
		}
				proceed(exchange);
				if(!inOnly)
					{
if (exchange.getProperties() != null) {

			Map<String, Object> properties = exchange.getProperties();

			Iterator<Entry<String, Object>> it = properties.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Object> pairs = it.next();
				exchange.setProperty(pairs.getKey(), pairs.getValue());
			}
		}
                                      }

			} else if (type == TO_JBI && !(displayOnly)) {
				
				if (exchange.getProperties() != null) {

			Map<String, Object> properties = exchange.getProperties();

			Iterator<Entry<String, Object>> it = properties.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Object> pairs = it.next();
				exchange.setProperty(pairs.getKey(), pairs.getValue());
			}
		}
				proceed(exchange);
				if(!inOnly)
					{
Message inMessage = exchange.getIn();
		if (inMessage.getHeaders() != null) {

			Map<String, Object> headers = inMessage.getHeaders();

			Iterator<Entry<String, Object>> it = headers.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Object> pairs = it.next();
				exchange.setProperty(pairs.getKey(), pairs.getValue());
			}
		}
}
			}
			if(displayOnly)
				proceed(exchange);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

 Please Help.. The JMS provider, when it sends the message on Topic, the
header is still missing...

-- 
View this message in context: http://servicemix.396122.n5.nabble.com/How-to-avoid-loosing-header-between-JBI-endpoint-and-Camel-SU-tp2709458p2709458.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.

Re: How to avoid loosing header between JBI endpoint and Camel SU

Posted by arshadansari27 <ar...@gmail.com>.
Hello Mr Claus Ibsen,
Thank you for the quick reply and I really appreciate the help. I went
through the link regarding Camel and JMS. However, I'm using Camel to only
route the message to JMS Provider SU (XBean). This service unit is
responsible for sending it forward.. Also, the header information I am
trying to add to the message is 
name=>value
userPrincipal=>smx,admin
Both Strings..
This is what I'm trying to attach with the JMS Header before it goes to the
topic.. Based on the content of the value, we will use JMS Consumer SU's
message selector.. Can it be done in JMS Marshaller.. I'm already copying
Camel Properties to JMS Headers in custom jms marshaller.. Here's the
provider marshaller code:


package com.in2m.servicemix.common.marshaller.jms;

import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;

import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.NormalizedMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.xml.transform.Source;

import org.apache.servicemix.jbi.jaxp.SourceTransformer;
import org.apache.servicemix.jbi.jaxp.StringSource;
import org.apache.servicemix.jms.endpoints.JmsProviderMarshaler;

/**
 * Refactor the class so that it extends the DefaultJms Provider class
instead of implementing JmsProviderMarshaler
 * @author sreddy
 *
 */
public class DefaultJmsProviderMarshaler implements JmsProviderMarshaler {
	
	private SourceTransformer transformer = new SourceTransformer();
	
	/* (non-Javadoc)
	 * @see
org.apache.servicemix.jms.endpoints.JmsProviderMarshaler#createMessage(javax.jbi.messaging.MessageExchange,
javax.jbi.messaging.NormalizedMessage, javax.jms.Session)
	 */
	public Message createMessage(MessageExchange exchange, NormalizedMessage
in, Session session) throws Exception {
		TextMessage text = session.createTextMessage();
		text.setText(transformer.contentToString(in));
		
		for (Iterator iter = in.getPropertyNames().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			Object value = in.getProperty(name);
			if (shouldIncludeHeader(in, name, value)) {
				text.setObjectProperty(name, value);
			
System.out.println("******************************************************Setting
JMS Marshaller Property: "+name+"=>"+value);
			}
		}
		return text;
	}
	
	/**
	 * @param in
	 * @param name 
	 * @param value
	 * @return
	 */
	private boolean shouldIncludeHeader(NormalizedMessage in, String name,
Object value) {
		return ((value instanceof String || value instanceof Number || value
instanceof Date)
			&& isJavaIdentifier(name));
    }
    
    private static boolean isJavaIdentifier(String s) {
        int n = s.length();
        if (n==0) return false;
        if (!Character.isJavaIdentifierStart(s.charAt(0)))
            return false;
        for (int i = 1; i < n; i++)
            if (!Character.isJavaIdentifierPart(s.charAt(i)))
                return false;
        return true;
      }
	
	/* (non-Javadoc)
	 * @see
org.apache.servicemix.jms.endpoints.JmsProviderMarshaler#populateMessage(javax.jms.Message,
javax.jbi.messaging.MessageExchange, javax.jbi.messaging.NormalizedMessage)
	 */
	public void populateMessage(Message message, MessageExchange exchange,
NormalizedMessage normalizedMessage) throws Exception {
		if (message instanceof TextMessage) {
			addNmsProperties(normalizedMessage, message);
			TextMessage textMessage = (TextMessage) message;
			Source source = new StringSource(textMessage.getText());
			normalizedMessage.setContent(source);
		} else {
			throw new UnsupportedOperationException("JMS message is not a
TextMessage");
		}
	}
	
    /**
     * @param message
     * @param jmsMessage
     * @throws JMSException
     */
    protected void addNmsProperties(NormalizedMessage message, Message
jmsMessage) throws JMSException {
        Enumeration enumeration = jmsMessage.getPropertyNames();
        while (enumeration.hasMoreElements()) {
            String name = (String) enumeration.nextElement();
            Object value = jmsMessage.getObjectProperty(name);
            message.setProperty(name, value);
        }
    }

}


I am new to servicemix.. So, I may be naive to some extent or more.. I
apologize for it and thanks in advance..
-- 
View this message in context: http://servicemix.396122.n5.nabble.com/How-to-avoid-loosing-header-between-JBI-endpoint-and-Camel-SU-tp2709458p2711139.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.

Re: How to avoid loosing header between JBI endpoint and Camel SU

Posted by Claus Ibsen <cl...@gmail.com>.
The JMS spec only allows certain types of headers to be send (they are
called properties in JMS).
And a Camel Exchange only send message body + headers over JMS. So you
gotta store what you want to send over JMS as Camel Message headers.
And basically those can only be String, numeric, and other simple types.

You can see a bit here in the section _Message format when sending_
http://camel.apache.org/jms

On Thu, Aug 26, 2010 at 3:09 PM, arshadansari27
<ar...@gmail.com> wrote:
>
> I'm trying to set a header information in a message and that header is there
> till it gets lost, when sent to the JMS Provider SU.. I'm not sure why, but
> I don't  see the header in JMS message itself..
> Here is the flow of what I'm trying to do.
>
> HTTP SU Consumer ===> Bean SU (Adding header here) ==> Validation SU ===>
> JMS Provider SU ==> Topic ==> JMS Consumer SU (Message Selector is used -
> which needs the header to do selection) ==> HTTP SU Provider.
>
> All the '===>' means Camel is used for Routing from one Su to another..
> Servicemix version is 3.3.2. Camel version is the one that comes with 3.3.2.
>
> Here's the Camel SU's DSL Code that is doing the Routing..
> from("jbi:service:http://servicemix.in2m.com/operations/updateprofile/RoutingService")
>
> .to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/MessageEnricherService?mep=in-out")
>        .process(new HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,
> true,false))
>
>
> .to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/ValidateService?mep=in-out")
>
>        .process(new
> HeaderPropertyExchange(HeaderPropertyExchange.TO_JBI,false,false))
>
>
> .to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/JMSProviderService?mep=in-only")
>        .process(new
> HeaderPropertyExchange(HeaderPropertyExchange.TO_JBI,false,true))
>
>
> .to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/ResponseGeneratorService?mep=in-out");
>
>
>
> from("jbi:service:http://servicemix.in2m.com/operations/updateprofile/DirectorConsumerService")
>
>        .process(new
> HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,true,false))
>
> .to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/DirectorService?mep=in-out")
>
>        .process(new
> HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,false,false));
>
>
> from("jbi:service:http://servicemix.in2m.com/operations/updateprofile/PortalConsumerService")
>        .process(new
> HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,true,false))
>
> .to("jbi:service:http://servicemix.in2m.com/operations/updateprofile/PortalService?mep=in-out")
>        .process(new
> HeaderPropertyExchange(HeaderPropertyExchange.TO_CAMEL,false,false));
>
>
> The Delegate Processor used above is : HeaderPropertyExchange.java. I am
> using this to copy the header to an from Properties of Message and its code
> is as follows:
> Just writing the process(Exchange exchange) code and its constructor code
>
> public HeaderPropertyExchange(int type, boolean displayOnly, boolean inOnly)
> {
>                this.type = type;
>                this.displayOnly = displayOnly;
>                this.inOnly = inOnly;
>        }
>
> public void process(Exchange exchange) {
>                try {
>
>                        if (type == TO_CAMEL && !(displayOnly)) {
>                                Message inMessage = exchange.getIn();
>                if (inMessage.getHeaders() != null) {
>
>                        Map<String, Object> headers = inMessage.getHeaders();
>
>                        Iterator<Entry<String, Object>> it = headers.entrySet().iterator();
>                        while (it.hasNext()) {
>                                Map.Entry<String, Object> pairs = it.next();
>                                exchange.setProperty(pairs.getKey(), pairs.getValue());
>                        }
>                }
>                                proceed(exchange);
>                                if(!inOnly)
>                                        {
> if (exchange.getProperties() != null) {
>
>                        Map<String, Object> properties = exchange.getProperties();
>
>                        Iterator<Entry<String, Object>> it = properties.entrySet().iterator();
>                        while (it.hasNext()) {
>                                Map.Entry<String, Object> pairs = it.next();
>                                exchange.setProperty(pairs.getKey(), pairs.getValue());
>                        }
>                }
>                                      }
>
>                        } else if (type == TO_JBI && !(displayOnly)) {
>
>                                if (exchange.getProperties() != null) {
>
>                        Map<String, Object> properties = exchange.getProperties();
>
>                        Iterator<Entry<String, Object>> it = properties.entrySet().iterator();
>                        while (it.hasNext()) {
>                                Map.Entry<String, Object> pairs = it.next();
>                                exchange.setProperty(pairs.getKey(), pairs.getValue());
>                        }
>                }
>                                proceed(exchange);
>                                if(!inOnly)
>                                        {
> Message inMessage = exchange.getIn();
>                if (inMessage.getHeaders() != null) {
>
>                        Map<String, Object> headers = inMessage.getHeaders();
>
>                        Iterator<Entry<String, Object>> it = headers.entrySet().iterator();
>                        while (it.hasNext()) {
>                                Map.Entry<String, Object> pairs = it.next();
>                                exchange.setProperty(pairs.getKey(), pairs.getValue());
>                        }
>                }
> }
>                        }
>                        if(displayOnly)
>                                proceed(exchange);
>
>                } catch (Exception e) {
>                        e.printStackTrace();
>                }
>
>  Please Help.. The JMS provider, when it sends the message on Topic, the
> header is still missing...
>
> --
> View this message in context: http://servicemix.396122.n5.nabble.com/How-to-avoid-loosing-header-between-JBI-endpoint-and-Camel-SU-tp2709458p2709458.html
> Sent from the ServiceMix - User mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus