웹 서비스 주소 지정을 사용하는 JAX-RPC 웹 서비스 애플리케이션 작성
웹 서비스 주소 지정(WS-Addressing)은 웹 서비스의 주소를 지정하고 메시지에 주소 지정 정보를 제공하는 표준 방법을 정의하여 웹 서비스 간의 상호 운영성을 돕습니다. 이 태스크에서는 WS-Addressing 엔드포인트 참조를 사용하여 액세스하는 JAX-RPC 웹 서비스를 작성하는 데 필요한 단계에 대해 설명합니다. 또한 이 태스크에서는 웹 서비스의 일환으로 Stateful 자원을 사용하는 데 필요한 추가 단계에 대해 설명합니다.
시작하기 전에
이 태스크에 설명된 단계는 WebSphere® Application Server에서 실행되는 서버 및 클라이언트에 적용됩니다.
이 태스크 정보
WS-Addressing 스펙을 사용하는 웹 서비스를 작성 중인 경우에는 이 태스크를 완료하십시오.
프로시저
결과
대상 서비스에 엔드포인트 참조를 리턴하는 웹 서비스 인터페이스 제공
다음 예제는 프로시저에서 1 - 4단계에 해당합니다. 예제에서는 IT 조직에서 프린터 네트워크를 관리하기 위해 웹 서비스를 사용할 수 있는 방법을 보여줍니다. 이 조직은 각 프린터를 엔드포인트 참조를 통해 주소 지정되는 자원으로 표시할 수도 있습니다. 다음 예제에서는 WebSphere Application Server에서 제공하는 웹 서비스 주소 지정(WS-Addressing) 및 JAX-WS를 사용하여 해당 서비스를 코딩하는 방법을 보여줍니다.
IT 조직은 CreatePrinter portType 요소를 제공하는 PrinterFactory 서비스를 구현합니다. 이 portType 요소는 논리 프린터를 표시하는 자원을 작성하기 위해 CreatePrinterRequest 메시지를 승인하고, 자원에 대한 참조인 엔드포인트 참조와 함께 응답합니다.
<wsdl:definitions targetNamespace="http://example.org/printer" ...
xmlns:pr=" http://example.org/printer">
<wsdl:types>
...
<xsd:schema...>
<xsd:element name="CreatePrinterRequest"/>
<xsd:element name="CreatePrinterResponse"
type="wsa:EndpointReferenceType"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="CreatePrinterRequest">
<wsdl:part name="CreatePrinterRequest"
element="pr:CreatePrinterRequest" />
</wsdl:message>
<wsdl:message name="CreatePrinterResponse">
<wsdl:part name="CreatePrinterResponse"
element="pr:CreatePrinterResponse" />
</wsdl:message>
<wsdl:portType name="CreatePrinter">
<wsdl:operation name="createPrinter">
<wsdl:input name="CreatePrinterRequest"
message="pr:CreatePrinterRequest" />
<wsdl:output name="CreatePrinterResponse"
message="pr:CreatePrinterResponse" />
</wsdl:operation>
</wsdl:portType>
</wsdl:definitions>
이전 예제에서 CreatePrinter 조작은 새로 작성된 프린터 자원을 표시하는 wsa:EndpointReference 오브젝트를 리턴합니다. 클라이언트는 이 엔드포인트 참조를 사용하여 프린터를 표시하는 서비스 인스턴스에 메시지를 전송할 수 있습니다.
다음 예제에서 표시된 createPrinter 메소드는 프린터 서비스에 대한 엔드포인트 참조를 작성합니다. 그런 다음 이 조작은 개별 프린터 자원 인스턴스에 대한 ID를 얻고 이를 엔드포인트 참조와 연관시킵니다. 마지막으로 createPrinter 메소드는 이제 새 프린터를 표시하는 EndpointReference 오브젝트를 W3CEndpointReference 오브젝트로 변환하고 변환된 엔드포인트 참조를 리턴합니다.
import com.ibm.websphere.wsaddressing.EndpointReferenceManager;
import com.ibm.websphere.wsaddressing.EndpointReference;
import com.ibm.websphere.wsaddressing.jaxws.EndpointReferenceConverter;
import com.ibm.websphere.wsaddressing.jaxws.W3CEndpointReference;
import javax.xml.namespace.QName;
public class MyClass {
// Create the printer
...
// Define the printer resource ID as a static constant as it is required in later steps
public static final QName PRINTER_ID_PARAM_QNAME = new QName("example.printersample",
"IBM_WSRF_PRINTERID", "ws-rf-pr" );
public static final QName PRINTER_SERVICE_QNAME = new QName("example.printer.com", "printer", "...");
public static final String PRINTER_ENDPOINT_NAME = new String("PrinterService");
public W3CEndpointReference createPrinter(java.lang.Object createPrinterRequest)
throws Exception {
// Create an EndpointReference that targets the appropriate WebService URI and port name.
EndpointReference epr = EndpointReferenceManager.createEndpointReference(PRINTER_SERVICE_QNAME,
PRINTER_ENDPOINT_NAME);
// Create or lookup the stateful resource and derive a resource
// identifier string.
String resource_identifier = "...";
// Associate this resource identifier with the EndpointReference as
// a reference parameter.
// The choice of name is arbitrary, but should be unique
// to the service.
epr.setReferenceParameter(PRINTER_ID_PARAM_QNAME,resource_identifier);
// The endpoint reference now targets the resource rather than the service.
...
return EndpointReferenceConverter.createW3CEndpointReference(epr);
}
}
이전에 설명한 웹 서비스 구현 때문에 이제 프린터 자원 인스턴스에는 엔드포인트 참조에 고유 ID가 임베드되어 있습니다. 이 ID는 웹 서비스를 대상으로 하는 후속 메시지의 SOAP 헤더에 참조 매개변수로 나타나고 웹 서비스가 수신 메시지를 적합한 프린터와 일치시키기 위해 사용할 수 있습니다.
웹 서비스가 WS-Addressing 메시지 주소 지정 특성이 있는 메시지를 받으면 WebSphere Application Server는 메시지가 애플리케이션 엔드포인트로 디스패치되기 전에 이 특성을 처리하고, 이 특성을 스레드의 메시지 컨텍스트로 설정합니다. 프린터 웹 서비스 애플리케이션은 다음 예제에 표시된 대로 WebServiceContext 오브젝트의 대상 엔드포인트와 연관된 참조 매개변수에 액세스합니다.
import com.ibm.websphere.wsaddressing.EndpointReferenceManager;
...
// Initialize the reference parameter name
QName name = new QName(..);
// Extract the String value.
String resource_identifier =
EndpointReferenceManager.getReferenceParameterFromMessageContext(PRINTER_ID_PARAM_QNAME);
웹 서비스 구현은 getReferenceParameterFromMessageContext 메소드로부터 획득한 프린터 ID를 기반으로 메시지를 적합한 프린터 인스턴스로 전달할 수 있습니다.
import javax.xml.ws.BindingProvider;
...
javax.xml.ws.Service service= ...;
Printer myPrinterProxy = service.getPort(portName, Printer.class);
javax.xml.ws.BindingProvider bp = (javax.xml.ws.BindingProvider)myPrinterProxy;
// Retrieve the request context for the BindingProvider object
Map myMap = myBindingProvider.getRequestContext();
// Associate the endpoint reference that represents the new printer to the request context
// so that the BindingProvider object now represents a specific printer instance.
myMap.put(WSADDRESSING_DESTINATION_EPR, destinationEpr);
...
이제 BindingProvider 오브젝트는 새 프린터 자원 인스턴스를
표시하고, 클라이언트가 프린터 웹 서비스를 통해 프린터에 메시지를 전송하는 데
이 오브젝트를 사용할 수 있습니다. 클라이언트가 BindingProvider 오브젝트를 호출하면
WebSphere
Application Server는 적합한 메시지 주소 지정 특성을 메시지 헤더에 추가합니다. 이 경우에는
대상 프린터 자원을 식별하는 엔드포인트 참조 내에 포함된 참조 매개변수입니다.또는 클라이언트가 다음 예제에 표시된 대로 새 프린터를 표시하도록 구성하는 JAX-RPC Stub 또는 Call 오브젝트를 사용할 수도 있습니다. Call 오브젝트의 사용은 다음 예제에 표시되어 있습니다.
import javax.xml.rpc.Call;
...
:
// Associate the endpoint reference that represents the new printer with the call.
call.setProperty(
"com.ibm.websphere.wsaddressing.WSAConstants.
WSADDRESSING_DESTINATION_EPR ", epr);
클라이언트의 관점에서 엔드포인트 참조는 불투명합니다. 클라이언트는 엔드포인트 참조 매개변수의 컨텐츠를 해석할 수 없으므로 어떤 방법으로도 이를 사용하려고 시도하면 안 됩니다. 참조 매개변수는 서비스 제공자에서 개인용 매개변수이므로 클라이언트는 엔드포인트 참조의 인스턴스를 직접 작성할 수 없습니다. 클라이언트는 서비스 제공자로부터 엔드포인트 참조를 얻은 다음(예를 들어, 제공자 팩토리 서비스를 통해) 아래와 같이 이를 사용하여 웹 서비스 조작을 엔드포인트 참조가 표시하는 엔드포인트로 직접 연결해야 합니다.
다음에 수행할 작업
- WS-Addressing을 사용하는 보안에 대한 자세한 정보는 웹 서비스 주소 지정 보안의 내용을 참조하십시오.
- 애플리케이션를 배치하십시오. 이 시나리오의 경우 클라이언트에서 WS-Addressing 특성을 지정했으므로 WebSphere Application Server에서 WS-Addressing 지원을 사용 가능하게 하도록 추가 단계를 수행하지 않아도 됩니다. 자세한 정보 및 추가 단계가 필요할 수 있는 기타 시나리오에 대해서는 JAX-RPC 애플리케이션에 대한 웹 서비스 주소 지정 지원 사용의 내용을 참조하십시오.