Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FenixEdu Training: Part Two - Fenix Framework &...

FenixEdu Training: Part Two - Fenix Framework & Bennu

Avatar for Artur Ventura

Artur Ventura

May 13, 2014

More Decks by Artur Ventura

Other Decks in Technology

Transcript

  1. Schedule 12 15 Introduction Concepts Architecture Bennu Fenix Framework Struts

    & Renderers JSP & Renderers Faces & Spring New technologies Git & Maven Collaboration Coffee Break Coffee Break You are here
  2. Fenix Framework • Java Framework for applications that require transactional,

    persistent, rich domain models • Transparent persistency, strong consistency guarantees across multiple concurrent actors • Designed for modularity • No commitment with a particular backend • Bible Chapter 6
  3. • Fenix Framework Core • Fenix Framework modules • We

    will use the Consistency Predicates module • Persistence backend • We will focus on JVSTM / MySQL
  4. Fenix Framework Core • Domain Modelling Language (DML) • Generated

    API • Domain Objects / Relations • Transactions API
  5. DML • Declarative Language used to reify the Domain Model

    • Classes, Slots, Relations, Roles, ValueTypes, Enums • Code is generated for each element • Modular, allows dependencies between DML files
  6. Classes • Syntax similar to a regular Java class •

    Can only contain fields • Allows slot options • Allows specifying visibility modifiers • Will generate User.java • Slot types?! public class User { protected String username (REQUIRED); protected String password; protected String salt; protected DateTime created; public LocalDate expiration; public String email; public Locale preferredLocale; }
  7. ValueTypes • Builtin: Primitives, Primitive Wrappers, String, Joda Time, byte[],

    Serializable, JSON, Enums • User-defined: Any type • Multiple aliases for the same type • Must externalise to one or more value types • MUST be immutable valueType java.util.Locale as Locale { externalizeWith { String toLanguageTag(); } internalizeWith forLanguageTag(); } ! OR ! valueType java.util.Locale as JsonLocale { externalizeWith { String getLanguage(); String getCountry(); } }
  8. Relations • Named first-class citizen • Relates two domain classes

    • Each role must have a name (unique for that type) • Multiplicity ranges • E.g.: 0..1, 1..*, 5..9 • Upper multiplicity determines relation type (to-one, to-many) relation SystemUsers { protected Bennu playsRole bennu { multiplicity 1..1; } protected User playsRole user { multiplicity *; } }
  9. Relation Listeners • Hooks that are notified whenever the relation

    is modified • Useful for ensuring constraints are maintained • Allows notifications before add, after add, before remove and after remove. static { Registration. getRelationShiftStudent(). addListener( new ShiftStudentListener()); }
  10. Generated API DML Declaration Generated Code public class User public

    class User extends User_Base protected String username protected String getUsername() protected void setUserName(String) protected Bennu playsRole bennu { multiplicity 1..1; } protected Bennu getBennu() protected void setBennu(Bennu) protected User playsRole user { multiplicity *; } protected Set<User> getUserSet() protected void addUser(User) protected void removeUser(User)
  11. DML - Remarks • NEVER touch the _Base classes •

    They are re-generated for each module, and depend on the underlying backend • Undefined behaviour if invoked outside a transactional context (Hint: NPE) • Each DomainObject has a unique External ID • Object equality determined by identity (i.e. equals 㱻 ==) • Serializable, stores the object’s ID
  12. Fenix Framework Core API • Transactions API • Runtime configuration

    and initialization • Domain Object retrieval API • Defines backend API • Your code should not depend on this • FenixFramework class
  13. Transactions API • All operations on Domain Objects must occur

    within a transaction • Three different types of transactions: • Read-Only - Any write will throw a WriteOnReadError • Speculative Read - Write causes restart in full write • Write • Allows listeners and introspectors • Provide transaction-local storage
  14. Transactions API • Three different ways to declare transactions: •

    Manually - Begin, Commit, Rollback • FenixFramework.getTransactionManager().withTransaction(Callable<T>) • @Atomic methods • withTransaction or @Atomic preferred, as they handle conflicts by restarting the transaction, and take care of proper bookkeeping
  15. Transactions API @Atomic(mode = TxMode.READ) public String doIt() { return

    “Hello”; } public String doIt() { return FenixFramework. getTransactionManager(). withTransaction(() -> return “Hello”); }
  16. Runtime Configuration • How to configure the Fenix Framework? •

    How does the Framework know what domain to consider when running the application?
  17. Runtime Configuration • When using the provided maven plugin, it

    is as simple as placing a file fenix-framework.properties on your final application • When it is first needed, the Framework will read the configuration file and start up automatically, taking all of the project’s dependencies as part of the domain
  18. Domain Object API • You created your domain model, with

    all its classes and relations. How can you get a reference to it? • The framework defines a special DomainRoot class. Invocations of FenixFramework.getDomainRoot() are guaranteed to always return the same instance DomainRoot ApplicationRoot A B
  19. Domain Object API • How to identify domain objects outside

    your application? • DomainObject.getExternalId() Returns a (unique) String representation of the object • FenixFramework.getDomainObject(String) Recovers the object by its ID. Results undefined if ID not returned by a call to getExternalId() • FenixFramework.isDomainObjectValid(DomainObject) determines whether the given object is still valid (i.e. is a proper reference, has not been deleted, etc)
  20. Consistency Predicates • Consistency predicates ensure that no write transaction

    can leave the domain in an inconsistent state • Programmer is responsible for specifying constraints on the domain • Two ways to specify Consistency Predicates
  21. Consistency Predicates public class Person extends Person_Base { ! (…)

    ! @ConsistencyPredicate public final boolean namesCorrectlyPartitioned() { final String fullName = getName(); final String familyName = getFamilyNames(); final String composedName = getGivenNames() + " " + familyName; ! return fullName.equals(composedName); } ! } public class User { protected String username (REQUIRED); protected String password; (…) } public class User_Base extends AbstractDomainObject { ! @ConsistencyPredicate public final boolean checkMultiplicityOfUsername() { return getUsername() != null; } ! }
  22. Consistency Predicates • All @ConsistencyPredicates are run before the commit

    of a write transaction manipulating the object • Any failed Consistency Predicate will cause the commit to fail, throwing a ConsistencyException.
  23. Backends • Backends provide the concrete implementation of the Transactional

    and Persistence Support • You should NEVER write code that depends on backends, as it is bound to change drastically, even between minor versions • Two kinds of backends: • In-memory: Useful for testing • Persistence backed: For production usage
  24. JVSTM • Java Versioned Software Transactional Memory • Built in-house

    • Based on the concept of Versioned Boxes, containing the history of an object’s value • Requires that every stored value is immutable • Changes can only occur within write transactions
  25. MySQL/MariaDB • Used to synchronize the STM across multiple instances

    of an application • Each Domain Object is stored as a row in the database • Tables organised per type hierarchy • Special FF$ tables are used for bookkeeping and metadata (Specially FF$DOMAIN_CLASS_INFO, which contains class information) • MySQL/MariaDB supported types are used natively when possible (i.e., time, blob)
  26. Bennu App Bootstrap Deploy configuration Menu Configuration User authentication REST

    server infrastructure Transactional IO Scheduling system Theming Internationalization Signalling Access Groups Indexing & searching
  27. Bennu • Provides web application facilites for Fenix Framework •

    Open Source • https://github.com/FenixEdu/bennu
  28. Bennu Scheduling • Add tasks to run on given intervals

    • Execute tasks on a system while running • Bible page 104