6 Replies Latest reply on Apr 24, 2008 9:13 AM by willem.jiang

    Mechanism for passing information from Consumer to Producer in and Endpoint

    eamdwyer

      Is there a way of passing information from the a camel endpoint Consumer to the Producer?

      For example if I set information in the Exchange while running in the Consumer can I be guaranteed that the information I set will be available in the Producer? Also would that information also be available in the Exchange parameter passed to the a camel org.apache.camel.processor.Interceptor process() method?

       

      Or is setting properties in the Exchange a bad idea, period?

        • 1. Re: Mechanism for passing information from Consumer to Producer in and Endp
          jstrachan

          Could you give a more concrete example of what you mean by any chance?

           

          When a producer is sending an Exchange to some endpoint (where it'll get processed typically by a consumer), the consumer can modify the Exchange setting properties, or it can set the OUT message.

           

          So if you are a producer and you wanna see what is returned by the consumer you do something like this in Java....

           

          class Producer implements Processor {
            ...
          
            Exchange exchange = endpoint.createExchange();
            consumer.process(exchange);
            
            // now lets see if we have any output...
            Message out = exchange.getOut(false); // lets not create it if its not present
            if (out != null) { ....
              /// lets process it
            }
          }
          

           

          • 2. Re: Mechanism for passing information from Consumer to Producer in and Endp
            eamdwyer

            My scenario is a bit different to the one you describe. I am using a modified version of the basic CXF example from the kits where a cxf client invokes on a camel-cxf soap-over-http endpoint and those requests are then forwarded on to a camel-cxf soap-over-jms endpoint.

             

            For this, the cxf client invokes on a camel-cxf soap-over-http endpoint and first hits the camel cxf endpoint consumer (this is as much a statement as well as a place where you can confirm if I have the architecture/terminology correct in my head;) ). I also have cxf interceptors loaded at that point to process in the request from the client. Those interceptors will set up some data based on the message it receives.

             

            Camel then routes this request on to the soap-over-jms endpoint so to get there an invocation goes through the camel exchange and enters the camel-cxf endpoint producer. I would like at this stage, i.e. in the producer, to put the data setup in the consumer onto the outbound request, but I dont know how to get the data from the consumer.

             

            Also it would be nice if the data setup in the consumer could be available in a camel interceptor if I had on in my route.

            • 3. Re: Mechanism for passing information from Consumer to Producer in and Endp
              wtam

              I don't see anything wrong with putting the data in the Exchange.  Is the data is guaranteed to be there when the producer reads the exchange?  I believe so.  Have you tried it to see if it works?  I am not too familiar with how the Camel interceptor works.  Maybe someone could provide information on Camel interceptor but I think Camel interceptor can access the Exchange as well.

              • 4. Re: Mechanism for passing information from Consumer to Producer in and Endp
                eamdwyer

                test

                 

                Edited by: eamdwyer on Apr 22, 2008 10:31 AM

                • 5. Re: Mechanism for passing information from Consumer to Producer in and Endp
                  eamdwyer

                  Some psuedo code to try illustrate the problem :

                  if you could imagine that my Camel Cxf Soap over HTTP endpoint has a cxf type MyInInterceptor and a cxf type MyOutInterceptor and they are configured to intercept Soap messages going in and out of this endpoint.

                   

                  The MyInInterceptor is part of the Camel CXF Consumer invoation chain and intercepts the request from the client. The cxf handleMesage code for this interceptor could look like

                   

                  public void handleMessage(SoapMessage msg) throws Fault {

                   

                  String userName = getUserNameFromSoapMessage(msg);

                  String password = getPasswordFromSoapMessage(msg);

                  String token = authenticate(userName, password);

                   

                  //Now how can I make this token available to the outbout MyOutInterceptor??

                   

                  //Can I do something like

                  Exchange myExchange = msg.getExchange();

                  myExchange.setProperty("token", token);

                  // I know that myExchange above is of type CxfExchange and not a Camel Exchange, but the two objects

                  //are linked together in code AT THE MOMENT - will they always be?

                  .... // rest of handleMessage

                   

                  }

                   

                  MyOutInterceptor would look like

                  public void handleMessage(SoapMessage msg) throws Fault {

                   

                  Exchange myExchange = msg.getExchange();

                  String token = (String)myExchange.getProperty("token");

                  //add the token to the out bound message

                  .... // rest of handleMessage

                   

                  }

                   

                  In addition to this a CAMEL InterceptorProcessor could then possible have

                  void process(Exchange exchange) {

                   

                  Exchange myExchange = exchange.getExchange();

                  String token = (String)myExchange.getProperty("token");

                  // Do some logic in this processor based on the token value

                   

                  }

                   

                  Edited by: eamdwyer on Apr 22, 2008 10:28 AM

                  • 6. Re: Mechanism for passing information from Consumer to Producer in and Endp
                    willem.jiang

                    If you have a chance to look at the CamelInvoker[1]'s  Object invoke(Exchange exchange, Object o) method, you can get the answer

                    The parameter could be an instance of List class or an Object due to the WSDL operation's parameter definition.

                     

                    You can also find the code example[2] to should how to set the CXF producer invocation parameter.

                     

                    https://svn.apache.org/repos/asf/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CamelInvoker.java

                    https://svn.apache.org/repos/asf/activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerTest.java