Skip to content

Commit 6012529

Browse files
Polish samples (#137)
Signed-off-by: Anders Swanson <[email protected]>
1 parent a6d9d91 commit 6012529

File tree

18 files changed

+109
-59
lines changed

18 files changed

+109
-59
lines changed

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-duality/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The sample application test uses Testcontainers, and creates a temporary Oracle
1616
To run the test application, run the following command:
1717

1818
```shell
19-
mvn test
19+
mvn test -Dtest=JSONDualitySampleApplicationTest
2020
```
2121

2222
## Configure your project to use Oracle JSON Relational Duality Views

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-duality/src/main/java/com/oracle/database/spring/jsonduality/Course.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
package com.oracle.database.spring.jsonduality;
44

5-
import java.util.UUID;
6-
75
public class Course {
86
private String _id;
97
private String name;
108
private String description;
119
private Integer credits;
1210
private LectureHall lecture_hall;
1311

14-
public static Course createCourse() {
15-
Course course = new Course();
16-
course.set_id(UUID.randomUUID().toString());
17-
return course;
18-
}
19-
2012
public Course() {
2113
}
2214

@@ -67,4 +59,15 @@ public LectureHall getLecture_hall() {
6759
public void setLecture_hall(LectureHall lecture_hall) {
6860
this.lecture_hall = lecture_hall;
6961
}
62+
63+
@Override
64+
public String toString() {
65+
return "Course{" +
66+
"_id='" + _id + '\'' +
67+
", name='" + name + '\'' +
68+
", description='" + description + '\'' +
69+
", credits=" + credits +
70+
", lecture_hall=" + lecture_hall +
71+
'}';
72+
}
7073
}

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-duality/src/main/java/com/oracle/database/spring/jsonduality/Enrollment.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
package com.oracle.database.spring.jsonduality;
44

5-
import java.util.UUID;
6-
75
public class Enrollment {
86
private String _id;
97
private Course course;
108

11-
public Enrollment createEnrollment() {
12-
Enrollment enrollment = new Enrollment();
13-
enrollment.set_id(UUID.randomUUID().toString());
14-
return enrollment;
15-
}
16-
179
public Enrollment() {}
1810

1911
public Enrollment(String _id, Course course) {
@@ -36,4 +28,12 @@ public Course getCourse() {
3628
public void setCourse(Course course) {
3729
this.course = course;
3830
}
31+
32+
@Override
33+
public String toString() {
34+
return "Enrollment{" +
35+
"_id='" + _id + '\'' +
36+
", course=" + course +
37+
'}';
38+
}
3939
}

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-duality/src/main/java/com/oracle/database/spring/jsonduality/LectureHall.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
package com.oracle.database.spring.jsonduality;
44

5-
import java.util.UUID;
6-
75
public class LectureHall {
86
private String _id;
97
private String name;
108

11-
public static LectureHall createLecureHall() {
12-
LectureHall hall = new LectureHall();
13-
hall.set_id(UUID.randomUUID().toString());
14-
return hall;
15-
}
16-
179
public LectureHall() {}
1810

1911
public LectureHall(String _id, String name) {
@@ -36,4 +28,12 @@ public String getName() {
3628
public void setName(String name) {
3729
this.name = name;
3830
}
31+
32+
@Override
33+
public String toString() {
34+
return "LectureHall{" +
35+
"_id='" + _id + '\'' +
36+
", name='" + name + '\'' +
37+
'}';
38+
}
3939
}

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-duality/src/main/java/com/oracle/database/spring/jsonduality/Student.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
package com.oracle.database.spring.jsonduality;
44

55
import java.util.List;
6-
import java.util.UUID;
76

87

9-
public class Student {
8+
public final class Student {
109
private String _id;
1110
private String first_name;
1211
private String last_name;
@@ -16,12 +15,6 @@ public class Student {
1615
private double credits;
1716
private List<Enrollment> enrollments;
1817

19-
public static Student createStudent() {
20-
Student student = new Student();
21-
student.set_id(UUID.randomUUID().toString());
22-
return student;
23-
}
24-
2518
public Student() {}
2619

2720
public Student(String _id, String first_name, String last_name, String email, String major, double gpa, double credits, List<Enrollment> enrollments) {
@@ -98,4 +91,18 @@ public List<Enrollment> getEnrollments() {
9891
public void setEnrollments(List<Enrollment> enrollments) {
9992
this.enrollments = enrollments;
10093
}
94+
95+
@Override
96+
public String toString() {
97+
return "Student{" +
98+
"_id='" + _id + '\'' +
99+
", first_name='" + first_name + '\'' +
100+
", last_name='" + last_name + '\'' +
101+
", email='" + email + '\'' +
102+
", major='" + major + '\'' +
103+
", gpa=" + gpa +
104+
", credits=" + credits +
105+
", enrollments=" + enrollments +
106+
'}';
107+
}
101108
}

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-duality/src/test/java/com/oracle/database/spring/jsonduality/JSONDualitySampleApplicationTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@ static void properties(DynamicPropertyRegistry registry) {
5050

5151
@Test
5252
void jsonDualityViewsSampleApplication() {
53+
System.out.println("#### Querying Courses By Name:");
5354
// fetch courses
5455
List<Course> courseByName = courseService.getCourseByName("Introduction to Computer Science");
5556
assertThat(courseByName).hasSize(1);
5657
Course introToCS = courseByName.get(0);
58+
System.out.println("#### Intro to Computer Science:\n" + introToCS);
5759
courseByName = courseService.getCourseByName("Data Structures and Algorithms");
5860
assertThat(courseByName).hasSize(1);
5961
Course dsAndAlgo = courseByName.get(0);
62+
System.out.println("\n#### Data Structures and Algorithms:\n" + dsAndAlgo);
6063

61-
64+
System.out.println("\n\n\n#### Enrolling Student in CS101");
6265
// Enroll existing student in a new course
6366
Student aliceSmith = getStudent("Alice", "Smith");
6467
Enrollment introToCSEnrollment = new Enrollment();
@@ -76,7 +79,10 @@ void jsonDualityViewsSampleApplication() {
7679
.isEqualTo(introToCS.getName());
7780
assertThat(asEnrollments.get(0).getCourse().getLecture_hall().getName())
7881
.isEqualTo("Hoffman Hall");
82+
System.out.println("#### Enrollment created:\n" + aliceSmith);
83+
7984

85+
System.out.println("\n\n\n#### Creating new student with two enrollments");
8086
// Create a new student with two enrollments
8187
Student bobSwanson = new Student();
8288
bobSwanson.setFirst_name("Robert");
@@ -94,6 +100,7 @@ void jsonDualityViewsSampleApplication() {
94100
// Verify student created with enrollments
95101
bobSwanson = getStudent("Robert", "Swanson");
96102
assertThat(bobSwanson.getEnrollments()).hasSize(2);
103+
System.out.println("#### Student created:\n" + bobSwanson);
97104
}
98105

99106
private Student getStudent(String firstName, String lastName) {

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-events/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The sample application test uses Testcontainers, and creates a temporary Oracle
1616
To run application test, run the following command:
1717

1818
```shell
19-
mvn test
19+
mvn test -Dtest=JSONEventsSampleTest
2020
```
2121

2222
The test starts a sensor data consumer, and sends a series of raw weather station events to the producer. The test verifies that the events have been processed and saved to the database, available in JSON Relational Duality View form.
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Collections;
66
import java.util.Properties;
77
import java.util.concurrent.ExecutionException;
8+
import java.util.concurrent.Future;
89

910
import jakarta.annotation.PostConstruct;
1011
import org.apache.kafka.clients.admin.Admin;
@@ -13,24 +14,26 @@
1314
import org.oracle.okafka.clients.admin.AdminClient;
1415
import org.springframework.beans.factory.annotation.Qualifier;
1516
import org.springframework.beans.factory.annotation.Value;
16-
import org.springframework.context.annotation.Configuration;
1717
import org.springframework.core.task.AsyncTaskExecutor;
18+
import org.springframework.stereotype.Component;
1819

1920
/**
2021
* OKafkaSetup creates the app's OKafka topic, and starts the consumer thread.
2122
*/
22-
@Configuration
23-
public class OKafkaSetup {
23+
@Component
24+
public class OKafkaComponent {
2425
private final AsyncTaskExecutor asyncTaskExecutor;
2526
private final SensorConsumer sensorConsumer;
2627
private final Properties okafkaProperties;
2728

28-
@Value("${app.topic}")
29+
@Value("${app.topic:weathersensor}")
2930
private String topic;
3031

31-
public OKafkaSetup(@Qualifier("applicationTaskExecutor") AsyncTaskExecutor asyncTaskExecutor,
32-
SensorConsumer sensorConsumer,
33-
@Qualifier("okafkaProperties") Properties okafkaProperties) {
32+
private Future<?> consumer;
33+
34+
public OKafkaComponent(@Qualifier("applicationTaskExecutor") AsyncTaskExecutor asyncTaskExecutor,
35+
SensorConsumer sensorConsumer,
36+
@Qualifier("okafkaProperties") Properties okafkaProperties) {
3437
this.asyncTaskExecutor = asyncTaskExecutor;
3538
this.sensorConsumer = sensorConsumer;
3639
this.okafkaProperties = okafkaProperties;
@@ -50,6 +53,14 @@ void init() {
5053
throw new RuntimeException(e);
5154
}
5255
}
53-
asyncTaskExecutor.submit(sensorConsumer);
56+
consumer = asyncTaskExecutor.submit(sensorConsumer);
57+
}
58+
59+
public void await() {
60+
try {
61+
consumer.get();
62+
} catch (InterruptedException | ExecutionException e) {
63+
throw new RuntimeException(e);
64+
}
5465
}
5566
}

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-events/src/main/java/com/oracle/database/spring/jsonevents/OKafkaConfiguration.java

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public Consumer<String, Sensor> okafkaConsumer() {
6969
props.put("group.id", consumerGroup);
7070
props.put("enable.auto.commit","false");
7171
props.put("max.poll.records", 2000);
72+
props.put("auto.offset.reset", "earliest");
7273

7374
Deserializer<String> keyDeserializer = new StringDeserializer();
7475
Deserializer<Sensor> valueDeserializer = new JSONBDeserializer<>(jsonb, Sensor.class);

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-events/src/main/java/com/oracle/database/spring/jsonevents/SensorConsumer.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,36 @@ public class SensorConsumer implements Runnable, AutoCloseable {
2020
private final String topic;
2121
private final SensorEnricher sensorEnricher;
2222
private final SensorService sensorService;
23+
// Used to end the consumer for example/testing purposes
24+
private final int limit;
2325

2426
public SensorConsumer(@Qualifier("okafkaConsumer") Consumer<String, Sensor> consumer,
25-
@Value("${app.topic}") String topic,
27+
@Value("${app.topic:weathersensor}") String topic,
28+
@Value("${app.consumer.limit:15}") int limit,
2629
SensorEnricher sensorEnricher,
2730
SensorService sensorService) {
2831
this.consumer = consumer;
2932
this.topic = topic;
33+
this.limit = limit;
3034
this.sensorEnricher = sensorEnricher;
3135
this.sensorService = sensorService;
36+
3237
}
3338

3439
@Override
3540
public void run() {
3641
consumer.subscribe(List.of(topic));
42+
int consumedRecords = 0;
3743
while (true) {
3844
ConsumerRecords<String, Sensor> records = consumer.poll(Duration.ofMillis(100));
3945
processRecords(records);
4046
// Commit records when done processing.
41-
consumer.commitAsync();
47+
consumer.commitSync();
48+
// End the consumed once we have consumed all records.
49+
consumedRecords += records.count();
50+
if (consumedRecords >= limit) {
51+
return;
52+
}
4253
}
4354
}
4455

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-events/src/main/java/com/oracle/database/spring/jsonevents/SensorEventProducer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class SensorEventProducer implements AutoCloseable {
2121
private final SensorEventParser sensorEventParser;
2222

2323
public SensorEventProducer(@Qualifier("okafkaProducer") Producer<String, Sensor> producer,
24-
@Value("${app.topic}") String topic,
24+
@Value("${app.topic:weathersensor}") String topic,
2525
SensorEventParser sensorEventParser) {
2626
this.producer = producer;
2727
this.topic = topic;

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-events/src/main/resources/application.yaml

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
app:
2-
topic: weathersensor
3-
consumerGroup: weathersensor
4-
bootstrapServers: localhost:1521
5-
ojdbcPath: /path/to/ojbdc/properties
6-
71
spring:
82
jpa:
93
hibernate:
@@ -27,5 +21,5 @@ server:
2721

2822
logging:
2923
level:
30-
org.apache.kafka: WARN
31-
org.oracle.okafka: WARN
24+
org.apache.kafka: FATAL
25+
org.oracle.okafka: FATAL
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
log4j.rootLogger=INFO, stdout
2-
log4j.logger.org.apache.kafka=WARN
3-
log4j.logger.org.oracle.okafka=WARN
2+
log4j.logger.org.apache.kafka=FATAL
3+
log4j.logger.org.oracle.okafka=FATAL

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-events/src/test/java/com/oracle/database/spring/jsonevents/JSONEventsSampleTest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,31 @@ public static void setUp() throws Exception {
6565
@Autowired
6666
SensorController sensorController;
6767

68+
@Autowired
69+
OKafkaComponent okafkaComponent;
70+
6871

6972
@Test
7073
void jsonEventsSampleAppTest() throws InterruptedException {
7174
// Produce events for two different stations
75+
System.out.println("Produced events for ST001");
7276
sensorController.produce(event1());
77+
System.out.println("Produced events for ST002");
7378
sensorController.produce(event2());
79+
80+
System.out.println("Waiting for consumer to process all events");
7481
// Wait for queues to process all events
75-
Thread.sleep(3000);
82+
okafkaComponent.await();
83+
7684
// Assert all events have been processed and are available in the database
7785
ResponseEntity<List<Sensor>> st001Events = sensorController.getEvents("ST001");
7886
ResponseEntity<List<Sensor>> st002Events = sensorController.getEvents("ST002");
87+
88+
System.out.printf("Received %d events for ST001\n", st001Events.getBody().size());
7989
assertEquals(st001Events.getBody().size(), 5);
90+
System.out.printf("Received %d events for ST002\n", st002Events.getBody().size());
8091
assertEquals(st002Events.getBody().size(), 10);
92+
8193
}
8294

8395
private SensorEvent event1() {

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-okafka/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The sample application test uses Testcontainers, and creates a temporary Oracle
2121
To run the test application, run the following command:
2222

2323
```shell
24-
mvn test
24+
mvn test -Dtest=OKafkaSampleTest
2525
```
2626

2727
## Configure your project to use the Kafka Java Client for Oracle Database Transactional Event Queues

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-okafka/src/main/java/com/oracle/database/spring/okafka/OKafkaConfiguration.java

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public SampleConsumer<String> sampleConsumer() {
7676
props.put("max.poll.records", 2000);
7777
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7878
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
79+
props.put("auto.offset.reset", "earliest");
7980
// Note the use of the org.oracle.okafka.clients.producer.KafkaConsumer class, for Oracle TxEventQ.
8081
Consumer<String, String> okafkaConsumer = new KafkaConsumer<>(props);
8182
return new SampleConsumer<>(okafkaConsumer, TOPIC_NAME, expectedMessages);

database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-okafka/src/main/java/com/oracle/database/spring/okafka/SampleConsumer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void run() {
3030
consumer.subscribe(List.of(topic));
3131
int consumedRecords = 0;
3232
while (true) {
33-
ConsumerRecords<String, T> records = consumer.poll(Duration.ofMillis(100));
33+
ConsumerRecords<String, T> records = consumer.poll(Duration.ofMillis(500));
3434
System.out.println("Consumed records: " + records.count());
3535
consumedRecords += records.count();
3636
if (consumedRecords >= expectedMessages) {

0 commit comments

Comments
 (0)