Annotations and the DSL
Fabric3 supports defining components using annotations, XML, or a DSL. Before discussing other aspects of programming with Fabric3, we will provide an overview of working with annotations and the DSL.
Annotation Basics
Fabric3 is a modular runtime that is designed to be configured and deployed based on the specific functional requirements of the services it is hosting. For example, if a runtime is to host a service that requires JAX-RS functionality, an image can be assembled from the Fabric3 kernel and JAX-RS extensions; extensions for other technologies such as JMS or WS-* may be excluded.
Fabric3 provides Maven and Gradle plugins for assembling runtime images as part of a build. For details, see Service Delivery.
This modularity is reflected in the structure of the Fabric3 API. The main API module, org.fabric3:fabric3-api
contains base annotations for working with Java POJO. Component implementation types (such as the timer component) and bindings also have API modules that extend from the core fabric3-api
module. For example, the org.fabric3:fabric3-timer-api
contains the Timer
annotation used to declare a Java class as timer component. It may therefore be necessary to configure multiple API modules as dependencies in a project build. At runtime, Fabric3 will ensure installed API module classes are visible to applications.
Â
Throughout this manual, we refer to Fabric3 modules (JARs) using their coordinate names, e.g. org.fabric3:fabric3-api
. These coordinates correspond to [group id:artifact id
] and can be used to derive dependency configuration for various build systems such as Maven and Gradle.
Â
The following table lists the set of Fabric3 API modules:
Â
Coordinates | Description |
---|---|
org.fabric3:fabric3-api | The base package containing general annotations and API classes |
org.fabric3:fabric3-model-api | The base package for DSL classes |
org.fabric3:fabric3-binding-jms-api | Package for JMS annotations and DSL classes |
org.fabric3:fabric3-binding-file-api | Package for file binding annotations and DSL classes |
org.fabric3:fabric3-binding-rs-api | Package for JAX-RS annotations and DSL classes |
org.fabric3:fabric3-binding-ws-api | Package for JAX-WS annotations and DSL classes |
org.fabric3:fabric3-binding-zeromq-api | Package for ZeroMQ annotations and DSL classes |
org.fabric3:fabric3-junit-api | Package for JUnit annotations |
org.fabric3:fabric3-timer-api | Package for Timer annotations and DSL classes |
Using the DSL
  Â
Components and channels can also be defined using the Fabric3 DSL. To use the DSL, one or more provider classes must be packaged in an application archive under the Java package "f3". Provider classes are invoked by the runtime to programmatically create composites. The following is an example of a provider class:
Â
package f3; Â public class TestCompositeProvider { private static final QName QNAME = new QName(Namespaces.F3, "TestComposite"); Â @Provides public static Composite getComposite() { CompositeBuilder compositeBuilder = CompositeBuilder.newBuilder(QNAME); compositeBuilder.component(JavaComponentDefinitionBuilder.newBuilder(ContributionServiceImpl.class).build()); return compositeBuilder.build(); } }
Â
A provider class contains one or more static methods annotated with org.fabric3.api.annotation.model.Provides
that returns a Composite. Programmatic configuration is extremely flexible as it can be used to create conditional configuration such as changing binding settings based on the environment. To facilitate this, Fabric3 supports the org.fabric3.api.annotation.model.Environment
annotation:
Â
 public class CompositeProvider { @Provides public static Composite getScannedComposite(@Environment String environment) { if (!"production".equals(environment)) { // create a composite specific to production } else { // create another composite } } }
The Environment
annotation signals to Fabric3 to inject the name of the environment the runtime is started with. For example of how to configure the runtime with different environment settings, see Packaging For Different Environments.
For specific examples of using annotations or the DSL, refer to the sample applications.Â