Skip to content

Commit eeb848b

Browse files
TxEventQ Spring Docs (#1019)
Signed-off-by: Anders Swanson <[email protected]>
1 parent 6a349d3 commit eeb848b

File tree

6 files changed

+256
-79
lines changed

6 files changed

+256
-79
lines changed

docs-source/transactional-event-queues/content/getting-started/message-operations.md

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ This section explains message operations using queues, topics, and different pro
1515
* [Kafka Consumer](#kafka-consumer)
1616
* [Enqueuing and Dequeuing with JMS](#enqueuing-and-dequeuing-with-jms)
1717
* [JMS APIs](#jms-apis)
18-
* [Spring JMS](#spring-jms)
1918
* [Message Operations in Other Languages and APIs](#message-operations-in-other-languages-and-apis)
2019
* [Message Expiry and Exception Queues](#message-expiry-and-exception-queues)
2120
* [Message Delay](#message-delay)
@@ -264,82 +263,6 @@ try (Connection conn = cf.createConnection()) {
264263
}
265264
```
266265

267-
##### Spring JMS
268-
269-
To configure Spring JMS to use Oracle Database Transactional Event Queues, you'll need to ensure your Spring datasource configuration is setup for Oracle Database:
270-
271-
```yaml
272-
spring:
273-
datasource:
274-
username: ${USERNAME}
275-
password: ${PASSWORD}
276-
url: ${JDBC_URL}
277-
278-
# Set these to use UCP over Hikari.
279-
driver-class-name: oracle.jdbc.OracleDriver
280-
type: oracle.ucp.jdbc.PoolDataSourceImpl
281-
oracleucp:
282-
initial-pool-size: 1
283-
min-pool-size: 1
284-
max-pool-size: 30
285-
connection-pool-name: UCPSampleApplication
286-
connection-factory-class-name: oracle.jdbc.pool.OracleDataSource
287-
```
288-
289-
Additionally, you should define a JMS ConnectionFactory bean using the AQjmsFactory class. The presence of the ConnectionFactory bean ensures Spring JMS uses Oracle Database Transactional Event Queues as the JMS provider for message operations.
290-
291-
```java
292-
@Bean
293-
public ConnectionFactory aqJmsConnectionFactory(DataSource ds) throws JMSException {
294-
return AQjmsFactory.getConnectionFactory(ds);
295-
}
296-
```
297-
298-
The following Spring JMS Producer class makes use of Spring’s JMSTemplate, which is available via autowiring. The JMSTemplate class provides a simple API for working with JMS — here we use the convertAndSend method to produce messages to a queue.
299-
300-
The JMSTemplate class provides a variety of JMS functionality, and may also be used to receive messages.
301-
302-
```java
303-
import org.springframework.beans.factory.annotation.Value;
304-
import org.springframework.jms.core.JmsTemplate;
305-
import org.springframework.stereotype.Component;
306-
307-
@Component
308-
public class Producer {
309-
private final JmsTemplate jmsTemplate;
310-
private final String queueName;
311-
312-
public Producer(JmsTemplate jmsTemplate,
313-
@Value("${txeventq.queue.name:testqueue}") String queueName) {
314-
this.jmsTemplate = jmsTemplate;
315-
this.queueName = queueName;
316-
}
317-
318-
public void enqueue(String message) {
319-
jmsTemplate.convertAndSend(queueName, message);
320-
}
321-
}
322-
```
323-
324-
The following Spring JMS Consumer class uses Spring’s @JmsListener annotation to set up a message receiver for a given queue — in this case, a queue named “testqueue”. The basic consumer prints each message it receives to stdout.
325-
326-
```java
327-
import java.util.concurrent.CountDownLatch;
328-
329-
import org.springframework.beans.factory.annotation.Value;
330-
import org.springframework.jms.annotation.JmsListener;
331-
import org.springframework.stereotype.Component;
332-
333-
@Component
334-
public class Consumer {
335-
336-
@JmsListener(destination = "${txeventq.queue.name:testqueue}", id = "sampleConsumer")
337-
public void receiveMessage(String message) {
338-
System.out.printf("Received message: %s%n", message);
339-
}
340-
}
341-
```
342-
343266
##### Message Operations in Other Languages and APIs
344267

345268
For Python, Javascript, .NET, and ORDS, refer to the respective documentation for code samples:

docs-source/transactional-event-queues/content/kafka/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ Oracle offers a Kafka connector for TxeventQ, enabling seamless integration of m
4646
## Transactional Messaging
4747

4848
One of TxEventQ's unique features is its ability to combine messaging and database operations within a single transaction. This capability, often referred to as the "transactional outbox" pattern, ensures data consistency across microservices. We'll explore this pattern through the Kafka Java Client for Oracle Database Transactional Event Queues.
49+
50+
## Spring Boot Integration
51+
52+
Spring Boot integration is supported through the [Kafka Java Client for Oracle Database Transactional Event Queues' Spring Boot Starter](https://central.sonatype.com/artifact/com.oracle.database.spring/oracle-spring-boot-starter-okafka). You can read more about the starter in the [Spring Boot Kafka section](../spring-boot/kafka.md).

docs-source/transactional-event-queues/content/kafka/kafka-connectors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title = "Kafka Connectors"
44
weight = 3
55
+++
66

7-
This section provides examples using Kafka connectors to connect Oracle Database Transactional Event Queues with other data and messaging systems, like [Apache Kafka](https://github.com/oracle/okafka/tree/master/connectors).
7+
This section introduces Kafka connectors to connect Oracle Database Transactional Event Queues with other data and messaging systems, like [Apache Kafka](https://github.com/oracle/okafka/tree/master/connectors) topics.
88

99
## Kafka Connectors
1010

docs-source/transactional-event-queues/content/spring-boot/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The Oracle Spring Boot Starter for AQ/JMS simplifies TxEventQ integration with S
1616

1717
## Spring Boot Starter for Kafka Java Client for Oracle Database Transactional Event Queues
1818

19-
The Kafka Java Client for TxEventQ Spring Boot Starter pulls in all necessary dependencies to work with TxEventQ's [Kafka Java API](https://github.com/oracle/okafka) using Spring Boot.
19+
The Kafka Java Client for Oracle Database Transactional Event Queues Spring Boot Starter pulls in all necessary dependencies to work with Transactional Event Queues [Kafka Java API](https://github.com/oracle/okafka) using Spring Boot.
2020

2121
## TxEventQ Spring Cloud Stream Binder
2222

docs-source/transactional-event-queues/content/spring-boot/jms.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,144 @@ archetype = "page"
33
title = "Spring JMS"
44
weight = 1
55
+++
6+
7+
Java Message Service (JMS) is an API that provides a standardized way for Java applications to create, send, receive, and read messages in messaging systems. [Spring JMS](https://spring.io/guides/gs/messaging-jms) uses idiomatic APIs to produce and consume messages with JMS, using the JMSTemplate class and @JMSListener annotations for producers and consumers, respectively.
8+
9+
In this section, we’ll implement a producer/consumer example using Spring JMS with Oracle Database Transactional Event Queues JMS APIs.
10+
11+
* [Project Dependencies](#project-dependencies)
12+
* [Configure Permissions and Create a JMS Queue](#configure-permissions-and-create-a-jms-queue)
13+
* [Connect Spring JMS to Oracle Database](#connect-spring-jms-to-oracle-database)
14+
* [JMSTemplate Producer](#jmstemplate-producer)
15+
* [Receive messages with @JMSListener](#receive-messages-with-jmslistener)
16+
17+
## Project Dependencies
18+
19+
To start developing with the Spring JMS for Oracle Database Transactional Event Queues, add the [oracle-spring-boot-starter-aqjms](https://central.sonatype.com/artifact/com.oracle.database.spring/oracle-spring-boot-starter-aqjms) dependency to your Maven project, along with Spring Boot JDBC:
20+
21+
```xml
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
25+
<version>${spring-boot.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.oracle.database.spring</groupId>
29+
<artifactId>oracle-spring-boot-starter-aqjms</artifactId>
30+
<version>${oracle-database-starters.version}</version>
31+
</dependency>
32+
```
33+
34+
For Gradle users, add the following dependency to your project:
35+
36+
```groovy
37+
implementation "org.springframework.boot:spring-boot-starter-data-jdbc:${springBootVersion}"
38+
implementation "com.oracle.database.spring:oracle-spring-boot-starter-aqjms:${oracleDatabaseStartersVersion}"
39+
```
40+
41+
## Configure Permissions and Create a JMS Queue
42+
43+
The following SQL script grants the necessary permissions to use Transactional Event Queues with JMS to a database user, and then creates a Transactional Event Queue with a [JMS Payload Type](../getting-started/core-concepts.md#dbms_aqadmjms_type) for use by that user. We’ll dynamically invoke this script in our test later on, and it is provided here for reference purposes.
44+
45+
```sql
46+
grant aq_user_role to testuser;
47+
grant execute on dbms_aq to testuser;
48+
grant execute on dbms_aqadm to testuser;
49+
grant execute ON dbms_aqin TO testuser;
50+
grant execute ON dbms_aqjms TO testuser;
51+
grant execute on dbms_teqk to testuser;
52+
53+
begin
54+
-- Create a JMS queue
55+
dbms_aqadm.create_transactional_event_queue(
56+
queue_name => 'testuser.testqueue',
57+
queue_payload_type => DBMS_AQADM.JMS_TYPE,
58+
-- FALSE means queues can only have one consumer for each message. This is the default.
59+
-- TRUE means queues created in the table can have multiple consumers for each message.
60+
multiple_consumers => false
61+
);
62+
63+
-- Start the queue
64+
dbms_aqadm.start_queue(
65+
queue_name => 'testuser.testqueue'
66+
);
67+
end;
68+
/
69+
```
70+
71+
## Connect Spring JMS to Oracle Database
72+
73+
Spring JMS with Oracle Database Transactional Event Queues uses a standard Oracle Database JDBC connection to produce and consume messages. To configure this with YAML-style Spring datasource properties, it'll look something like this (`src/main/resources/application.yaml`):
74+
75+
```yaml
76+
spring:
77+
datasource:
78+
username: ${USERNAME}
79+
password: ${PASSWORD}
80+
url: ${JDBC_URL}
81+
82+
driver-class-name: oracle.jdbc.OracleDriver
83+
type: oracle.ucp.jdbc.PoolDataSourceImpl
84+
oracleucp:
85+
initial-pool-size: 1
86+
min-pool-size: 1
87+
max-pool-size: 30
88+
connection-pool-name: UCPSampleApplication
89+
connection-factory-class-name: oracle.jdbc.pool.OracleDataSource
90+
```
91+
92+
Additionally, you should define a JMS ConnectionFactory bean using the AQjmsFactory class. The presence of the ConnectionFactory bean ensures Spring JMS uses Oracle Database Transactional Event Queues as the JMS provider for message operations.
93+
94+
```java
95+
@Bean
96+
public ConnectionFactory aqJmsConnectionFactory(DataSource ds) throws JMSException {
97+
return AQjmsFactory.getConnectionFactory(ds);
98+
}
99+
```
100+
101+
## JMSTemplate Producer
102+
103+
The Spring JMSTemplate bean provides an interface for JMS operations, including producing and consuming messages. The following class uses the `jmsTemplate.convertAndSend()` method to produce a message to a queue.
104+
105+
```java
106+
import org.springframework.beans.factory.annotation.Value;
107+
import org.springframework.jms.core.JmsTemplate;
108+
import org.springframework.stereotype.Component;
109+
110+
@Component
111+
public class Producer {
112+
private final JmsTemplate jmsTemplate;
113+
private final String queueName;
114+
115+
public Producer(JmsTemplate jmsTemplate,
116+
@Value("${txeventq.queue.name:testqueue}") String queueName) {
117+
this.jmsTemplate = jmsTemplate;
118+
this.queueName = queueName;
119+
}
120+
121+
public void enqueue(String message) {
122+
jmsTemplate.convertAndSend(queueName, message);
123+
}
124+
}
125+
```
126+
127+
## Receive messages with @JMSListener
128+
129+
The `@JMSListener` annotation may be applied to a method to configure a receiver for messages from a JMS queue.
130+
131+
The following class uses the @JMSListener annotation to read messages from a queue named `testqueue`, printing each message to stdout.
132+
133+
```java
134+
import org.springframework.beans.factory.annotation.Value;
135+
import org.springframework.jms.annotation.JmsListener;
136+
import org.springframework.stereotype.Component;
137+
138+
@Component
139+
public class Consumer {
140+
141+
@JmsListener(destination = "${txeventq.queue.name:testqueue}", id = "sampleConsumer")
142+
public void receiveMessage(String message) {
143+
System.out.printf("Received message: %s%n", message);
144+
}
145+
}
146+
```

docs-source/transactional-event-queues/content/spring-boot/kafka.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,112 @@ archetype = "page"
33
title = "Kafka Starter"
44
weight = 2
55
+++
6+
7+
This section provides information on getting started using the Kafka Java Client for Oracle Database Transactional Event Queues from a Spring Boot application.
8+
9+
You can learn more about the Kafka APIs of Oracle Database Transactional Event Queues in the [Kafka chapter](../kafka/_index.md).
10+
11+
## Project Dependencies
12+
13+
[The Kafka Java Client for Oracle Database Transactional Event Queues Spring Boot Starter](https://central.sonatype.com/artifact/com.oracle.database.spring/oracle-spring-boot-starter-okafka) pulls in all necessary dependencies for developers to work with Transactional Event Queues' Kafka Java API using Spring Boot.
14+
15+
If you're using Maven, add the following dependency to your project:
16+
17+
```xml
18+
<dependency>
19+
<groupId>com.oracle.database.spring</groupId>
20+
<artifactId>oracle-spring-boot-starter-okafka</artifactId>
21+
<version>${oracle-database-starters.version}</version>
22+
</dependency>
23+
```
24+
25+
Or, if you're using Gradle:
26+
27+
```groovy
28+
implementation 'com.oracle.database.spring:oracle-spring-boot-starter-okafka:${oracleDatabaseStartersVersion}'
29+
```
30+
31+
## Configuring the Starter
32+
33+
We'll create a simple Spring Configuration class that pulls in properties for configuring the Kafka Java Client for Oracle Database Transactional Event Queues. You can read more about these configuration properties in the [Developing With Kafka APIs](../kafka/developing-with-kafka.md) section.
34+
35+
```java
36+
@Configuration
37+
public class OKafkaConfiguration {
38+
public static final String TOPIC_NAME = "OKAFKA_SAMPLE";
39+
40+
@Value("${ojdbc.path}")
41+
private String ojdbcPath;
42+
43+
@Value("${bootstrap.servers}")
44+
private String bootstrapServers;
45+
46+
// We use the default 23ai Free service name
47+
@Value("${service.name:freepdb1}")
48+
private String serviceName;
49+
50+
// We use plaintext for a containerized, local database.
51+
// Use SSL for wallet connections, like Autonomous Database.
52+
@Value("${security.protocol:PLAINTEXT}")
53+
private String securityProtocol;
54+
55+
@Bean
56+
@Qualifier("okafkaProperties")
57+
public Properties kafkaProperties() {
58+
return OKafkaUtil.getConnectionProperties(ojdbcPath,
59+
bootstrapServers,
60+
securityProtocol,
61+
serviceName);
62+
}
63+
}
64+
```
65+
66+
### Configuring a Producer Bean
67+
68+
We can now configure a sample producer bean using the org.oracle.okafka.clients.producer.KafkaProducer class:
69+
70+
```java
71+
@Bean
72+
@Qualifier("sampleProducer")
73+
public Producer<String, String> sampleProducer() throws IOException {
74+
// Create the OKafka Producer.
75+
Properties props = kafkaProperties();
76+
props.put("enable.idempotence", "true");
77+
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
78+
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
79+
// Note the use of the org.oracle.okafka.clients.producer.KafkaProducer class, for Oracle TxEventQ.
80+
return new KafkaProducer<String, String>(props);
81+
}
82+
```
83+
84+
The Producer bean can be autowired into Spring components to write messages to Oracle Database Transactional Event Queue topics.. For a complete example of writing data to topics, see [Producing messages to Kafka Topics](../kafka/developing-with-kafka.md#producing-messages).
85+
86+
### Configuring a Consumer Bean
87+
88+
Next, we'll configure a sample consumer bean using the org.oracle.okafka.clients.consumer.KafkaConsumer class:
89+
90+
```java
91+
@Bean
92+
@Qualifier("sampleConsumer")
93+
public Consumer<String, String> sampleConsumer() {
94+
// Create the OKafka Consumer.
95+
Properties props = kafkaProperties();
96+
props.put("group.id" , "MY_CONSUMER_GROUP");
97+
props.put("enable.auto.commit","false");
98+
props.put("max.poll.records", 2000);
99+
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
100+
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
101+
props.put("auto.offset.reset", "earliest");
102+
// Note the use of the org.oracle.okafka.clients.consumer.KafkaConsumer class, for Oracle TxEventQ.
103+
return new KafkaConsumer<>(props);
104+
}
105+
```
106+
107+
The Consumer bean can be autowired into Spring components to poll messages from Oracle Database Transactional Event Queue topics. For a complete consumer example, see [Consuming messages from Kafka topics](../kafka/developing-with-kafka.md#consuming-messages).
108+
109+
## Sample Application Code
110+
111+
The following samples provide application code using the Spring starter.
112+
113+
- [Oracle Spring Boot Sample for JSON Events and the Kafka Java Client for Oracle Database Transactional Event Queues](https://github.com/oracle/spring-cloud-oracle/tree/main/database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-json-events)
114+
- [Oracle Spring Boot Sample for the Kafka Java Client for Oracle Database Transactional Event Queues](https://github.com/oracle/spring-cloud-oracle/tree/main/database/starters/oracle-spring-boot-starter-samples/oracle-spring-boot-sample-okafka)

0 commit comments

Comments
 (0)