Skip to content

Commit 7ee831b

Browse files
Migrate jpa-idempotent-repository example from deprecated derby container image to MariaDB
1 parent a89c384 commit 7ee831b

File tree

9 files changed

+203
-236
lines changed

9 files changed

+203
-236
lines changed

jpa-idempotent-repository/README.adoc

+16-56
Original file line numberDiff line numberDiff line change
@@ -64,46 +64,23 @@ The camel application should produce logs as below:
6464
2023-09-15 15:48:20,804 INFO [org.acm.jpa.ide.rep.CostlyApiService] (vert.x-worker-thread-1) Costly API has been called with new content => GOOD
6565
----
6666
67-
When running in dev mode, the idempotent consumer is storing the list of already processed messages in-memory, into a h2 database.
68-
Later on, another database will be used when we'll package and run the application.
69-
Indeed, the duplicate messages will then be stored in files, into a derby database.
67+
The idempotent consumer is storing the list of already processed messages into a MariaDB database.
7068
71-
== Starting and initializing the derby database in a container
69+
If you're wondering how the database schema was created, it happens automatically thanks to `quarkus-flyway`. On application startup, it
70+
creates the `my-db` database and the required `CAMEL_MESSAGEPROCESSED` table. You can find the Flyway migration script at `src/main/resources/db/migration/V1.0.0__add_camel_message_processed.sql`.
71+
You can find more information about Flyway in the https://quarkus.io/guides/flyway[Quarkus Flyway guide].
7272
73-
Before packaging and running the application in JVM mode, we need to start and initialize a derby database in a container.
74-
So, in a first shell, please launch a derby database container:
73+
== Starting and initializing the MariaDB database in a container
7574
76-
[source,shell]
77-
----
78-
docker run -p 1527:1527 az82/docker-derby:10.16
79-
----
80-
81-
And from a second shell, please run the commands below in order to initialize the derby database:
75+
Before packaging and running the application in JVM mode, we need to start and initialize a MariaDB database in a container.
76+
So, in a first shell, please launch a MariaDB database container:
8277
8378
[source,shell]
8479
----
85-
DERBY_DOCKER_ID=$(docker ps -q --filter ancestor=az82/docker-derby)
86-
docker cp src/test/resources/init.sql ${DERBY_DOCKER_ID}:/init.sql
87-
docker exec -it ${DERBY_DOCKER_ID} java -Djdbc.drivers=org.apache.derbbc.EmbeddedDriver org.apache.derby.tools.ij /init.sql
88-
----
89-
90-
It should output some logs like below:
91-
92-
[source,shell]
80+
docker run -e MARIADB_USER=mariadb -e MARIADB_PASSWORD=mariadb -e MARIADB_DATABASE=my-db -e MARIADB_ROOT_PASSWORD=secret -p 3306:3306 docker.io/mariadb:10.11
9381
----
94-
$ DERBY_DOCKER_ID=$(docker ps -q --filter ancestor=az82/docker-derby)
9582
96-
$ docker cp src/test/resources/init.sql ${DERBY_DOCKER_ID}:/init.sql
97-
Successfully copied 2.05kB to c88edda502f7:/init.sql
98-
99-
$ docker exec -it ${DERBY_DOCKER_ID} java -Djdbc.drivers=org.apache.derbbc.EmbeddedDriver org.apache.derby.tools.ij /init.sql
100-
ij version 10.16
101-
ij> CONNECT 'jdbc:derby:my-db;create=true';
102-
ij> CREATE TABLE CAMEL_MESSAGEPROCESSED ( processorName VARCHAR(255), messageId VARCHAR(100), createdAt TIMESTAMP, PRIMARY KEY (processorName, messageId) );
103-
0 rows inserted/updated/deleted
104-
ij> CREATE SEQUENCE CAMEL_MESSAGEPROCESSED_SEQ AS INT MAXVALUE 999999 CYCLE;
105-
0 rows inserted/updated/deleted
106-
----
83+
If successful, you should see the message `mariadbd: ready for connections` output to the console.
10784
10885
=== Package and run the application
10986
@@ -120,26 +97,7 @@ mvn clean package -DskipTests
12097
java -jar target/quarkus-app/quarkus-run.jar
12198
----
12299
123-
Please, note that the shell running the derby database should react by printing some logs as below:
124-
125-
[source,shell]
126-
----
127-
Booting Derby version The Apache Software Foundation - Apache Derby - 10.16.1.1 - (1901046): instance a816c00e-018a-996e-54bf-00003e718008
128-
on database directory /dbs/my-db with class loader jdk.internal.loader.ClassLoaders$AppClassLoader@5c626da3
129-
Loaded from file:/derby/lib/derby.jar
130-
java.vendor=Eclipse Adoptium
131-
java.runtime.version=17.0.4.1+1
132-
user.dir=/dbs
133-
os.name=Linux
134-
os.arch=amd64
135-
os.version=4.18.0-477.21.1.el8_8.x86_64
136-
derby.system.home=null
137-
derby.stream.error.field=java.lang.System.out
138-
Database Class Loader started - derby.database.classpath=''
139-
----
140-
141-
Beyond that, notice how the application behaves the same way.
142-
The only variation compared to the dev mode is actually that the idempotent repository is now a derby database running in a container.
100+
As mentioned above, `quarkus-flyway` will automatically create the required database and tables for you.
143101
144102
==== Native mode
145103
@@ -184,12 +142,14 @@ Check pods are running by executing:
184142
kubectl get pods
185143
----
186144
187-
We expect a list of two pods similar to below:
145+
We expect a list of three pods similar to below.
146+
Note that the `camel-quarkus-examples-jpa-idempotent-repository-flyway` pod will transition from `running` to `completed`, after it has completed initializing the MariaDB database.
188147
189148
[source,shell]
190149
----
191150
NAME READY STATUS RESTARTS AGE
192-
camel-quarkus-examples-derby-database-deployment-76f6dc9bdnwwxn 1/1 Running 0 23s
151+
camel-quarkus-examples-mariadb-database-deployment-76f6dc9bdnwwxn 1/1 Running 0 23s
152+
camel-quarkus-examples-jpa-idempotent-repository-flyway-in2q5n5 0/1 Completed 0 23s
193153
camel-quarkus-examples-jpa-idempotent-repository-7c74b9cf5ph68r 1/1 Running 1 (18s ago) 23s
194154
----
195155
@@ -205,8 +165,8 @@ To clean up do:
205165
[source,shell]
206166
----
207167
kubectl delete all -l app.kubernetes.io/name=camel-quarkus-examples-jpa-idempotent-repository
208-
kubectl delete all -l app.kubernetes.io/name=camel-quarkus-examples-derby-database
209-
kubectl delete configmap -l app.kubernetes.io/name=camel-quarkus-examples-derby-database
168+
kubectl delete all -l job-name=camel-quarkus-examples-jpa-idempotent-repository-flyway-init
169+
kubectl delete all -l app.kubernetes.io/name=camel-quarkus-examples-mariadb-database
210170
----
211171
212172
[NOTE]

jpa-idempotent-repository/pom.xml

+10-5
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,21 @@
100100
<groupId>org.apache.camel.quarkus</groupId>
101101
<artifactId>camel-quarkus-timer</artifactId>
102102
</dependency>
103-
<!-- Note we added a dependency to quarkus-h2 to have an in memory
104-
database during dev mode -->
103+
104+
<!-- Use MariaDB for the database -->
105105
<dependency>
106106
<groupId>io.quarkus</groupId>
107-
<artifactId>quarkus-jdbc-h2</artifactId>
107+
<artifactId>quarkus-jdbc-mariadb</artifactId>
108108
</dependency>
109-
<!-- While in test and prod we use a derby database -->
109+
110+
<!-- Flyway is used to set up the MariaDB database -->
110111
<dependency>
111112
<groupId>io.quarkus</groupId>
112-
<artifactId>quarkus-jdbc-derby</artifactId>
113+
<artifactId>quarkus-flyway</artifactId>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.flywaydb</groupId>
117+
<artifactId>flyway-mysql</artifactId>
113118
</dependency>
114119

115120
<!-- Test -->

jpa-idempotent-repository/src/main/kubernetes/kubernetes.yml

+40-39
Original file line numberDiff line numberDiff line change
@@ -18,77 +18,78 @@
1818
apiVersion: apps/v1
1919
kind: Deployment
2020
metadata:
21-
name: camel-quarkus-examples-derby-database-deployment
21+
name: camel-quarkus-examples-mariadb-database-deployment
2222
labels:
23-
app.kubernetes.io/name: camel-quarkus-examples-derby-database
23+
app.kubernetes.io/name: camel-quarkus-examples-mariadb-database
2424
app.kubernetes.io/version: 3.7.0-SNAPSHOT
2525
spec:
2626
replicas: 1
2727
selector:
2828
matchLabels:
29-
app.kubernetes.io/name: camel-quarkus-examples-derby-database
29+
app.kubernetes.io/name: camel-quarkus-examples-mariadb-database
3030
app.kubernetes.io/version: 3.7.0-SNAPSHOT
3131
template:
3232
metadata:
3333
labels:
34-
app.kubernetes.io/name: camel-quarkus-examples-derby-database
34+
app.kubernetes.io/name: camel-quarkus-examples-mariadb-database
3535
app.kubernetes.io/version: 3.7.0-SNAPSHOT
3636
spec:
3737
containers:
38-
- name: derby-database
39-
# Use a default configured derby database for example purpose, think twice before deploying to production
40-
image: az82/docker-derby:10.16
38+
- name: mariadb-database
39+
image: docker.io/mariadb:10.11
4140
ports:
42-
- containerPort: 1527
41+
- containerPort: 3306
42+
env:
43+
- name: MARIADB_USER
44+
valueFrom:
45+
secretKeyRef:
46+
name: mariadb-secret
47+
key: db-user
48+
- name: MARIADB_PASSWORD
49+
valueFrom:
50+
secretKeyRef:
51+
name: mariadb-secret
52+
key: db-password
53+
- name: MARIADB_DATABASE
54+
value: my-db
55+
- name: MARIADB_RANDOM_ROOT_PASSWORD
56+
value: generate
4357
volumeMounts:
44-
# The /derby-init folder contains the SQL init script to create the database, the table and the sequence
45-
- name: derby-database-init-script-volume
46-
mountPath: /derby-init
47-
# The /dbs folder is where the actual database content is stored
48-
- name: derby-database-data-volume
49-
mountPath: /dbs
50-
lifecycle:
51-
postStart:
52-
# Execute the SQL init script after the derby container has started
53-
exec:
54-
command: ["java", "-Djdbc.drivers=org.apache.derbbc.EmbeddedDriver", "org.apache.derby.tools.ij", "/derby-init/init.sql"]
58+
# The /var/lib/mysql folder is where the actual database content is stored
59+
- name: mariadb-database-data-volume
60+
mountPath: /var/lib/mysql
5561
volumes:
56-
# Create a volume in order to store the SQL init file
57-
- name: derby-database-init-script-volume
58-
configMap:
59-
name: derby-database-init-script-config-map
60-
defaultMode: 0744
6162
# Explicitly create an empty dir volume in order to ensure read/write access needed to store database files
62-
- name: derby-database-data-volume
63+
- name: mariadb-database-data-volume
6364
emptyDir: {}
6465
---
6566
apiVersion: v1
6667
kind: Service
6768
metadata:
6869
labels:
69-
app.kubernetes.io/name: camel-quarkus-examples-derby-database
70+
app.kubernetes.io/name: camel-quarkus-examples-mariadb-database
7071
app.kubernetes.io/version: 3.7.0-SNAPSHOT
71-
name: derby-database
72+
name: mariadb-database
7273
spec:
7374
ports:
74-
- name: derby
75-
port: 1527
76-
targetPort: 1527
75+
- name: mariadb
76+
port: 3306
77+
targetPort: 3306
7778
selector:
78-
app.kubernetes.io/name: camel-quarkus-examples-derby-database
79+
app.kubernetes.io/name: camel-quarkus-examples-mariadb-database
7980
app.kubernetes.io/version: 3.7.0-SNAPSHOT
8081
type: ClusterIP
8182
---
8283
apiVersion: v1
83-
kind: ConfigMap
84+
kind: Secret
8485
metadata:
85-
name: derby-database-init-script-config-map
8686
labels:
87-
app.kubernetes.io/name: camel-quarkus-examples-derby-database
87+
app.kubernetes.io/name: camel-quarkus-examples-mariadb-database
8888
app.kubernetes.io/version: 3.7.0-SNAPSHOT
89+
name: mariadb-secret
90+
type: Opaque
8991
data:
90-
init.sql: |
91-
CONNECT 'jdbc:derby:my-db;create=true';
92-
CREATE TABLE CAMEL_MESSAGEPROCESSED ( processorName VARCHAR(255), messageId VARCHAR(100), createdAt TIMESTAMP, PRIMARY KEY (processorName, messageId) );
93-
CREATE SEQUENCE CAMEL_MESSAGEPROCESSED_SEQ AS INT MAXVALUE 999999 CYCLE;
94-
---
92+
# mariadb
93+
db-user: bWFyaWFkYg==
94+
# s3cr3t
95+
db-password: czNjcjN0

0 commit comments

Comments
 (0)