This project has retired. For details please refer to its Attic page.
Services Composites Tutorial
Zest™
Introduction
Tutorials
Javadoc
Samples
Core
Libraries
Extensions
Tools
Glossary 

Services Composites Tutorial

Tip

Theses tutorials are based on actual code found in the tutorials/ directory of the Zest™ SDK sources. You should start your favorite editor and find the code related to this tutorial, run it and play with it.

In this other set of tutorials it will be shown how to create and work with Service Composites, which are composites that extends from the ServiceComposite class. We will refactor one a very simple Library where you can borrow and return books to take advantage of the various features in Zest™. These refactorings will benefit from automatic Service activation and Configuration Entities management.

Each tutorial step in this series starts with the result from the previous tutorial, so you can always look at the next tutorial step for guidance on what to do.

At the bottom of each tutorial step, the is Solutions section, which list the files you should have come to if you have followed the instructions.

If you want to reproduce what’s explained in this tutorial, remember to depend on the Core Runtime artifact that depends on Core API, Core SPI, Core Bootstrap and Core Functional & I/O APIs:

Table 6. Artifact

Group IDArtifact IDVersion

org.qi4j.core

org.qi4j.core.runtime

2.1


See the Depend on Zest™ in your build tutorial for details.

Step 1 - Creating a ServiceComposite

In this tutorial we start with basic Java classes, to simulate a very simple Library where you can borrow and return books.

Zest™ relies heavily on the use of interfaces. This makes it possible for an object to externally implement a number of interfaces which internally is backed by a number of Mixins, some of which you may have written yourself, and some of which may have been reused. This is also true for services, which we are to cover in this tutorial.

The first task is therefore to refactor the code so that the methods are implemented from an interface instead, and to essentially make the identical "application" into a Zest™ application. We also want the Book to be a ValueComposite.

Steps for this tutorial:

  • Make the Book into a ValueComposite.
  • Make the Library an interface with the same methods. We call this a MixinType.
  • Create a LibraryMixin class, which implements the Library interface.
  • Create a LibraryService that binds the LibraryMixin.
  • The LibraryMixin will need to be injected with the ValueBuilderFactory in the @Structure scope.

Step 2 - Hooking into the Service Activation

Services can be "activated" and "passivated". Applications can be notified of this occurring by Zest™ runtime by assembling them with an Activator.

Activators methods are called around "activation" and "passivation": beforeActivation, afterActivation, beforeActivation, afterPassivation. The ActivatorAdapter class help you keeping your code short when you only need one or two hooks.

To showcase how this works, we refactor the code to create a number of copies of the books, to be lend out upon call to the borrowBook method, which will return null if no copy is available. The book copies are created in the activate method.

Steps to do:

  • Add a createInitialData method to Library.
  • In the implementation, create a couple of books of each title and store each copy in a HashMap<String,ArrayList<Book>>.
  • Write an Activator<ServiceReference<Library>> class extending ActivatorAdapter.
  • Override the afterActivation method, use the ServiceReference to get a handle on the Library and call its createInitialData method.
  • Add the @Activators annotation to the LibraryService declaring the new LibraryActivator.

Step 3 - Reading the Service Configuration

Services typically have configuration. Configurations are directly supported in Zest™. A ConfigurationComposite is a subtype of EntityComposite. That is because configurations are stored in EntityStores, can be modified in runtime by client code and has the same semantics as regular entities.

Zest™ also handles the bootstrapping of configuration for the services. If the ConfigurationComposite is not found in the configured entity store, then Zest™ will automatically locate a properties file for each service instance, read those properties into a ConfigurationComposite instance, save that to the entity store and provide the values to the service. The properties file must be with the same name as the service instance with the extension "properties" in the same package as the service.

For this exercise, create a LibraryConfiguration that contains "titles", "authors" and "copies". The first two are a string with a comma separated list, and the "copies" is just an Integer with how many copies are made of each title.

Steps to do.

  • Create a LibraryConfiguration interface that extends ConfigurationComposite, and has three Property instances named "titles", "authors" and "copies", where the first two are of String type and the last is of Integer type.
  • Delete the LibraryActivator and remove the @Activators annotation from the LibraryService and the corresponding createInitialData method.
  • In the LibraryMixin remove the member injection of the ValueBuilderFactory, and instead inject the ValueBuilderFactory in the constructor.
  • Inject the LibraryConfiguration via the constructor. The injection scope is @This.
  • Create a resource called LibraryService.properties and place it in the directory org/qi4j/tutorials/services/step4 in the classpath (for instance, src/main/resources ). Put something like this in: titles=Domain Driven Design, Pragmatic Programmer, Extreme Programming Explained authors=Eric Evans, Andy Hunt, Kent Beck #Number of copies of each book. copies=3
  • Load initial data from the LibraryConfiguration in the LibraryMixin constructor.