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:

Now, let’s try implementing this!
Follow these steps:
- Design a WSDL(SDX/SPDX) for both services.
- Generate code for the consumer and provider of the Service X.
- Generate code for the consumer and provider of the Service Y.
- 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.
- For all operations that will be called from Service X, add public methods after the
Additional Codesection. 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
- Add the public method
getParticipantIdentity()returning theparticipantIdentityvariable.
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:
- Add dependencies to the common and consumer projects of Service Y to the provider project of Service X.
- Implement the business code for the provider of Service X
To call operations of Service Y from Service X’s provider implementation class:
- Declare and instantiate the consumer of Service Y in
ServiceXProviderImpl.java. - Add start-up consumer code as shown in this code snippet.
- Add shut down consumer code as shown in this code snippet.
- Use the consumer object to call operations of Service Y:
/*****************************************************************
**
** Additional Code
**
** Put extra (unmodeled) fields and methods after this comment
**
*****************************************************************/
private ServiceYConsumerImpl consumerY = new ServiceYConsumerImpl();
/* 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());
}
/* 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();
}
/* 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;
}