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:
See the Depend on Zest™ in your build tutorial for details.
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:
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:
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.
org/apache/zest/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