Applications written in YAML can similarly be written in Java. However, the YAML approach is recommended.
The example below creates a three tier web service, composed of an Nginx load-balancer, a cluster of Tomcat app-servers, and a MySQL database. It is similar to the [YAML policies example]({{ site.path.guide }}/start/policies.html), but also includes the MySQL database to demonstrate the use of dependent configuration.
{% highlight java %} {% readj java_app/ExampleWebApp.java %} {% endhighlight %}
To describe each part of this:
AbstractApplication
.init()
, to add its child entities. The init
method is called only once, when instantiating the entity instance.addChild
method takes an EntitySpec
. This describes the entity to be created, defining its type and its configuration.brooklyn.example.db.url
is a system property that will be passed to each TomcatServer
instance. Its value is the database's URL (discussed below).NginxController
is the load-balancer and reverse-proxy: by default, it round-robins to the ip:port of each member of the cluster configured as the SERVER_POOL
.Often a component of an application will depend on another component, where the dependency information is only available at runtime (e.g. it requires the IP of a dynamically provisioned component). For example, the app-servers in the example above require the database URL to be injected.
The “DependentConfiguration” methods returns a future (or a “promise” in the language of some other programming languages): when the value is needed, the caller will block to wait for
the future to resolve. It will block only “at the last moment” when the value is needed (e.g. after the VMs have been provisioned and the software is installed, thus optimising the provisioning time). It will automatically monitor the given entity's sensor, and generate the value when the sensor is populated.
The attributeWhenReady
is used to generate a configuration value that depends on the dynamic sensor value of another entity - in the example above, it will not be available until that MySqlNode.DATASTORE_URL
sensor is populated. At that point, the JDBC URL will be constructed (as defined in the formatString
method, which also returns a future).