Project

General

Profile

Sample-DB-worker-node » History » Version 15

Henning Blohm, 10.09.2015 15:25

1 3 Udo Offermann
h1. Sample DB Worker Node
2 2 Udo Offermann
3
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.
4
5
The sample is provided via the repository "z2-samples-dbworker":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-dbworker.
6
7
h2. Prerequisites
8
9 15 Henning Blohm
All you need is a Java 6 or Java 7 distribution as described in [[Step_2_-_Install_and_run_in_5_minutes]]. 
10
11
*NOTE:* All versions less than 3 will not run with Java 8. You need to use Java 7 instead!
12 2 Udo Offermann
13
h2. Setting up the sample
14
15 3 Udo Offermann
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:
16 1 Udo Offermann
17 3 Udo Offermann
<pre><code class="ruby">
18
mkdir install
19
cd install 
20
git clone -b master http://git.z2-environment.net/z2-base.core
21
git clone -b master http://git.z2-environment.net/z2-samples.dbworker
22 1 Udo Offermann
23 3 Udo Offermann
# on Linux / Mac OS:
24
cd z2-base.core/run/bin
25
./gui.sh
26 1 Udo Offermann
27 3 Udo Offermann
# on Windows:
28
cd z2-base.core\run\bin
29
gui.bat
30
</code></pre>
31 2 Udo Offermann
32 3 Udo Offermann
The first time you launch the sample will take a while. Watch the log output and keep an eye out for the following lines:
33 2 Udo Offermann
<pre>
34 3 Udo Offermann
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)
35
04/03 15:50:52 [32]...ment/h2Worker@0.1 [800]: SQLState(90028) vendor code(90028)
36
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)
37
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
38
04/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...worker.WorkerSoul [800]: Completed worker process initialization
39 1 Udo Offermann
</pre>
40
41 3 Udo Offermann
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]]).
42
43 2 Udo Offermann
h3. Run the sample app
44
45
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. 
46
47 6 Udo Offermann
h3. How to switch to Derby
48
49
By default the sample uses the H2 RDBMS. In order to use Apache Derby you have to change the following file:
50
51 8 Udo Offermann
# *in @environment/home.properties@:* 
52 6 Udo Offermann
  replace the line
53
  @home.workers = com.zfabrik.db.environment/h2Worker, environment/webWorker@ 
54
  by 
55 1 Udo Offermann
  @home.workers = com.zfabrik.db.environment/derbyWorker, environment/webWorker@
56 8 Udo Offermann
# *in @environment/systemDB.properties@:*
57
  you have to enable the Derby related @ds.prop.*@ settings and to disable the related H2 settings:
58 6 Udo Offermann
<pre>
59
#
60
# Java DB (Derby)
61
#
62
ds.prop.driverClass=org.apache.derby.jdbc.ClientDriver
63
ds.prop.url=jdbc:derby://localhost/${var.dataBaseName};create=true
64
65
#
66
# H2
67
# jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName>
68
# see http://www.h2database.com/html/features.html#database_url
69
#
70
#ds.prop.driverClass=org.h2.Driver
71
#ds.prop.url=jdbc:h2:tcp://localhost/${var.dataBaseName}
72
</pre>
73
74 13 Udo Offermann
After restart the z2-Environment will run with the Apache Derby database. The "sample app":http://localhost:8080/dbworker_sample displays the database product name so you can easily check what database is active. 
75 9 Udo Offermann
76 4 Udo Offermann
h2. How to use the DB Worker Add-on for own projects?
77 2 Udo Offermann
78 10 Udo Offermann
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:
79 12 Udo Offermann
80 11 Udo Offermann
@environment/dbWorkerAddon.properties@:
81 10 Udo Offermann
<pre>
82
...
83
gitcr.uri=http://git.z2-environment.net/z2-addons.dbworker
84
...
85
</pre>
86 1 Udo Offermann
87 10 Udo Offermann
Furthermore the _home layout_ defined in @home.properties@ launches the database worker in addition to the web worker:
88 12 Udo Offermann
89 11 Udo Offermann
@environment/home.properties@:
90 3 Udo Offermann
<pre>
91
com.zfabrik.component.type=com.zfabrik.homeLayout
92
home.workers = com.zfabrik.db.environment/h2Worker, environment/webWorker
93
</pre>
94
95
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.