Examples
TODO: this examples page is deprecated; code is out-of-date, and better examples are described on the web site. need to figure out if this page should be kept at all (indeed if the "guide" is even still relevant)
Integrating with a Maven project
If you have a Maven-based project, integrate this XML fragment with your pom.xml:
<dependencies>
<dependency>
<groupId>io.brooklyn</groupId>
<artifactId>brooklyn-all</artifactId>
<version>0.5.0</version> <!-- BROOKLYN_VERSION -->
</dependency>
</dependencies>
<repository>
<id>cloudsoft-releases</id>
<url>http://developers.cloudsoftcorp.com/download/maven2/</url>
</repository>
<repository>
<id>libs-snapshot-local</id>
<url>http://ccweb.cloudsoftcorp.com/maven/libs-snapshot-local/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
Starting a Tomcat Server
The code below starts a Tomcat server on the local machine.
The main
method defines the application, and passes it to the BrooklynLauncher
to be managed.
It is then started in a localhost location (other locations are shown in the next section).
The Tomcat's configuration indicates that the given WAR should be deployed to the Tomcat server when it is started.
// TODO Untested code; see brooklyn-example for better maintained examples!
public class TomcatServerApp extends AbstractApplication {
@Override
public void init() {
addChild(EntitySpecs.spec(TomcatServer.class)
.configure("httpPort", "8080+")
.configure("war", "/path/to/booking-mvc.war")));
}
}
The wars
config is also supported (with config keys ROOT_WAR
and NAMED_WARS
the long-hand syntax);
they accept EARs and other common archives, and can be described as files or as URLs (as Strings),
with URLs supporting an optional classpath://org/acme/resources/xxx.war
syntax.
Starting Tomcat in Amazon EC2
To start a tomcat node or cluster in Amazon EC2, the application is identical to that for localhost. The only difference is the location supplied.
The Brooklyn CLI can be used to launch the application in your choice of location, such as:
brooklyn launch --app TomcatServerApp --location localhost
brooklyn launch --app TomcatServerApp --location aws-ec2:eu-west-1
Starting a Tomcat Cluster with Nginx
The code below starts a Tomcat cluster along with an Nginx instance, where each Tomcat server in the cluster is registered with the Nginx instance.
// TODO Untested code; see brooklyn-example for better maintained examples!
public class TomcatClusterWithNginxApp extends AbstractApplication {
@Override
public void init() {
addChild(EntitySpecs.spec(NginxController.class)
.configure("domain", "brooklyn.geopaas.org")
.configure("port", "8000+")
.configure("portNumberSensor", Attributes.HTTP_PORT));
addChild(EntitySpecs.spec(ControlledDynamicWebAppCluster.class)
.configure("controller", nginxController)
.configure("memberSpec", : EntitySpecs.spec(TomcatServer.class)
.configure("httpPort", "8080+")
.configure("war", "/path/to/booking-mvc.war"))
.configure("initialSize", 2));
}
}
This creates a cluster that of Tomcat servers, along with an Nginx instance. The NginxController
instance
is notified whenever a member of the cluster joins or leaves; the entity is configured to look at the HTTP_PORT
attribute of that instance so that the Nginx configuration can be updated with the ip:port of the cluster member.
Starting a Multi-location Tomcat Fabric
The ControlledDynamicWebAppCluster
entity used above can also be used with a DynamicFabric to start
a web-cluster in each location.
// TODO Untested code; see brooklyn-example for better maintained examples!
public class TomcatFabricApp extends AbstractApplication {
@Override
public void init() {
addChild(EntitySpecs.spec(DynamicFabric.class)
.configure("displayName", "WebFabric")
.configure("displayNamePrefix", "")
.configure("displayNameSuffix", " web cluster")
.configure("memberSpec", EntitySpecs.spec(ControlledDynamicWebAppCluster.class)
.configure("initialSize", 2)
.configure("memberSpec", : EntitySpecs.spec(TomcatServer.class)
.configure("httpPort", "8080+")
.configure("war", "/path/to/booking-mvc.war"))));
}
}
Examples Source
Source code for (more up-to-date!) examples is available for download from GitHub. To retrieve the source, execute the following command:
git clone git@github.com:brooklyncentral/brooklyn-examples.git
You can also browse the code on the web.