예제: 일반 웹 서비스 자원 인스턴스에 액세스하도록 JAX-WS 웹 서비스 주소 지정 API를 사용하는 웹 서비스 작성

웹 서비스를 사용하여 관리할 프린터 네트워크가 있는 IT 조직을 고려해 보십시오. 이 조직은 각 프린터를 엔드포인트 참조를 통해 주소 지정되는 자원으로 표시할 수도 있습니다. 이 예제에서는 WebSphere® Application Server에서 제공하는 JAX-WS 웹 서비스 주소 지정(WS-Addressing) API(Application Programming Interface)를 사용하여 해당 서비스를 코딩하는 방법을 보여줍니다.

대상 서비스에 엔드포인트 참조를 리턴하는 웹 서비스 인터페이스 제공

IT 조직은 CreatePrinter portType 요소를 제공하는 PrinterFactory 서비스를 구현합니다. 이 portType 요소는 논리 프린터를 표시하는 자원을 작성하기 위해 CreatePrinterRequest 메시지를 승인하고, 자원에 대한 참조인 엔드포인트 참조와 함께 응답합니다.

이러한 PrinterFactory 서비스에 대한 WSDL 정의에는 다음 코드가 포함될 수 있습니다.
<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를 얻습니다. 그런 다음, 조작은 프린터 서비스에 대한 엔드포인트 참조를 작성하고 프린터 ID를 엔드포인트 참조에 연관시킵니다. 마지막으로 createPrinter 메소드는 엔드포인트 참조를 리턴합니다.

import javax.xml.ws.wsaddressing.W3CEndpointReference;
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;

import javax.xml.namespace.QName;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

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_SERVICE_QNAME = new QName("example.printer.com", "printer", "..."); 
    public static final QName PRINTER_ENDPOINT_NAME = new QName("example.printer.com", "PrinterService", "...");
    
    public W3CEndpointReference createPrinter(java.lang.Object createPrinterRequest)
    {
     Document document = ...;

     // 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.
     Element element = document.createElementNS("example.printersample",
             "IBM_WSRF_PRINTERID");
     element.appendChild( document.createTextNode(resource_identifier) );
     ...
     
     // Create an EndpointReference that targets the appropriate WebService URI and port name.
     // Alternatively, the getEndpointReference() method of the MessageContext can be used.
     W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
     builder.serviceName(PRINTER_SERVICE_QNAME);
     builder.endpointName(PRINTER_ENDPOINT_NAME);
     builder.referenceParameter(element);
     
     // The endpoint reference now targets the resource rather than the service.
     return builder.build();
    }
}

대상 서비스를 웹 서비스 자원 인스턴스의 수신 메시지와 일치하도록 확장

이전에 설명한 웹 서비스 구현 때문에 이제 프린터 자원 인스턴스에는 엔드포인트 참조에 고유 ID가 임베드되어 있습니다. 이 ID는 웹 서비스를 대상으로 하는 후속 메시지의 SOAP 헤더에 참조 매개변수로 나타나고 웹 서비스가 수신 메시지를 적합한 프린터와 일치시키기 위해 사용할 수 있습니다.

웹 서비스가 WS-Addressing 메시지 주소 지정 특성이 있는 메시지를 받으면 WebSphere Application Server는 메시지가 애플리케이션 엔드포인트로 디스패치되기 전에 이 특성을 처리하고, 이 특성을 스레드의 메시지 컨텍스트로 설정합니다. 프린터 웹 서비스 애플리케이션은 다음 예제에 표시된 대로 WebServiceContext 오브젝트의 대상 엔드포인트와 연관된 참조 매개변수에 액세스합니다.

@Resource
private WebServiceContext context;
...
List list = (List) context.getMessageContext().get(MessageContext.REFERENCE_PARAMETERS);

애플리케이션이 WS-Addressing 스펙의 2004/08 버전을 사용하는 경우 IBM 독점 API를 사용하여 다음 예제에서 설명한 대로, 메시지 매개변수를 검색하십시오.

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);

웹 서비스 구현은 프린터 ID에 기반하여 메시지를 적절한 프린터 인스턴스로 전달할 수 있습니다.

메시지를 엔드포인트로 전송하기 위해 엔드포인트 참조 사용

클라이언트는 서비스에서 리턴된 엔드포인트 참조를 사용하여 다음 예제에서 보여준 대로 프린터에 대한 JAX-WS 프록시를 작성합니다.
javax.xml.ws.Service jaxwsServiceObject= ...;
W3CEndpointReference epr = ...;
...
Printer myPrinterProxy = jaxwsServiceObject.getPort(epr, Printer.class, new AddressingFeature());
이제 프록시 오브젝트는 새 프린터 자원 인스턴스를 표시하고, 클라이언트가 프린터 웹 서비스를 통해 프린터에 메시지를 전송하는 데 이 오브젝트를 사용할 수 있습니다. 클라이언트가 서비스를 호출하면 WebSphere Application Server는 적합한 메시지 주소 지정 특성을 메시지 헤더에 추가합니다. 이 경우에는 대상 프린터 자원을 식별하는 엔드포인트 참조 내에 포함된 참조 매개변수입니다.

클라이언트의 관점에서 엔드포인트 참조는 불투명합니다. 클라이언트는 엔드포인트 참조 매개변수의 컨텐츠를 해석할 수 없으므로 어떤 방법으로도 이를 사용하려고 시도하면 안 됩니다. 참조 매개변수는 서비스 제공자에서 개인용 매개변수이므로 클라이언트는 엔드포인트 참조의 인스턴스를 직접 작성할 수 없습니다. 클라이언트는 서비스 제공자로부터 엔드포인트 참조를 얻은 다음(예를 들어, 제공자 팩토리 서비스를 통해) 아래와 같이 이를 사용하여 웹 서비스 조작을 엔드포인트 참조가 표시하는 엔드포인트로 직접 연결해야 합니다.


주제 유형을 표시하는 아이콘 참조 주제



시간소인 아이콘 마지막 업데이트 날짜: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=xwbs_wsa_jaxws
파일 이름:xwbs_wsa_jaxws.html