Skip to content

Commit

Permalink
Updates to the TxEventQ Java sample (#980)
Browse files Browse the repository at this point in the history
* Updates
  • Loading branch information
andytael authored Oct 31, 2024
1 parent c5c8efe commit f8b7a0a
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 186 deletions.
12 changes: 12 additions & 0 deletions code-teq/javaTeq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Transactional Event Queues (TxEventQ) example in Java

Transactional Event Queues (TxEventQ) is a messaging platform built into Oracle Database that is used for application workflows, microservices, and event-triggered actions.

## Setup
1. Install an Oracle Database 23ai.
1. Execute the `user_perm.sql` as the `SYS` or `SYSTEM` user.

## Test
1. Create the TxEventQ by running the `CreateTxEventQ` class.
1. Publish a message to the TxEventQ by running the `PublishTxEventQ` class.
1. Consume the published message by running the `ConsumeTXEventQ` class.
59 changes: 16 additions & 43 deletions code-teq/javaTeq/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2022, Oracle and/or its affiliates. -->
<!-- Copyright (c) 2022, 2024, Oracle and/or its affiliates. -->
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -19,57 +19,30 @@
</properties>

<dependencies>
<dependency>
<groupId>com.oracle.database.messaging</groupId>
<artifactId>aqapi-jakarta</artifactId>
<version>23.3.1.0</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.2</version>
<groupId>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.5.0.24.07</version>
</dependency>
<dependency>
<groupId>com.oracle.database.messaging</groupId>
<artifactId>aqapi</artifactId>
<version>21.3.0.0</version>
<version>23.3.0.0</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
<groupId>jakarta.jms</groupId>
<artifactId>jakarta.jms-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Doracle.jdbc.fanEnabled=false</argument>
<argument>-classpath</argument>
<classpath/>
<argument>com.oracle.example.ConsumeTEQ</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>

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

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

package com.oracle.example;

import java.sql.Connection;
import java.sql.SQLException;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import jakarta.jms.JMSException;
import jakarta.jms.Session;
import jakarta.jms.Topic;
import jakarta.jms.TopicConnection;
import jakarta.jms.TopicConnectionFactory;

import oracle.AQ.AQException;
import oracle.jms.AQjmsFactory;
import oracle.jms.AQjmsSession;
import oracle.jms.AQjmsTextMessage;
import oracle.jms.AQjmsTopicSubscriber;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.jakarta.jms.AQjmsFactory;
import oracle.jakarta.jms.AQjmsSession;
import oracle.jakarta.jms.AQjmsTextMessage;
import oracle.jakarta.jms.AQjmsTopicSubscriber;
import oracle.jdbc.pool.OracleDataSource;

public class ConsumeTEQ {
public class ConsumeTxEventQ {

private static String username = "pdbadmin";
private static String url = "jdbc:oracle:thin:@//localhost:1521/pdb1";
private static String topicName = "my_teq";
private static final String username = "testuser";
private static final String url = "jdbc:oracle:thin:@//localhost:1521/freepdb1";
private static final String password = "Welcome12345";
private static final String topicName = "my_jms_teq";

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

// create a topic session
PoolDataSource ds = PoolDataSourceFactory.getPoolDataSource();
ds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
// Create DB connection
OracleDataSource ds = new OracleDataSource();
ds.setURL(url);
ds.setUser(username);
ds.setPassword(System.getenv("DB_PASSWORD"));
ds.setPassword(password);
Connection con = ds.getConnection();
if (con != null) {
System.out.println("Connected!");
}

// create a JMS topic connection and session
TopicConnectionFactory tcf = AQjmsFactory.getTopicConnectionFactory(ds);
TopicConnection conn = tcf.createTopicConnection();
conn.start();
TopicSession session =
(AQjmsSession) conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
var session = (AQjmsSession) conn.createSession(true, Session.AUTO_ACKNOWLEDGE);

// create a subscriber on the topic
Topic topic = ((AQjmsSession) session).getTopic(username, topicName);
Topic topic = session.getTopic(username, topicName);
AQjmsTopicSubscriber subscriber =
(AQjmsTopicSubscriber) session.createDurableSubscriber(topic, "my_subscriber");

Expand Down
56 changes: 0 additions & 56 deletions code-teq/javaTeq/src/main/java/com/oracle/example/CreateTEQ.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2022, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

// This is an example of how to create a TEQ using Java.
// Please see the Maven POM file for dependencies.

package com.oracle.example;

import java.sql.SQLException;

import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Session;
import jakarta.jms.TopicConnection;
import jakarta.jms.TopicConnectionFactory;

import oracle.AQ.AQException;
import oracle.AQ.AQQueueTableProperty;
import oracle.jakarta.jms.AQjmsDestination;
import oracle.jakarta.jms.AQjmsFactory;
import oracle.jakarta.jms.AQjmsSession;

import oracle.jdbc.pool.OracleDataSource;
import java.sql.Connection;

public class CreateTxEventQ {

private static final String username = "testuser";
private static final String url = "jdbc:oracle:thin:@//localhost:1521/freepdb1";
private static final String password = "Welcome12345";
private static final String topicName = "my_jms_teq";

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

// Create DB connection
OracleDataSource ds = new OracleDataSource();
ds.setURL(url);
ds.setUser(username);
ds.setPassword(password);
Connection con = ds.getConnection();
if (con != null) {
System.out.println("Connected!");
}

// create a topic session
TopicConnectionFactory tcf = AQjmsFactory.getTopicConnectionFactory(ds);
TopicConnection conn = tcf.createTopicConnection();
conn.start();
AQjmsSession session = (AQjmsSession) conn.createSession(true, Session.AUTO_ACKNOWLEDGE);

// create properties
AQQueueTableProperty props = new AQQueueTableProperty("SYS.AQ$_JMS_TEXT_MESSAGE");
props.setMultiConsumer(true);
props.setPayloadType("SYS.AQ$_JMS_TEXT_MESSAGE");

// create queue table, topic and start it
Destination myTeq = session.createJMSTransactionalEventQueue(topicName, true);
((AQjmsDestination) myTeq).start(session, true, true);

if (con != null && !con.isClosed()) {
con.close();
}
}

}
63 changes: 0 additions & 63 deletions code-teq/javaTeq/src/main/java/com/oracle/example/PublishTEQ.java

This file was deleted.

Loading

0 comments on commit f8b7a0a

Please sign in to comment.