Skip to content

Commit f8b7a0a

Browse files
authored
Updates to the TxEventQ Java sample (#980)
* Updates
1 parent c5c8efe commit f8b7a0a

File tree

8 files changed

+197
-186
lines changed

8 files changed

+197
-186
lines changed

code-teq/javaTeq/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Transactional Event Queues (TxEventQ) example in Java
2+
3+
Transactional Event Queues (TxEventQ) is a messaging platform built into Oracle Database that is used for application workflows, microservices, and event-triggered actions.
4+
5+
## Setup
6+
1. Install an Oracle Database 23ai.
7+
1. Execute the `user_perm.sql` as the `SYS` or `SYSTEM` user.
8+
9+
## Test
10+
1. Create the TxEventQ by running the `CreateTxEventQ` class.
11+
1. Publish a message to the TxEventQ by running the `PublishTxEventQ` class.
12+
1. Consume the published message by running the `ConsumeTXEventQ` class.

code-teq/javaTeq/pom.xml

+16-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!-- Copyright (c) 2022, Oracle and/or its affiliates. -->
2+
<!-- Copyright (c) 2022, 2024, Oracle and/or its affiliates. -->
33
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->
44
<project xmlns="http://maven.apache.org/POM/4.0.0"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -19,57 +19,30 @@
1919
</properties>
2020

2121
<dependencies>
22+
<dependency>
23+
<groupId>com.oracle.database.messaging</groupId>
24+
<artifactId>aqapi-jakarta</artifactId>
25+
<version>23.3.1.0</version>
26+
</dependency>
2227
<dependency>
23-
<groupId>javax.transaction</groupId>
24-
<artifactId>javax.transaction-api</artifactId>
25-
<version>1.2</version>
28+
<groupId>jakarta.transaction</groupId>
29+
<artifactId>jakarta.transaction-api</artifactId>
30+
<version>2.0.1</version>
2631
</dependency>
2732
<dependency>
28-
<groupId>com.oracle.database.jdbc</groupId>
29-
<artifactId>ojdbc8</artifactId>
30-
<version>19.3.0.0</version>
33+
<groupId>com.oracle.database.jdbc</groupId>
34+
<artifactId>ojdbc11</artifactId>
35+
<version>23.5.0.24.07</version>
3136
</dependency>
3237
<dependency>
3338
<groupId>com.oracle.database.messaging</groupId>
3439
<artifactId>aqapi</artifactId>
35-
<version>21.3.0.0</version>
40+
<version>23.3.0.0</version>
3641
</dependency>
3742
<dependency>
38-
<groupId>javax.jms</groupId>
39-
<artifactId>javax.jms-api</artifactId>
40-
<version>2.0.1</version>
41-
</dependency>
42-
<dependency>
43-
<groupId>javax.transaction</groupId>
44-
<artifactId>jta</artifactId>
45-
<version>1.1</version>
43+
<groupId>jakarta.jms</groupId>
44+
<artifactId>jakarta.jms-api</artifactId>
45+
<version>3.1.0</version>
4646
</dependency>
4747
</dependencies>
48-
49-
<build>
50-
<plugins>
51-
<plugin>
52-
<groupId>org.codehaus.mojo</groupId>
53-
<artifactId>exec-maven-plugin</artifactId>
54-
<version>3.0.0</version>
55-
<executions>
56-
<execution>
57-
<goals>
58-
<goal>exec</goal>
59-
</goals>
60-
</execution>
61-
</executions>
62-
<configuration>
63-
<executable>java</executable>
64-
<arguments>
65-
<argument>-Doracle.jdbc.fanEnabled=false</argument>
66-
<argument>-classpath</argument>
67-
<classpath/>
68-
<argument>com.oracle.example.ConsumeTEQ</argument>
69-
</arguments>
70-
</configuration>
71-
</plugin>
72-
</plugins>
73-
</build>
74-
7548
</project>

code-teq/javaTeq/src/main/java/com/oracle/example/ConsumeTEQ.java renamed to code-teq/javaTeq/src/main/java/com/oracle/example/ConsumeTxEventQ.java

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
1-
// Copyright (c) 2022, Oracle and/or its affiliates.
1+
// Copyright (c) 2022, 2024, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
// This is an example of how to consume a message from a TEQ using Java.
55
// Please see the Maven POM file for dependencies.
66

77
package com.oracle.example;
88

9+
import java.sql.Connection;
910
import java.sql.SQLException;
1011

11-
import javax.jms.JMSException;
12-
import javax.jms.Session;
13-
import javax.jms.Topic;
14-
import javax.jms.TopicConnection;
15-
import javax.jms.TopicConnectionFactory;
16-
import javax.jms.TopicSession;
12+
import jakarta.jms.JMSException;
13+
import jakarta.jms.Session;
14+
import jakarta.jms.Topic;
15+
import jakarta.jms.TopicConnection;
16+
import jakarta.jms.TopicConnectionFactory;
1717

1818
import oracle.AQ.AQException;
19-
import oracle.jms.AQjmsFactory;
20-
import oracle.jms.AQjmsSession;
21-
import oracle.jms.AQjmsTextMessage;
22-
import oracle.jms.AQjmsTopicSubscriber;
23-
import oracle.ucp.jdbc.PoolDataSource;
24-
import oracle.ucp.jdbc.PoolDataSourceFactory;
19+
import oracle.jakarta.jms.AQjmsFactory;
20+
import oracle.jakarta.jms.AQjmsSession;
21+
import oracle.jakarta.jms.AQjmsTextMessage;
22+
import oracle.jakarta.jms.AQjmsTopicSubscriber;
23+
import oracle.jdbc.pool.OracleDataSource;
2524

26-
public class ConsumeTEQ {
25+
public class ConsumeTxEventQ {
2726

28-
private static String username = "pdbadmin";
29-
private static String url = "jdbc:oracle:thin:@//localhost:1521/pdb1";
30-
private static String topicName = "my_teq";
27+
private static final String username = "testuser";
28+
private static final String url = "jdbc:oracle:thin:@//localhost:1521/freepdb1";
29+
private static final String password = "Welcome12345";
30+
private static final String topicName = "my_jms_teq";
3131

3232
public static void main(String[] args) throws AQException, SQLException, JMSException {
3333

34-
// create a topic session
35-
PoolDataSource ds = PoolDataSourceFactory.getPoolDataSource();
36-
ds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
34+
// Create DB connection
35+
OracleDataSource ds = new OracleDataSource();
3736
ds.setURL(url);
3837
ds.setUser(username);
39-
ds.setPassword(System.getenv("DB_PASSWORD"));
38+
ds.setPassword(password);
39+
Connection con = ds.getConnection();
40+
if (con != null) {
41+
System.out.println("Connected!");
42+
}
4043

4144
// create a JMS topic connection and session
4245
TopicConnectionFactory tcf = AQjmsFactory.getTopicConnectionFactory(ds);
4346
TopicConnection conn = tcf.createTopicConnection();
4447
conn.start();
45-
TopicSession session =
46-
(AQjmsSession) conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
48+
var session = (AQjmsSession) conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
4749

4850
// create a subscriber on the topic
49-
Topic topic = ((AQjmsSession) session).getTopic(username, topicName);
51+
Topic topic = session.getTopic(username, topicName);
5052
AQjmsTopicSubscriber subscriber =
5153
(AQjmsTopicSubscriber) session.createDurableSubscriber(topic, "my_subscriber");
5254

code-teq/javaTeq/src/main/java/com/oracle/example/CreateTEQ.java

-56
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2022, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
// This is an example of how to create a TEQ using Java.
5+
// Please see the Maven POM file for dependencies.
6+
7+
package com.oracle.example;
8+
9+
import java.sql.SQLException;
10+
11+
import jakarta.jms.Destination;
12+
import jakarta.jms.JMSException;
13+
import jakarta.jms.Session;
14+
import jakarta.jms.TopicConnection;
15+
import jakarta.jms.TopicConnectionFactory;
16+
17+
import oracle.AQ.AQException;
18+
import oracle.AQ.AQQueueTableProperty;
19+
import oracle.jakarta.jms.AQjmsDestination;
20+
import oracle.jakarta.jms.AQjmsFactory;
21+
import oracle.jakarta.jms.AQjmsSession;
22+
23+
import oracle.jdbc.pool.OracleDataSource;
24+
import java.sql.Connection;
25+
26+
public class CreateTxEventQ {
27+
28+
private static final String username = "testuser";
29+
private static final String url = "jdbc:oracle:thin:@//localhost:1521/freepdb1";
30+
private static final String password = "Welcome12345";
31+
private static final String topicName = "my_jms_teq";
32+
33+
public static void main(String[] args) throws AQException, SQLException, JMSException {
34+
35+
// Create DB connection
36+
OracleDataSource ds = new OracleDataSource();
37+
ds.setURL(url);
38+
ds.setUser(username);
39+
ds.setPassword(password);
40+
Connection con = ds.getConnection();
41+
if (con != null) {
42+
System.out.println("Connected!");
43+
}
44+
45+
// create a topic session
46+
TopicConnectionFactory tcf = AQjmsFactory.getTopicConnectionFactory(ds);
47+
TopicConnection conn = tcf.createTopicConnection();
48+
conn.start();
49+
AQjmsSession session = (AQjmsSession) conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
50+
51+
// create properties
52+
AQQueueTableProperty props = new AQQueueTableProperty("SYS.AQ$_JMS_TEXT_MESSAGE");
53+
props.setMultiConsumer(true);
54+
props.setPayloadType("SYS.AQ$_JMS_TEXT_MESSAGE");
55+
56+
// create queue table, topic and start it
57+
Destination myTeq = session.createJMSTransactionalEventQueue(topicName, true);
58+
((AQjmsDestination) myTeq).start(session, true, true);
59+
60+
if (con != null && !con.isClosed()) {
61+
con.close();
62+
}
63+
}
64+
65+
}

code-teq/javaTeq/src/main/java/com/oracle/example/PublishTEQ.java

-63
This file was deleted.

0 commit comments

Comments
 (0)