Mercurial > dive4elements > framework
changeset 264:fa0d9acea897
flys/issue65: Added Jetty HTTP server as a replacement option to foster better scalability. Needs testing.
artifacts/trunk@1975 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 22 May 2011 12:40:32 +0000 |
parents | c0fb96f88ad1 |
children | d52947ce8629 |
files | ChangeLog artifact-database/pom.xml artifact-database/src/main/java/de/intevation/artifactdatabase/rest/JettyServer.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java pom.xml |
diffstat | 6 files changed, 100 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Sun May 22 11:21:40 2011 +0000 +++ b/ChangeLog Sun May 22 12:40:32 2011 +0000 @@ -1,3 +1,24 @@ +2011-04-22 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + flys/issue65: Added Jetty HTTP server as a replacement option to foster + better scalability. Needs testing. + + * pom.xml: Added repository for Jetty. + + * artifact-database/pom.xml: + Added dependencies to Jetty (Licenses: Apache 2.0 or Eclipse). + + * artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java: + Pass restlet context to super constructor. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/rest/JettyServer.java: + New. Use embedded Jetty server to serve rest API. + To enable it set "/artifact-database/rest-server/http-server/text()" + in global config to "de.intevation.artifactdatabase.rest.JettyServer". + + * artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java: + Refactored a bit to be useful as a base class. + 2011-04-22 Sascha L. Teichmann <sascha.teichmann@intevation.de> * artifact-database/src/main/java/de/intevation/artifactdatabase/rest/HTTPServer.java:
--- a/artifact-database/pom.xml Sun May 22 11:21:40 2011 +0000 +++ b/artifact-database/pom.xml Sun May 22 12:40:32 2011 +0000 @@ -65,6 +65,11 @@ <version>2.0.7</version> </dependency> <dependency> + <groupId>org.restlet.jse</groupId> + <artifactId>org.restlet.ext.jetty</artifactId> + <version>2.0.7</version> + </dependency> + <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.3.152</version> @@ -99,5 +104,10 @@ <artifactId>slf4j-log4j12</artifactId> <version>1.5.11</version> </dependency> + <dependency> + <groupId>org.mortbay.jetty</groupId> + <artifactId>jetty</artifactId> + <version>6.1.26</version> + </dependency> </dependencies> </project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/JettyServer.java Sun May 22 12:40:32 2011 +0000 @@ -0,0 +1,42 @@ +package de.intevation.artifactdatabase.rest; + +import de.intevation.artifacts.ArtifactDatabase; + +import org.restlet.Component; +import org.restlet.Server; + +import org.restlet.ext.jetty.HttpServerHelper; + +import org.apache.log4j.Logger; + +public class JettyServer +extends Standalone +{ + private static Logger logger = Logger.getLogger(JettyServer.class); + + @Override + public void startAsServer(ArtifactDatabase db) { + + Component component = new Component(); + + RestApp app = new RestApp(component.getContext(), db); + + Server server = createServer(); + + component.getServers().add(server); + + logServerStart(); + + HttpServerHelper serverHelper = new HttpServerHelper(server); + + // TODO: Do more sophisticated Jetty server configuration here. + + try { + serverHelper.start(); + } + catch (Exception e) { + logger.error(e.getLocalizedMessage(), e); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java Sun May 22 11:21:40 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/RestApp.java Sun May 22 12:40:32 2011 +0000 @@ -39,6 +39,11 @@ public RestApp() { } + public RestApp(Context context, ArtifactDatabase database) { + super(context); + this.database = database; + } + /** * Constructor to create REST appliction bound to a specific * artifact database.
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java Sun May 22 11:21:40 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java Sun May 22 12:40:32 2011 +0000 @@ -67,10 +67,10 @@ public Standalone() { } + @Override public void setup(Document document) { String portString = XMLUtils.xpathString(document, REST_PORT, null); - port = DEFAULT_PORT; if (portString != null) { @@ -90,6 +90,18 @@ maxThreads = XMLUtils.xpathString(document, MAX_THREADS, null); } + protected Server createServer() { + return listen != null + ? new Server(Protocol.HTTP, listen, port) + : new Server(Protocol.HTTP, port); + } + + protected void logServerStart() { + logger.info("Starting " + getClass().getName() + " HTTP server on " + + (listen != null ? listen : "*") + + ":" + port); + } + /** * Builds a RestApp wrapped around the given artifact database, * and bind this application to HTTP server. The HTTP server @@ -100,20 +112,14 @@ * @param db The artifact database to be exposed via the * REST application. */ + @Override public void startAsServer(ArtifactDatabase db) { RestApp app = new RestApp(db); Component component = new Component(); - Server server = null; - - if (listen != null) { - server = new Server(Protocol.HTTP, listen, port); - } - else { - server = new Server(Protocol.HTTP, port); - } + Server server = createServer(); component.getServers().add(server); @@ -124,9 +130,7 @@ component.getDefaultHost().attach(app); - logger.info("Starting rest HTTP server on " - + (listen != null ? listen : "*") - + ":" + port); + logServerStart(); try { component.start();
--- a/pom.xml Sun May 22 11:21:40 2011 +0000 +++ b/pom.xml Sun May 22 12:40:32 2011 +0000 @@ -69,10 +69,15 @@ <name>Public online Restlet repository</name> <url>http://maven.restlet.org</url> </repository> + <repository> + <id>maven-jetty</id> + <name>Public jetty repository</name> + <url>http://oss.sonatype.org/content/groups/jetty</url> + </repository> </repositories> <modules> <module>artifacts</module> <module>artifact-database</module> <module>artifacts-common</module> </modules> -</project> \ No newline at end of file +</project>