305.5. 例

305.5.1. Web サービスクライアント

次のルートは、リクエストのマーシャリングと、レスポンスまたはエラーのアンマーシャリングをサポートしています。

String WS_URI = "cxf://http://myserver/customerservice?serviceClass=com.example.customerservice&dataFormat=MESSAGE";
SoapJaxbDataFormat soapDF = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
from("direct:customerServiceClient")
  .onException(Exception.class)
    .handled(true)
    .unmarshal(soapDF)
  .end()
  .marshal(soapDF)
  .to(WS_URI)
  .unmarshal(soapDF);

以下のスニペットは、サービスインターフェイスのプロキシーを作成し、上記のルートへの SOAP 呼び出しを行います。

import org.apache.camel.Endpoint;
import org.apache.camel.component.bean.ProxyHelper;
...

Endpoint startEndpoint = context.getEndpoint("direct:customerServiceClient");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// CustomerService below is the service endpoint interface, *not* the javax.xml.ws.Service subclass
CustomerService proxy = ProxyHelper.createProxy(startEndpoint, classLoader, CustomerService.class);
GetCustomersByNameResponse response = proxy.getCustomersByName(new GetCustomersByName());

305.5.2. Web サービスサーバー

次のルートを使用すると、jms キュー customerServiceQueue をリッスンし、クラス CustomerServiceImpl を使用してリクエストを処理する Web サービスサーバーが設定されます。当然、customerServiceImpl はインターフェイス CustomerService を実装する必要があります。サーバークラスを直接インスタンス化する代わりに、Spring コンテキストで通常の Bean として定義できます。

SoapJaxbDataFormat soapDF = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
CustomerService serverBean = new CustomerServiceImpl();
from("jms://queue:customerServiceQueue")
  .onException(Exception.class)
    .handled(true)
    .marshal(soapDF)
  .end()
  .unmarshal(soapDF)
  .bean(serverBean)
  .marshal(soapDF);