この章では、Java API for XML Web Services (JAX-WS)用のWebLogic Webサービス・クライアントを開発するためのベスト・プラクティスについて説明します。
表7-1では、各ベスト・プラクティスとその例を示します。ベスト・プラクティスについては、このドキュメント内で後で詳しく説明します。
その他のベスト・プラクティスについては、次の項を参照してください。
非同期Webサービス・クライアントの開発の際のベスト・プラクティスについては、第11章「非同期Webサービス・クライアントを開発するためのロードマップ」を参照してください。
信頼性のあるWebサービス・クライアントの開発の際のベスト・プラクティスについては、第13章「信頼性のあるWebサービスとクライアントを開発するためのロードマップ」を参照してください。
|
注意: 次の表でクライアント・インスタンスは、ポートまたはディスパッチ・インスタンスです。 |
表7-1 Webサービス・クライアントを開発するためのロードマップ
| ベスト・プラクティス | 説明 |
|---|---|
|
クライアント・インスタンスの使用と同期させます。 |
クライアント・インスタンスは必要なときに作成します。長い間保存しないようにしてください。 |
|
クライアントIDを含む機能の保管されたリストを使用して、クライアント・インスタンスを作成します。 |
クライアントIDが含まれるWebサービス・クライアント・インスタンスのすべての機能を定義し、クライアント・インスタンスを作成するたびに異なることがないようにします。例:
|
|
クライアントIDを明示的に定義します。 |
注意: クライアントIDは明示的に定義することを強くお薦めします。明示的に定義しないと、サーバーによって自動的にクライアントIDが生成されますが、ユーザーにとってわかりにくい場合があります。 |
|
処理が完了したら、クライアント・インスタンスを明示的に閉じます。 |
例:
明示的に閉じないと、クライアント・インスタンスはユーザーがスコープ外になった時点で自動的に閉じられます。 注意: クライアントIDは、コンテナ(WebアプリケーションまたはEJB)が非アクティブ化されるまで、登録済みで可視の状態を維持します。詳細は、「クライアント・アイデンティティのライフサイクル」を参照してください。 |
次の例では、Webサービス・クライアントを開発するためのベスト・プラクティスを示します。
例7-1 Webサービス・クライアントのベスト・プラクティスの例
import java.io.IOException;
import java.util.*;
import javax.servlet.*;
import javax.xml.ws.*;
import weblogic.jws.jaxws.client.ClientIdentityFeature;
/**
* Example client for invoking a web service.
*/
public class BestPracticeClient
extends GenericServlet {
private BackendServiceService _service;
private WebServiceFeature[] _features;
private ClientIdentityFeature _clientIdFeature;
@Override
public void init()
throws ServletException {
// Create a single instance of a web service as it is expensive to create repeatedly.
if (_service == null) {
_service = new BackendServiceService();
}
// Best Practice: Use a stored list of features, per client ID, to create client instances.
// Define all features for the web service client instance, per client ID, so that they are
// consistent each time the client instance is created. For example:
// _service.getBackendServicePort(_features);
List<WebServiceFeature> features = new ArrayList<WebServiceFeature>();
// Best Practice: Explicitly define the client ID.
// TODO: Maybe allow ClientIdentityFeature to store other features, and
// then create new client instances simply by passing the
// ClientIdentityFeature (and the registered features are used).
_clientIdFeature = new ClientIdentityFeature("MyBackendServiceClient");
features.add(_clientIdFeature);
// Set the features used when creating clients with
// the client ID "MyBackendServiceClient". The features are stored in an array to
// reinforce that the list should be treated as immutable.
_features = features.toArray(new WebServiceFeature[features.size()]);
}
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// ... Read the servlet request ...
// Best Practice: Synchronize use of client instances.
// Create a web service client instance to talk to the backend service.
// Note, at this point the client ID is 'registered' and becomes
// visible to monitoring tools such as the Administration Console and WLST.
// The client ID *remains* registered and visible until the container
// (the Web application hosting our servlet) is deactivated (undeployed).
//
// A client ID can be used when creating multiple client instances (port or Dispatch client).
// The client instance should be created with the same set of features each time, and should
// use the same service class and refer to the same port type.
// A given a client ID should be used for a given port type, but not across port types.
// It can be used for both port and Dispatch clients.
BackendService port =
_service.getBackendServicePort(_features);
// Set the endpoint address for BackendService.
((BindingProvider)port).getRequestContext().
put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:7001/BestPracticeService/BackendService");
// Print out the explicit client ID, and compare it to the client ID
// that would have been generated automatically for the client instance.
showClientIdentity();
// Make the invocation on our real port
String request = "Make a cake";
System.out.println("Invoking DoSomething with request: " + request);
String response = port.doSomething(request);
System.out.println("Got response: " + response);
res.getWriter().write(response);
// Best Practice: Explicitly close client instances when processing is complete.
// If not closed, the client instance will be closed automatically when it goes out of
// scope. Note, this client ID will remain registered and visible until our
// container (Web application) is undeployed.
((java.io.Closeable)port).close();
}
/**
// Print out the client's full ID, which is a combination of
// the client ID provided above and qualifiers from the application and
// Web application that contain the client. Then compare this with the client ID that
// would have been generated for the client instance if not explicitly set.
//
private void showClientIdentity()
throws IOException {
System.out.println("Client Identity is: " + _clientIdFeature.getClientId());
// Create a client instance without explicitly defining the client ID to view the
// client ID that is generated automatically.
ClientIdentityFeature dummyClientIdFeature =
new ClientIdentityFeature(null);
BackendService dummyPort =
_service.getBackendServicePort(dummyClientIdFeature);
System.out.println("Generated Client Identity is: " +
dummyClientIdFeature.getClientId());
// Best Practice: Explicitly close client instances when processing is complete.
// If not closed, the client instance will be closed automatically when it goes out of
// scope. Note, this client ID will remain registered and visible until our
// container (Web application) is undeployed.
((java.io.Closeable)dummyPort).close();
}
@Override
public void destroy() {
}
}