2012-09-25 46 views
6

的端回覆消息,我想在我的行程的端部同時處理請求和響應消息。但是,我沒有看到如何訪問原始請求消息的方式。Apache的駱駝:訪問請求和在路線

我有可怕的感覺,我的一些基本概念掙扎。

這裏是DSL一個簡單的例子路線勾勒我的問題(streamCaching是整個上下文啓用):

from("activemq:queue:myQueue") 
.to("log:" + getClass().getName() + "?showOut=true") 
.to("http://localhost:8080/someBackend") 
.log("Now in.Body returns this: ${in.body} and out.Body this: ${out.body}") 
.to("log:" + getClass().getName() + "?showOut=true"); 

這裏是我的日誌的根據摘錄(換行符編輯以更好的閱讀) 。可以看到,一旦http服務器回覆,原始的SOAP消息就會丟失,並且SOAP響應對象存儲在消息的inBody中。

2012-09-25 17:28:08,312 local.bar.foo.MyRouteBuilder INFO - 
    Exchange[ExchangePattern:InOut, BodyType:byte[], 
    Body:<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header /><env:Body><urn:someRequest xmlns:urn="http://foo.bar.local/ns"></urn:someRequest></env:Body></env:Envelope>, 
    Out: null] 
2012-09-25 17:28:08,398 org.apache.camel.component.http.HttpProducer DEBUG - 
    Executing http POST method: http://localhost:8080/someBackend 
2012-09-25 17:28:09,389 org.apache.camel.component.http.HttpProducer DEBUG - 
    Http responseCode: 200 
2012-09-25 17:28:09,392 route2 INFO - 
    Now in.Body returns this: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:someResponse xmlns:ns2="http://foo.bar.local/ns"</ns2:someResponse></soap:Body></soap:Envelope> 
    and out.Body this: 
2012-09-25 17:28:09,392 local.bar.foo.MyRouteBuilder INFO - 
    Exchange[ExchangePattern:InOut, 
    BodyType:org.apache.camel.converter.stream.InputStreamCache, 
    Body:[Body is instance of org.apache.camel.StreamCache], 
    Out: null] 

我本來會期望in.body和out.body被保留在整個路線?

替代方案,我考慮:

  • 利用Correlation Identifier模式的關聯請求和迴應。但是,這是否也會保留郵件正文?另外,我的請求/回覆消息沒有用於關聯的唯一標識符。
  • 編寫自定義豆,其執行呼叫到HTTP後端,處理請求和回覆對象(但是這基本上無駱駝溶液,重新發明輪子,因此不優選的)

已經失敗的方法:

我試圖用一個處理器在我的路線結束訪問原始請求消息是這樣,沒有成功:

process(new Processor() { 
    @Override 
    public void process(Exchange exchange) throws Exception { 
     Message originalInMessage = exchange.getUnitOfWork().getOriginalInMessage(); 
     logger.debug(originalInMessage.getBody(String.class)); 
     logger.debug(exchange.getIn().getBody(String.class)); 
    } 
}); 

感謝您的幫助

+1

我面臨類似的情況,解決了:String payload = new String((byte [])exchange.getUnitOfWork()。getOriginalInMessage()。getBody()) – dkb

回答

14

簡單的原體存儲在消息中頁眉或財產,並在年底檢索:

from("activemq:queue:myQueue") 
.setProperty("origInBody", body()) 
.to("http://localhost:8080/someBackend") 

後http調用你可以訪問屬性origInBody。

12

首先,本文介紹了,非常善於在進出駱駝作品:http://camel.apache.org/using-getin-or-getout-methods-on-exchange.html

通常情況下,並不總是使用了消息,而是複製從報文中的每一步。

在你的情況,你想要的原始消息呆在身邊直到路線的終點,你可以與富集EIP繼續。 http://camel.apache.org/content-enricher.html

你的路線是這樣的:

public class MyAggregationStrategy implements AggregationStrategy { 
    public Exchange aggregate(Exchange orig, Exchange httpExchange){ 
    // if you want to check something with the Http request, you better do that here 
    if(httpExchange is not correct in some way) 
     throw new RuntimeException("Something went wrong"); 

    return orig; 
    } 
} 

AggregationStrategy aggStrategy = new MyAggregationStrategy(); 

from("activemq:queue:myQueue") 
    .enrich("http://localhost:8080/someBackend",aggStrategy) 
    .//keep processing the original request here if you like, in the "in" message 
+0

非常感謝。我使用了由「Christian Schneider」提供的解決方案,因爲它解決了我的使用案例。然而,對於更復雜的情況,我認爲AggregationStrategy是通向go.Many也感謝在FAQ條目的鏈接,就溜了我的注意。 (因爲我沒有足夠的積分,所以無法贊成,對此感到抱歉) – user1627943