Archiv

Artikel Tagged ‘Typed PAPI’

How To: Implementing Chained Service Calls with Generated Code

27. April 2010

How To: Implement Chained Service Calls with Generated Code and Typed PAPI

The Scenario

Let’s say you want service X to call an operation of service Y as a service consumer. Here’s the scenario in the form of a sequential diagram for you to follow:
chained_svc_call1

Now, let’s try implementing this!

Follow these steps:

  1. Design a WSDL(SDX/SPDX) for both services.
  2. Generate code for the consumer and provider of the Service X.
  3. Generate code for the consumer and provider of the Service Y.
  4. Implement the business code for the provider of the Service Y.

At this point you’ll have to make a few changes in the generated consumer code of Service Y, ServiceYConsumerImpl.java. These changes are described in this ordered list.

  1. For all operations that will be called from Service X, add public methods after the Additional Code section. Implementations of these methods simply delegate the call to the corresponding generated consumer methods

    Note that the generated consumer methods cannot be called directly, because they have default access levels and are accessible only from the same package

  2. Add the public method getParticipantIdentity() returning the participantIdentity variable.
Here is the sample code
/******************************************************************
**
**  Additional Code
**
**  Put extra (unmodeled) fields and methods after this comment
**
*********************************************************************/

public ElementYResponse operationYExtern(ElementY request) throws
TechnicalException {
return operationY(request);
}

public ParticipantIdentity getParticipantIdentity() {
return participantIdentity;
}
Now Start the Call Operation

This is what you need to do in advance:

  1. Add dependencies to the common and consumer projects of Service Y to the provider project of Service X.
  2. Implement the business code for the provider of Service X

To call operations of Service Y from Service X’s provider implementation class:

  1. Declare and instantiate the consumer of Service Y in ServiceXProviderImpl.java.
  2. /*****************************************************************
    **
    **  Additional Code
    **
    **  Put extra (unmodeled) fields and methods after this comment
    **
    *****************************************************************/
            private ServiceYConsumerImpl consumerY = new ServiceYConsumerImpl();
    
  3. Add start-up consumer code as shown in this code snippet.
  4. /* uid: _startupImpl_uid_ */
     /**
     * This is a hook that is called before the SBB is started up.
     * Add an implementation if you want to initialise other resources
      (e.g. open other connections).
     *
     *     It does not throw any checked exceptions - if there is an error,
     *     the program should either exit or throw an unchecked exception.
     */
     private void startupImpl() throws TechnicalException {
    
    	consumerY.startup(
    		     consumerY.getParticipantIdentity(),
    		     ServiceYConsumerImpl.getConsumerPolicy());
     }
    
  5. Add shut down consumer code as shown in this code snippet.
  6.     /* uid: _shutdownImpl_uid_ */
        /**
         * This is a hook that is called before the SBB is shut down.
         * Add an implementation if you want to release other resources
           (e.g. close obtained resources).
         *
         *     It does not throw any checked exceptions - if there is an error,
         *     the program should either exit or throw an unchecked exception.
         */
        private void shutdownImpl() {
        	consumerY.shutdown();
        }
    
  7. Use the consumer object to call operations of Service Y:
  8. /* uid: SoperaUID_operationX */
    /**
    * Synchronously execute a request to operation operationX, request-response
    * style (similar to JAX-WS).
    *
    * @param operationXRequest
    * the request
    * @return the response
    * @throws ParticipantException
    *             on technical error
    */
    public ElementXResponse operationX(final ElementX operationXRequest)
    	throws ParticipantException {
    	ObjectFactory ofX = new ObjectFactory();
    	ElementXResponse response = ofX.createElementXResponse();
    	// ... business code
    	try {
    		org.sopware.services.exampleuri_y.ObjectFactory of Y
    		= new org.sopware.services.exampleuri_y.ObjectFactory();
    		ElementY requestY = ofY.createElementY();
    		requestY.setIn("Request to service Y");
    		consumerY.operationYExtern(requestY);
    		} catch (TechnicalException e) {
    			throw new ParticipantException(
    				"Technical problem by Service Y call: "
    				+ e.getMessage(), e);
    		}
    		// ... business code
    		return response;
    }
    

Andrei Shakirin Entwicklung , , ,