Sample-DB-worker-node » History » Revision 12
Revision 11 (Udo Offermann, 03.04.2013 16:19) → Revision 12/19 (Udo Offermann, 03.04.2013 16:19)
h1. Sample DB Worker Node This sample illustrates the usage of the [[DB_Worker_Node_Add-on]] and thereby how easy it is to setup a all-in-one-system comprising the z2 environment and the database. The sample web-application has a very simple database schema containing one table with a primary key and a varchar column while the UI provides just the basic CRUD actions. The sample is provided via the repository "z2-samples-dbworker":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-dbworker. h2. Prerequisites All you need is a JDK 6 distribution as described in [[Step_2_-_Install_and_run_in_5_minutes]]. h2. Setting up the sample There is no further pre-requisite to running this sample, and you may proceed as described in [[How to run a sample]]. Here's the really fast version: <pre><code class="ruby"> mkdir install cd install git clone -b master http://git.z2-environment.net/z2-base.core git clone -b master http://git.z2-environment.net/z2-samples.dbworker # on Linux / Mac OS: cd z2-base.core/run/bin ./gui.sh # on Windows: cd z2-base.core\run\bin gui.bat </code></pre> The first time you launch the sample will take a while. Watch the log output and keep an eye out for the following lines: <pre> 4/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...2DatabaseLauncher [800]: H2 TCP Server running at tcp://192.168.178.28:9092 (only local connections allowed) 04/03 15:50:52 [32]...ment/h2Worker@0.1 [800]: SQLState(90028) vendor code(90028) 04/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...2DatabaseLauncher [800]: H2 Console Server running at http://192.168.178.28:8082 (only local connections allowed) 04/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...stemStateResource [800]: Attained system state: com.zfabrik.db.environment/h2WorkerUp 04/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...worker.WorkerSoul [800]: Completed worker process initialization </pre> If you want to inspect the code using Eclipse, please create a workspace in install (i.e. @install/workspace@) and import the Git repositories and the following projects into your workspace: _core_ from z2-base.core, _environemnt_ and _com.zfabrik.samples.dbworker.webapp_ from z2-samples.dbworker (see also [[Step_3_-_First_steps_with_Z2_on_Git|First steps]]). h3. Run the sample app run http://localhost:8080/dbworker_sample and play around with the UI. You can create new entries, modify and update existing entries by clicking inside the name column and delete entries. The sample application itself is not that tricky. It uses the JDBC API in order to create the schema on demand as well as to implement the CRUD operations. A single jsp file provides the UI. h3. How to switch to Derby By default the sample uses the H2 RDBMS. In order to use Apache Derby you have to change the following file: # *in @environment/home.properties@:* replace the line @home.workers = com.zfabrik.db.environment/h2Worker, environment/webWorker@ by @home.workers = com.zfabrik.db.environment/derbyWorker, environment/webWorker@ # *in @environment/systemDB.properties@:* you have to enable the Derby related @ds.prop.*@ settings and to disable the related H2 settings: <pre> # # Java DB (Derby) # ds.prop.driverClass=org.apache.derby.jdbc.ClientDriver ds.prop.url=jdbc:derby://localhost/${var.dataBaseName};create=true # # H2 # jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName> # see http://www.h2database.com/html/features.html#database_url # #ds.prop.driverClass=org.h2.Driver #ds.prop.url=jdbc:h2:tcp://localhost/${var.dataBaseName} </pre> After restart the z2-Environment will run with the Apache Derby database. h2. How to use the DB Worker Add-on for own projects? The *environment* project of the sample contains the important pieces: The z2 repository component @dbWorkerAddon.properties@ makes the DB Worker Node Add-on available to the z2-Environment: @environment/dbWorkerAddon.properties@: <pre> ... gitcr.uri=http://git.z2-environment.net/z2-addons.dbworker ... </pre> Furthermore the _home layout_ defined in @home.properties@ launches the database worker in addition to the web worker: @environment/home.properties@: <pre> com.zfabrik.component.type=com.zfabrik.homeLayout home.workers = com.zfabrik.db.environment/h2Worker, environment/webWorker </pre> Note that @com.zfabrik.db.environment/h2Worker@ precedes @environment/webWorker@ so that the DB is up and running when before the web applications are started.