Skip to content

Commit 6012529

Browse files
Polish samples (#137)
Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 8 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 15 additions & 8 deletions
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

Lines changed: 8 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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.
Lines changed: 19 additions & 8 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 13 additions & 2 deletions
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

0 commit comments

Comments
 (0)