Configuration for Java EE: Config JSR and Tamaya

Preview:

Citation preview

Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

Configuration for Java EE

Dmitry Kornilov@m0mus

Werner Keil@wernerkeil

November 16, 2016

Config JSR and Tamaya

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 2

Safe Harbor StatementThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 3

Dmitry Kornilov

• Software Developer @ Oracle• JSON-B (JSR-367) spec lead• JSON-P (JSR-374) spec lead• Outstanding Spec Lead 2016• EclipseLink project committer

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 4

Werner Keil

• Consultant – Coach• Creative Cosmopolitan• Open Source Evangelist• Software Architect• Spec Lead – JSR363• Individual JCP Executive Committee

Member

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 5

Program Agenda

Introduction

Problem Definition

JSR Proposal & Features

Tamaya

Q & A

1

2

3

4

5

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 6

Introduction

Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

Oracle Java EE 8 Survey 2014

Confidential – Oracle Internal

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 8

DZone and Java EE Guardians Survey Results

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 9

Java EE 7

Connector JAXBJSP Debugging

Managed BeansJSPConcurrency EE Interceptors JAX-WS WebSocket

Bean Validation JASPIC ServletJMS JTADeployment

Batch JACCDependency Injection JAXR JSTL Management

CDI EJB JAX-RPC Web ServicesJSF Java Persistence

JSON-PCommon Annotations EL JAX-RS Web Services MetadataJavaMail

CDI

JSON-P

Security

Bean Validation

JSF

JAX-RS

JSP

Servlet

Health CheckConfiguration

Java EE 8 (Revised Proposal, 2016)

JSON-B

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 10

Problem Definition

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 11

What is Configuration?

Application server setup?Runtime parameters?

Deployment descriptors?Parameters of used frameworks?

Deployment scripts? Used resources?

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 12

Problems• Lack of standard configuration API• Configuring multiple instances• Deploying on different environments• Change configuration without

redeployment• Configuration of decoupled

microservices

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 13

Problems• Lack of standard configuration API• Configuring multiple instances• Deploying on different environments• Change configuration without

redeployment• Configuration of decoupled

microservices

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 14

Problems• Lack of standard configuration API• Configuring multiple instances• Deploying on different environments• Change configuration without

redeployment• Configuration of decoupled

microservices

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 15

Problems• Lack of standard configuration API• Configuring multiple instances• Deploying on different environments• Change configuration without

redeployment• Configuration of decoupled

microservices

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 16

Problems• Lack of standard configuration API• Configuring multiple instances• Deploying on different environments• Change configuration without

redeployment• Configuration of decoupled

microservices

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 17

JSR Proposal

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 18

Configuration Definition• Application centric• Not modifiable by application• Consists of key/value pairs• Keys and values are strings• Flat structure

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 19

Java EE Configuration• Unified API• Externalized configuration• Support of multiple configuration sources– Properties, xml and json formats support out of the box

• Layering and overrides• Optional configuration descriptor• Dynamic configuration• Integration with other Java EE frameworks

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 20

Java EE Configuration• Unified API• Externalized configuration• Support of multiple configuration sources– Properties, xml and json formats support out of the box

• Layering and overrides

Java EE 8

• Optional configuration descriptor• Dynamic configuration• Integration with other Java EE frameworks

Java EE 9

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 21

JSR Features (Java EE 8)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 22

API

a=JavaOneb=9c=2016

Config config = ConfigProvider.getConfig();

// Returns "JavaOne"String a = config.getProperty("a");

// Returns string "9"String b = config.getProperty("b");

// Returns string "default"String defaultValue = config.getProperty("not.exist", "default");

// Returns number 2016Long c = config.getProperty("c", Long.class);

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 23

Converters• Type safe access to values• Built-in converters for:– Primitive Wrappers– BigDecimal, BigInteger, URL, URI– Date, Calendar– java.time.*

public interface Converter<Target> { Target convert(String value, Config c);}

public class FooConverter implements Converter<Foo> {

public Foo convert(String value, Config c) { ... }}

Config cfg = ConfigProvider.builder() .withConverters(new FooConverter()) .build();

Foo foo = cfg.getProperty("foo", Foo.class);

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 24

Configuration Sources• Multiple configuration sources • Supported configuration sources:– System properties– Runtime parameters– File (Properties, xml, json)– Resource on a web server

• Pluggable architecture– Custom sources (like DB)

• Configuration sources are ordered

Java EE Config

XML JSONprop

DBweb

Application

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 25

Default Configuration Sources

Java EE Config

• System properties (ordinal=400)• Environment properties (ordinal=300)• /META-INF/config.properties (ordinal=100)• /META-INF/config.xml (ordinal=100)• /META-INF/config.json (ordinal=100)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 26

Defining Configuration Sources• Using config.sources runtime parameter

• Using API

• Using config-sources.xml file

java –jar my.jar –Dconfig.source=http://shared/global.xml,/conf/my.json

Config config = ConfigProvider.builder() .addSource(new WebSource("http://shared:8080/global.xml"), 200) .addSource(new FileSource("/conf/my.json"), 100) .addSource(new MyCustomSource()) .build();

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 27

config-sources.xml• File with defined schema using to define configuration sources and their

metadata• Default location /META-INF/config-sources.xml• Can be placed outside of the application package• Define using runtime parameter

• Define using API

java –jar my.jar –Dconfig.sources=http://sharedhost/config-sources.xml

Config c = ConfigProvider.builder() .withSources("/cfg/config-sources.xml") .build();

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 28

Sample config-sources.xml Simple

With ordinals

<config-sources> <source>http://shared:8080/config.xml</source> <source>/cfg/myconf.json</source></config-sources>

<config-sources> <source ordinal="500">http://shared:8080/config.xml</source> <source ordinal="450">/cfg/myconf.json</source></config-sources>

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 29

Sample config-sources.xml Custom<config-sources> <source>http://shared:8080/config.xml</source> <source type="com.oracle.config.CloudConfig"> <user>user</user> <password>secret</password> </source></config-sources>

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 30

JSR Features (Java EE 9)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 31

Integration With Other Java EE Frameworks• Read configuration from

Java EE Config• Store configuration as part of

whole application configuration• Use standard API Java EE

Config

ApplicationConfiguration

Application

JPAConfiguration

JAX-RSConfiguration

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 32

Configuration Descriptor• File with defined format• Defines all configurable properties

and metadata• Optional• It’s not XML-Schema!

<config-descriptor> <property name="a"/> <property name="b" default=”valueB"/> <property name="c" mutable="false"/> <property name="d"/></config-descriptor>

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 33

Dynamic Configuration• Polling framework• Expressions• Property Resolvers• Configuration Context

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 34

Polling

// Defining polling using APIConfig cfg = ConfigProvider.builder() .withSource(new FileSource("/cfg/config.xml"), 200, Duration.ofSeconds(30)) .withSource(new WebSource("http://shared/config.xml"), 100, Duration.ofMinutes(1)) .build();

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 35

Polling

// Defining polling using APIConfig cfg = ConfigProvider.builder() .withSource(new FileSource("/cfg/config.xml"), 200, Duration.ofSeconds(30)) .withSource(new WebSource("http://shared/config.xml"), 100, Duration.ofMinutes(1)) .build();

Ordinal Refresh IntervalLocation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 36

Polling

<config-sources refresh-rate="300000">

<source ordinal="200" refresh-rate="30000">/cfg/config.xml</source>

<source ordinal="100" refresh-rate="60000">http://shared/config.xml</source>

</config-sources>

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 37

Polling

<config-sources refresh-rate="300000">

<source ordinal="200" refresh-rate="30000">/cfg/config.xml</source>

<source ordinal="100" refresh-rate="60000">http://shared/config.xml</source>

</config-sources>

Sources list refresh interval

(5 min)

Source 1 refresh interval (30 sec) Source 2 refresh

interval (1 min)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 38

Expressions• EL like expressions evaluated at runtime to a property value• Property substitution

• Conditional configuration sources

foo=${some.other.value}bar=${foo + 10}baz=${foo * bar}

<config-sources> <source>//cfg/config.properties</source> <source enabled=”${app==‘ios’}”> //cfg/cust_ios.properties </source> </config-sources>

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 39

Property Resolvers• Flexible mechanism allowing

executing user code in configuration expressions• Can be used to inject cloud

resources

rating.service.url=${eureka:rating.url}cust.db=${cloud:cust.db}

<config-sources> <resolvers> <resolver name=”cloud”> <class>com.example.CloudResolver</class> <username>user</username> <password>secret</password> </resolver> </resolvers> <!-- ... --> </config-sources>

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 40

Configuration Context• Allows setting application variables which can be used in configuration

expressions• Example: geographical zone, application type, etc.

<config-sources> <source>/cfg/config.properties</source> <source enabled=”${app==‘ios’}”>cust_ios.properties</source> </config-sources>

Config config = ConfigProvider.getConfig();ConfigContext context = ConfigContext.builder().addProperty("app", "ios").build(); Long prop = config.getPropertyWithContext("prop", context);

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 41

Tamaya

Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

• Configuration = ordered list of PropertySources• Properties found are combined using a CombinationPolicy• Raw properties are filtered by PropertyFilter• For typed access PropertyConverters

have to do work• Extensions add more features • Component Lifecycle is controlled by the ServiceContextManager

ConfigurationContext

PropertyFilters

PropertySource

PropertySource

PropertySource

PropertySource

Configuration

Com

bina

tionP

olic

y

PropertyProviders<provides>

PropertyConverter

42

Apache Tamaya in 120 seconds...

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 43

Requirements• Developer‘s Perspective• Architectural/Design Requirements• Operational Aspects• Other Aspects

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 44

Developer‘s Requirements• Easy to use.• Developers want defaults.• Developers don‘t care about the runtime (for configuration only).• Developers are the ultimate source of truth• Type Safety

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 45

Architectural/Design Requirements• Decouple code that consumes configuration from– Backends Used– Storage Format– Distribution– Lifecycle and versioning– Security Aspects

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 46

Operational's Requirements• Enable Transparency: –What configuration is available ?–What are the current values and which sources provided the value ?– Documentation

• Manageable:– Configuration changes without redeployment or restart.– Solution must integrate with existing environment

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 47

Other Aspects• Support Access Constraints and Views• No accidental logging of secrets• Dynamic changes• Configuration Validation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 48

API Requirements• Leverage existing functionality where useful• Only one uniform API for access on all platforms!• Defaults provided by developer during development

(no interaction with operations or external dependencies)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 49

Existing Mechanisms• Environment Properties• System Properties• CLI arguments• Properties, xml-Properties

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 50

Dependencies - API & Core<dependency> <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-api</artifactId> <version>0.3-incubating-SNAPSHOT</version></dependency>

<dependency> <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-core</artifactId> <version>0.3-incubating-SNAPSHOT</version></dependency>

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 51

Programmatic APIConfiguration config = ConfigurationProvider.getConfiguration();

// single property accessString name = config.getOrDefault("name", "John");int ChildNum = config.get("childNum", int.class);

// Multi property accessMap<String,String> properties = config.getProperties();

// Templates (provided by extension)MyConfig config = ConfigurationInjection.getConfigurationInjector() .getConfig(MyConfig.class);

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 52

Dependencies – Injection SE<dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-injection-api</artifactId> <version>0.3-incubating-SNAPSHOT</version></dependency>

<dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-injection</artifactId> <version>0.3-incubating-SNAPSHOT</version></dependency>

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 53

Dependencies – Injection SE@Config(value="admin.server", defaultValue="127.0.0.1")private String server;

@Config(value="admin.port", defaultValue="8080")private int port;

@Config(value="admin.connections")private int connections = 5;

@Config("address")private Address address; MyTenant t = new MyTenant();

ConfigurationInjection .getConfigurationInjector() .configure(t);

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 54

Configuration Backends• Support existing mechanisms OOTB• Provide a simple SPI for (multiple) property sources• Define a mechanism to prioritize different property sources• Allow different strategies to combine values• Support Filtering• Support Type Conversion

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 55

PropertySourcepublic interface PropertySource { PropertyValue get(String key); Map<String, String> getProperties(); boolean isScannable(); String getName(); int getOrdinal();}

public final class PropertyValue{ public String getKey(); public String getValue(); public String get(String key); public Map<String, String> getConfigEntries(); ...}

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 56

Predefined Property Sources• System & Environment Properties• (CLI Arguments)• Files– ${configDir}/*.properties

• Classpath Resources– /META-INF/javaconfiguration.properties

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 57

Property Sources• Mostly map to exact one file, resource or backend• Have a unique name• Must be thread safe• Can be dynamic• Provide an ordinal• Can be scannable

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 58

PropertySource - Examplepublic class MyPropertySource extends BasePropertySource { private Map<String, String> props = new HashMap<>(); public SimplePropertySource() throws IOException { URL url = getClass().getClassLoader().getResource(

"/META-INF/myFancyConfig.xml"); // read config properties into props ... }

@Override public String getName() { return "/META-INF/myFancyConfig.xml"; };

@Override public Map<String, String> getProperties() { return props; }}

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 59

PropertySource - Registration• By default, register it using the java.util.ServiceLoader –→ /META-INF/services/org.apache.tamaya.spi.PropertySource–MyPropertySource

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 60

PropertySource Provider• Allow dynamic registration of multiple property sources• E.g. all files found in a config directory• Are evaluated once and then discarded• Are also registered using the ServiceLoader.

public interface PropertySourceProvider{ public Collection<PropertySource> getPropertySources();}

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 61

More SPI artifacts• Filters for filtering values evaluated (remove, map, change)• Converters for converting String values to non-String types• A ValueCombinationPolicy– determines how values evaluated are combined to a final value

(defaults to overriding)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 62

Q & A

Recommended