Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Components

Fabric3 is built on Service Component Architecture (SCA). In SCAIn Fabric3, application code is organized as a set of components implemented as POJOs. A component may have one or more services it exposes to clients and may contain references to other services it depends on. A component may be implemented as a Java class. The following illustrates a calculator component written in Java (taken from the samples) that is wired to operand services:

Code Block
languagejava
 public@Component 
public class CalculatorServiceImpl implements CalculatorService {

    @Reference
    protected AddService addService;

    @Reference
    protected SubtractService subtractService;

    @Reference
    protected MultiplyService multiplyService;

    @Reference
    protected DivideService divideService;
  
    public double add(double n1, double n2) {
        return addService.add(n1, n2);
    }
    public double subtract(double n1, double n2) {
        return subtractService.subtract(n1, n2);
    }
    public double multiply(double n1, double n2) {
        return multiplyService.multiply(n1, n2);
    }
    public double divide(double n1, double n2) {
        return divideService.divide(n1, n2);
    }
}

...

Bindings

Components can provide one or more services to clients. Clients may be remote or local. For remote clients, a component service can be exposed as an endpoint using a binding such as WS-*, JAX-RS or JMS.

Image Added

Fabric3 supports native JAX-RS annotations for binding a component service as and endpoint:

<composite xmlns='http://docs.oasis-open.org/ns/opencsa/sca/200912'
Code Block
Code Block
languagexml
languagexml
<component name='CalculatorService'>
   <implementation.java class="org.sample.CalculatorServiceImpl"/>
</component>

Composites

Applications are composed of one or more composites.  A composite is an XML file that contains a set of component definitions:

java
@Path("/")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
@Component 
public class CalculatorServiceImpl implements CalculatorService {

    @Reference
    protected AddService addService;

    @Reference
    protected SubtractService subtractService;

    @Reference
   targetNamespace='urn:tempuri.org'
   protected MultiplyService multiplyService;

    @Reference
  name='SampleComposite'>  protected DivideService divideService;
 <component name='CalculatorService'>
    @GET
   <implementation.java class="org.sample.CalculatorServiceImpl"/> @Path("/{formula}")
    public  </component>

</composite>

A composite is similar to a Spring application context but with several important differences:

  • An application can be comprised of 1..n composites that are peers or nested in a top-level composite.
  • Composites define visibility boundaries for encapsulation. It is not possible to wire to components within a composite unless they are explcitly made visibile outside the composite through promotion. Promotion is similar to making a method public on a Java class.  
  • A composite may be distributed and span multiple runtimes. In this case, wiring will be remote.

Bindings

Components can provide one or more services to clients. Clients may be remote or local. For remote clients, a component service can be exposed as an endpoint using a binding such as WS-*, JAX-RS or JMS.

...

String calculate(@PathParam("formula") String formula) {
		// ...
	}

}

Fabric3 also provides annotations for binding services to other transports. The WebServiceBinding annotation exposes the calculator service as a WS-* endpoint:

Code Block
languagejava
@WebServiceBinding(uri = "CalculatorService")
public class CalculatorServiceImpl implements CalculatorService {

	// ...
 
}

 

The following demonstrates how to expose an endpoint to a JMS queue:

Code Block
languagexmljava
<composite ...>

    <component name='TargetComponent'>
        <implementation.java class="org.sample.TargetComponent"/>
        <service name="TargetService"> 	   
  @Component
@JMS(@JMSConfiguration(destination = "Queue"))
public class ConsumerImpl implements Consumer {
    public String onMessage(String message) {
        return message;
    }
}

 <binding.jms>
      

Wires

A component can act as a client to a service by wiring a reference.

Image Added

The following shows how to wire a reference in a composite:

Code Block
languagejava
@Component 
public class CalculatorServiceImpl implements CalculatorService {

      <destination jndiName="SampleQueue"/>@Reference
	@Target("AddService")	
    protected AddService addService;

  
           </binding.jms>
   	// ...
}
 

In the above example, the wire from CalculatorServiceImpl to AddService can be local or remote (possibly passing through a message queue) depending on if the two components are collocated. For distributed services, bindings can be specified on references:

 

Code Block
languagejava
@Component 
public class CalculatorServiceImpl implements CalculatorService {

    @Reference
	@ZeroMQ("AddService")	
    </service>protected AddService addService;

 <	//component> 
</composite>

Wires

A component can act as a client to a service by wiring a reference.

 

Image Removed

...

...
}

The above example wires CalculatorServiceImpl to AddService using a ZeroMQ socket. Note that IP addresses and port bindings do not need to be specified - these will be resolved transparently to the application by the Fabric3 runtime.

Composites

Components can be defined using annotations (in which case they will be scanned by the runtime) or by XML or a Java-based DSL. If you prefer to define components using one of the latter methods, you must create a composite containing the components.  A composite can be defined by an XML file containing a set of component definitions:

Code Block
languagexml
<composite xmlns='http://docs.oasis-open.org/ns/opencsa/sca/200912'
           targetNamespace='urn:tempuri.org'
           name='SampleComposite'>

    <component name='CalculatorService'>
        <implementation.java class="org.sample.CalculatorServiceImpl"/>
	    <reference name="addService" target="AddService"/></component>

</composite>

Alternatively, a composite can be defined using the Fabric3 DSL:

 

Code Block
languagejava
public class CompositeProvider {
    @Provides
   .... public static Composite  </component>calculatorComposite() {
        <componentQName name='AddService'> = new QName(Namespaces.F3, "CalculatorComposite");
        JavaComponentDefinitionBuilder <f3:implementation.javacomponentBuilder class="org.sample.AddServiceImpl"/> JavaComponentDefinitionBuilder.newBuilder(CalculatorServiceImpl.class);
         return CompositeBuilder.newBuilder(name).component(componentBuilder.build()).build();
  </component>  </composite>

...

}
}

 

A composite is similar to a Spring application context but with several important differences:

  • An application can be comprised of 1..n composites that are peers or nested in a top-level composite.
  • Composites define visibility boundaries for encapsulation. It is not possible to wire to components within a composite unless they are explcitly made visibile outside the composite through promotion. Promotion is similar to making a method public on a Java class.  

Channels

Fabric3 also supports pub/sub interactions where components pass events through a channel as opposed to being directly wired. A component producer is connected to a channel, which in turn may be connected to 0..N component consumers.

...

The following shows how to create pub/sub interaction using a channel using XML:

Code Block
languagexml
<composite ...>

    <channel name="SampleChannel"/>

    <component name='SampleComponent'>
        <implementation.java/>
	    <producer name="channel" target="SampleChannel"/>
    </component>

    <component name='TargetComponent1'>
        <implementation.java/>    
        <consumer name="channel" source="SampleChannel"/>    
    </component>

    <component name='TargetComponent2'>
        <implementation.java/>    
        <consumer name="channel" source="SampleChannel"/>    
    </component>

</composite>

...