ReliableClientImpl.java
001 package examples.webservices.reliable;
002 
003 // Import the clientgen-generated implementation of the JAX-RPC stub
004 // for the ReliableHelloWorldService Web Service
005 import examples.webservices.reliable.client.ReliableHelloWorldPortType;
006 
007 import java.rmi.RemoteException;
008 
009 import javax.jws.WebMethod;
010 import javax.jws.WebService;
011 import javax.jws.soap.SOAPBinding;
012 import javax.xml.rpc.Stub;
013 
014 import weblogic.jws.WLHttpTransport;
015 import weblogic.jws.ServiceClient;
016 import weblogic.jws.ReliabilityErrorHandler;
017 import weblogic.wsee.reliability.WsrmUtils;
018 import weblogic.wsee.reliability.ReliabilityErrorContext;
019 
020 /**
021  * Standard JWS annotation that specifies that the name of the Web Service is
022  * "ReliableClientService", its public service name is "ReliableClientService".
023  */
024 @WebService(name="ReliableClientPortType", serviceName="ReliableClientService")
025 
026 /**
027  * Standard JWS annotation that specifies this is a document-literal-wrapped
028  * Web Service
029  */
030 @SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
031              use=SOAPBinding.Use.LITERAL,
032              parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
033 
034 /**
035  * WebLogic-specific JWS annotation that specifies the port name is
036  * "ReliableClientServicePort", and the context path and service URI used to
037  * build the URI of the Web Service is "ReliableClient/ReliableClient"
038  */
039 @WLHttpTransport(portName="ReliableClientServicePort",
040                  contextPath="ReliableClient"
041                  serviceUri="ReliableClient")
042 
043 /**
044  * Implementation of a source Web Service that reliably invokes an operation 
045  * on a reliable destination WebLogic Web Service.
046  <p>
047  * This Web Service uses the WebLogic-specific @ServiceClient JWS annotation 
048  * to specify the Web Service that it is going to invoke.  All the other JWS 
049  * annotations of this example are the ones you typically see in a Web Service.
050  <p>
051  * Note, this web service is non-conversational (no @Conversational annotation)
052  * and thus, each invocation of a method on this JWS could potentially be
053  * serviced by a different object instance than any other invocation. You should
054  * not depend on member variables holding their values for any longer than the
055  * current method is executing.
056  <p>
057  * Since the ReliableHelloWorldPortType member variable (port) is just like any
058  * other member variable, it too, could be different for each invocation of a
059  * method on this service. The stub holds properties that identify the reliable
060  * sequence we'll use to deliver the helloWorld request to the
061  * ReliableHelloWorld service. Once we leave any given method, these properties
062  * are lost, and we'll end up using another sequence the next time we want
063  * to send a reliable message.
064  <p>
065  * This service will send messages to the ReliableHelloWorld web service using
066  * the WS-RM version indicated by that service (in our case WS-RM 1.1)
067  
068  @author Copyright (c) 2009 by BEA Systems, Inc. All Rights Reserved.
069  */
070 public class ReliableClientImpl {
071 
072   /**
073    * Annotation that specifies the reliable Web Service whose operation is
074    * to be invoked. The ReliableHelloWorldPortType member variable is a
075    * JAX-RPC Stub instance that is used to make web service invocations on the
076    * ReliableHelloWorld service. Note that the stub holds contextual information
077    * in the form of properties, that track the reliable sequence used to deliver
078    * any requests generated by invocations on the stub.
079    */
080   @ServiceClient(serviceName="ReliableHelloWorldService")
081   private ReliableHelloWorldPortType port;
082 
083   /**
084    * The WebMethod annotation exposes the subsequent method as a public
085    * operation on the Web Service.
086    */
087   @WebMethod
088   public void callHelloWorld(String input, String serviceUrl)
089       throws RemoteException {
090     System.out.println("Calling ReliableHelloWorldService.helloWorld");
091     // Tell reliable messaging that this is the final message to send on this
092     // sequence (we're only sending one). For more information, see notes at the
093     // end of this class
094     WsrmUtils.setFinalMessage((Stub)port);
095     // Now, send the helloWorld request message to the reliable service.
096     // Note, the reliable messaging subsystem will negotiate a reliable sequence
097     // using the WS-RM 1.1 protocol on behalf of this client service, and
098     // complete the handshaking process before sending the helloWorld message
099     // to the ReliableHelloWorld service.
100     port.helloWorld(input);
101     System.out.println("Invoked the ReliableHelloWorld.helloWorld operation "+
102         "reliably.");
103     // Leaving this method. 'port' and any other member variable could be new
104     // and/or different the next time we enter a method on this JWS. Note, that
105     // the reliable sequence (by virtue of setFinalMessage above) will be
106     // completed, and disposed of shortly after the helloWorld method returns.
107   }
108 
109   //
110   // Notes on sequence lifecycle, and the use of WsrmUtils.setFinalMessage
111   //
112   // The comments at the class declaration level of this class indicated that
113   // our reliable sequence for sending helloWorld will be tracked by properties
114   // in the stub (port). Since helloWorld is the only invocation we'll
115   // make on this stub in callHelloWorld, and invoking another method will give
116   // us a new/different Stub/sequence, we tell the reliable messaging
117   // subsystem that this is the final message to be sent on this sequence.
118   // This allows the reliable messaging subsystem to proactively and
119   // efficiently complete the sequence, and recover any resources that were
120   // associated with it.
121   //
122   // It is good/recommended practice to keep track of when you are
123   // sending the final message on a sequence/stub and to use setFinalMessage to
124   // indicate the final message.
125 
126   /**
127    * This method is a callback method that can give the developer insight to
128    * any failed requests. If a reliable request fails to be delivered, you will
129    * get a callback here. It is strongly recommended to always have a
130    * ReliabilityErrorHandler defined in your client service. Examples of
131    * delivery failures would be:
132    <ul>
133    *   <li>Destination server is down, longer than the sequence timeout allows</li>
134    *   <li>Destination service not deployed or unavailable on destination server, longer than the sequence timeout allows</li>
135    *   <li>Request is invalid in some way. Here it is very important to have robust error handling. It would be recommended to 'repair' the request by changing its data fields as appropriate, and resending the request on a new sequence.</li>
136    </ul>
137    */
138   @ReliabilityErrorHandler(target="port")
139   public void onReliabilityError(ReliabilityErrorContext context) {
140 
141     // TODO: The implementation of this method is entirely up to you.
142     // TODO: Add robust error handling using the information in the
143     //       given ReliabilityErrorContext, including the request SOAPMessage.
144 
145     System.out.println("Reliable messaging failed for operation: " + context.getOperationName());
146   }
147 }