2013-08-01 36 views
1

假設我想利用Camel作爲RESTful Web服務的客戶端。但不確定駱駝是否足夠適合這種工作。我也想使用http4或ahc組件,而不是cxf。Can Camel可以用作純粹的RESTful web服務客戶端嗎?

總的來說,我只需要兩種途徑:

  1. 從豆 - >馬歇爾JSON - >到AHC靜態URI - >解組 從JSON - >到豆。靜態URI的示例:ahc:http://host/api/user/create
  2. 從Bean - > marshall到Json - > to Ahc with dynamic URI - > unmarshall from Json - > to Bean。動態URI的示例:ahc:http://host/api/user/id/1

我想象有一個服務類火災的方式像這樣的路線:

UserService { 

    @Autowired 
    protected CamelContext restApiCamelContext; 

    public UserCreateResponse createUser (UserModel user) { 
     ... Camel's magick which starts create user route ... 
    } 

    public UserModel getUserById (Long id) {   
     ... the id must be placed somehow into endpoint uri: http://host:port/api/user/id/${id} ... 
     ... Camel's magick which get user by id ... 
    } 
} 

的UserService假定在Spring MVC控制器中使用。

那麼,是否有可能實現基於駱駝功能的這樣的UserService?如果是的話,那麼它會在用戶要求春季控制器的大量用戶請求的高壓下運行嗎?它可以在近百個不同的uris中正常工作嗎?

回答

0

您可以通過動態設置CamelHttpUri的消息頭來更改請求的URI。 如果您的業務邏輯很簡單,我認爲您可以創建一個簡單的駱駝路線來完成這項工作。 然後您使用camel ProducerTemplate將請求發送到駱駝路由。

0
  1. 從豆 - >馬歇爾JSON - >到AHC靜態URI - 從Json的> 和解組 - >到的Bean。
  2. 從Bean - > marshall到Json - >到 Ahc with dynamic URI - > unmarshall from Json - > to Bean。

CAMEL方法之間的差異,以()& recipientList()之處在於對方法不能作爲其中recipientList-方法可以解析駱駝的動態參數。

from("restlet:/your/some/address/{sourceId}?restletMethods=GET") 
.log("execute '${header.sourceId}' for something").to("log:WebRequestThroughput?groupSize=10") 
.beanRef("yourServiceBeanRef", "serviceMethodName") 
.marshal().json(JsonLibrary.Jackson) 
.to("http://domain/address/?bridgeEndpoint=true&throwExceptionOnFailure=true") 
.unmarshal().json(JsonLibrary.Jackson, YourResponseObject.class) 
.beanRef("anotherServiceBeanRef", "anotherMethodName"); 


from("restlet:/your/address/{sourceId}?restletMethods=GET") 
.log("execute '${header.sourceId}' for something").to("log:WebRequestThroughput?groupSize=10") 
.beanRef("yourServiceBeanRef", "serviceMethodName") 
.marshal().json(JsonLibrary.Jackson) 
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST)) 
.recipientList(simple("http://domain/address/${header.sourceId}?bridgeEndpoint=true&throwExceptionOnFailure=true")) 
.unmarshal().json(JsonLibrary.Jackson, YourResponseObject.class) 
.beanRef("anotherServiceBeanRef", "anotherMethodName");