4 min read

Service Component Architecture

We have been creating IT assets in the form of programs and codes since many years, and been implementing SOA architecture. This doesn’t mean that we follow a big bang approach and throw away all old assets in place of new. Instead, the success of any SOA effort depends largely on how we can make the existing assets co-exist with new architecture principles and patterns. To this end, Service Component Architecture (SCA) aims at creating new and transforms existing, IT assets into re-usable services more easily. These IT assets can then be rapidly adapted to changing business requirements. In this section, we will introduce SCA and also look into some working samples for the same.

What is SCA?

SCA introduces the notion of services and references. A component which implements some business logic offers their capabilities through service-oriented interfaces. Components may also consume functionality offered by other components through service-oriented interfaces, called service references. If you follow SOA best practices, you will perhaps appreciate the importance of fine-grained tight coupling and coarse-grained loose coupling between components. SCA composition aids recursive assembly of coarse-grained components out of fine-grained tightly coupled components. These coarse-grained components can even be recursively assembled to form higher levels of coarse-grained components. In SCA, a composite is a recursive assembly of fine-grained components. All these are shown in the SCA assembly model in the following screenshot:

SOA with Service Component Architecture and Enterprise Service Bus

Apache Tuscany SCA Java

Apache Tuscany SCA is a sub-project within open-source Apache Tuscany, which has got a Java implementation of SCA. Tuscany SCA is integrated with Tomcat, Jetty, and Geronimo.

SCA Java runtime is composed of core and extensions. The core wires functional units together and provides SPIs that extensions can interact with. Extensions enhance SCA runtime functionality such as service discovery, reliability, support for transport protocols, and so on.

Tuscany SCA Java is available for download at: http://incubator.apache.org/tuscany/sca-java.html.

SCA Sample Using Tuscany SCA Java

The sample here provides a single booking service with a default SCA (java) binding. The BookingAgentServiceComponent exercises this component by calling three other components that is, FlightServiceComponent, HotelServiceComponent, and CabServiceComponent as shown in the BookingAgent SCA assembly diagram shown below:

SOA with Service Component Architecture and Enterprise Service Bus

Code the Sample Artifacts

The sample consists of two sets of artifacts. The first set is the individual fine-grained service components. The second set is the coarse-grained service component, which wires the referenced fine-grained service components.

Code Fine-Grained Service Components

There are three fine-grained service components whose code is self explanatory and are listed below:

FlightServiceComponent

public interface IFlightService{ 
String bookFlight(String date, int seats, String flightClass);
}
public class FlightServiceImpl implements IFlightService{
public String bookFlight(String date, int seats, String flightClass){
System.out.println("FlightServiceImpl.bookFlight...");
return "Success";
}
}

HotelServiceComponent

public interface IHotelService{ 
String bookHotel(String date, int beds, String hotelClass);
}
public class HotelServiceImpl implements IHotelService{
public String bookHotel(String date, int beds, String hotelClass){
System.out.println("HotelServiceImpl.bookHotel...");
return "Success";
}
}

CabServiceComponent

public interface ICabService{ 
String bookCab(String date, String cabType);
}
public class CabServiceImpl implements ICabService{
public String bookCab(String date, String cabType){
System.out.println("CabServiceImpl.bookCab...");
return "Success";
}
}

Code BookingAgent Service Component

BookingAgentServiceComponent depends on three referenced service components, which are the fine-grained service components listed previously. They are initialized by the dependency injection by the SCA runtime. Also, for the actual business method invocation, the call is delegated to the referenced service components as shown in the bookTourPackage method in the following code:

import org.osoa.sca.annotations.Reference;
public class BookingAgentServiceComponent implements IBookingAgent{
private IFlightService flightService;
private IHotelService hotelService;
private ICabService cabService;
@Reference
public void setFlightService(IFlightService flightService) {
this.flightService = flightService;
}
@Reference
public void setHotelService(IHotelService hotelService) {
this.hotelService = hotelService;
}
@Reference
public void setCabService(ICabService cabService) {
this.cabService = cabService;
}
public String bookTourPackage(String date,
int people, String tourPack){
System.out.println("BookingAgent.bookTourPackage...");
String flightBooked = flightService.bookFlight(date, people, tourPack);
String hotelBooked = hotelService.bookHotel(date, people, tourPack);
String cabBooked = cabService.bookCab(date, tourPack);
if((flightBooked.equals("Success")) && (hotelBooked.equals("Success")) && (cabBooked.equals("Success"))){
return "Success";
}
else{
return "Failure";
}
}
}

Code BookingAgent Client

The BookingAgentClient first creates an instance of SCADomain and then gets a reference of the BookingAgentServiceComponent using the name of the configured service component. Then it executes the business method, bookTourPackage.

import org.apache.tuscany.sca.host.embedded.SCADomain;
public class BookingAgentClient{
public static void main(String[] args) throws Exception {
SCADomain scaDomain = SCADomain.newInstance("BookingAgent.composite");
IBookingAgent bookingAgent = scaDomain.getService(IBookingAgent.class, "BookingAgentServiceComponent");
System.out.println("BookingAgentClient.bookingTourPackage...");
String result = bookingAgent.bookTourPackage("20Dec2008", 5, "Economy");
System.out.println("BookingAgentClient.bookedTourPackage : "+ result);
scaDomain.close();
}
}

Build and Run the SCA Sample

You can download the required code for this article from http://www.packtpub.com/files//code/3216_Code.zip. Unzip the file and the code of our interest is in the folder 3216_04_Code. There is also a README.txt file, which gives detailed steps to build and run the samples.

To build the sample in a single command, it is easy for you to go to ch04sca folder and execute the following command:

cd ch04sca
ant

Now, you can execute the BookingAgentClient program by executing:

ant run

You can see that the BookingAgentServiceComponent will delegates calls to book individual line items to the referred service components and if all the individual bookings are done right, the overall transaction is “success”. The following figure shows the screenshot of such a success scenario:

SOA with Service Component Architecture and Enterprise Service Bus

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here