How to use Oracle Advanced Queuing (AQ) with a Java JMS application
首先配置Oracle AQ队列和数据库对象,创建消息类型、队列表及启动队列;接着在Java项目中引入ojdbc8.jar、aqapi.jar和JMS API依赖;然后通过AQjmsFactory建立JMS连接,使用标准JMS接口发送和接收消息;支持自定义消息类型和事务控制,确保消息处理与数据库事务一致;适用于已使用Oracle数据库、需持久化异步消息的场景。

Using Oracle Advanced Queuing (AQ) with a Java JMS application allows you to leverage Oracle Database as a message broker. This integration supports reliable, transactional messaging using standard JMS APIs while storing messages in database tables. Here’s how to set it up and use it effectively.
Configure Oracle AQ and Database Objects
Before connecting from Java, ensure AQ is enabled and required database objects are created.
Create a queue table and queue:
-- Example: Create an object type for payload CREATE OR REPLACE TYPE msg_type AS OBJECT ( text_content VARCHAR2(4000) ); / <p>-- Create queue table BEGIN DBMS_AQADM.CREATE_QUEUE_TABLE( queue_table => 'jms_queue_tab', queue_payload_type => 'MSG_TYPE', compatible => '10.0' ); END; /</p><p>-- Create the queue BEGIN DBMS_AQADM.CREATE_QUEUE( queue_name => 'jms_queue', queue_table => 'jms_queue_tab' ); END; /</p><p>-- Start the queue BEGIN DBMS_AQADM.START_QUEUE(queue_name => 'jms_queue'); END; /</p>
Set Up Java Dependencies
To use Oracle AQ with JMS, include the required JAR files in your project:
- ojdbc8.jar – Oracle JDBC driver
- aqapi.jar – Oracle AQ JMS client library
- javax.jms-api.jar or equivalent (JMS 1.1+)
If using Maven, ensure these are added manually or via a repository if available.
Connect Using Oracle AQ JMS API
Oracle provides an implementation of the JMS interface through AQjmsSession and related classes. Use AQjmsFactory to establish connections.
Sample code to send a message:
import javax.jms.*;
import oracle.jms.AQjmsFactory;
import java.sql.Connection;
import java.sql.DriverManager;
<p>public class AQMessageSender {
public static void main(String[] args) throws Exception {
// Connect to Oracle DB
Connection dbConn = DriverManager.getConnection(
"jdbc:oracle:thin:@//localhost:1521/ORCL",
"username", "password");</p><pre class='brush:php;toolbar:false;'>// Get JMS connection
QueueConnectionFactory qcf = AQjmsFactory.getQueueConnectionFactory(dbConn);
QueueConnection qc = qcf.createQueueConnection();
QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Access the queue
Queue queue = ((AQjmsSession)qs).getQueue("username", "jms_queue");
// Send message
QueueSender sender = qs.createSender(queue);
TextMessage msg = qs.createTextMessage("Hello from JMS via Oracle AQ");
sender.send(msg);
System.out.println("Message sent.");
// Clean up
sender.close();
qs.close();
qc.close();
dbConn.close();} }
Sample code to receive a message:
public class AQMessageReceiver {
public static void main(String[] args) throws Exception {
Connection dbConn = DriverManager.getConnection(
"jdbc:oracle:thin:@//localhost:1521/ORCL",
"username", "password");
<pre class='brush:php;toolbar:false;'>QueueConnectionFactory qcf = AQjmsFactory.getQueueConnectionFactory(dbConn);
QueueConnection qc = qcf.createQueueConnection();
QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = ((AQjmsSession)qs).getQueue("username", "jms_queue");
QueueReceiver receiver = qs.createReceiver(queue);
qc.start(); // Required to enable message delivery
Message msg = receiver.receive(3000); // Wait up to 3 seconds
if (msg != null && msg instanceof TextMessage) {
System.out.println("Received: " + ((TextMessage) msg).getText());
} else {
System.out.println("No message received.");
}
receiver.close();
qs.close();
qc.close();
dbConn.close();} }
Handle Payload Types and Transactions
Oracle AQ supports both raw JMS messages and user-defined object types. When using custom types like MSG_TYPE, ensure the JMS message matches the expected structure.
For transaction control:
- Use
Session.SESSION_TRANSACTEDwhen creating the session - Call
session.commit()to dequeue successfully, orrollback()to retain the message
This ensures message processing aligns with database transactions.
Basically, integrating Oracle AQ with JMS involves setting up AQ queues, using Oracle's JMS implementation, and managing DB-backed message flow through standard JMS patterns. It’s especially useful in environments already using Oracle Database for data and needing asynchronous, durable messaging without adding a separate broker.
The above is the detailed content of How to use Oracle Advanced Queuing (AQ) with a Java JMS application. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20519
7
13632
4
How to troubleshoot the Oracle Listener startup? (Network Services)
Mar 10, 2026 am 12:58 AM
Oraclelistenerstartupfailuresstemfromsilentlistener.oraparsingerrors,hostnameresolutionissues,orpermissionproblems—notbinariesorports;validatesyntaxwithreload,checkownership,verifyactualconfigpath,testDNS,useexplicitIPs,confirmADR_BASE,enabletracingp
How to use Sequences in Oracle to generate IDs? (Auto-increment)
Mar 06, 2026 am 01:16 AM
ID auto-increment in Oracle requires the cooperation of SEQUENCE and BEFOREINSERT triggers, and the trigger must check: NEW.IDISNULL; 12c supports IDENTITY but is not compatible with older versions and disables explicit insertion.
How to patch Oracle Grid Infrastructure? (System Maintenance)
Mar 10, 2026 am 01:00 AM
Three things must be confirmed before applying the GI patch: 1. The opatchlsinventory-detail output of each node is consistent; 2. OCR and VoteDisk are online and crsctlcheckcluster-all and ocrcheck both return SUCCESS; 3. $GRID_HOME/crs/install/rootcrs.sh-prepatch has been successfully executed.
How to use Oracle APEX to build a low-code app? (Rapid Development)
Mar 13, 2026 am 12:48 AM
OracleAPEXislow-glue,notno-code:itskipsinfrastructurebutrequiresSQL,PL/SQL,anddeclarativelogic;ApplicationProcesseshandleserver-sidevalidationandsideeffects,DynamicActionsmanageclient-sideinteractivity;InteractiveGridneedskey-preservedsourcesforediti
How to implement Transparent Data Encryption (TDE) in Oracle? (Data Security)
Mar 13, 2026 am 12:14 AM
OracleTDE must first enable and open the encrypted wallet (Wallet), otherwise ORA-28365 will be reported when executing ALTERTABLESPACE...ENCRYPTION; Wallet needs to be created, opened and managed through the ADMINISTERKEYMANAGEMENT command, and the path must be explicitly configured in sqlnet.ora and permissions must be ensured.
How to manage Flashback Data Archive_Flashback Data Archive table space allocation
Mar 28, 2026 pm 04:06 PM
The reason why the FlashbackDataArchive table space is full is that the hidden history table (SYS_FBA_HIST_XXXXXX) occupies the table space where the main table is located and does not go through ASSM cleaning; you need to use ALTERFLASHBACKARCHIVE...MODIFYTABLESPACE to migrate to the local management automatic segment space table space, and manually clean up the orphan history table.
How to use JSON data types in Oracle Database? (NoSQL Features)
Mar 08, 2026 am 01:03 AM
In Oracle's JSON scenario, you should select VARCHAR2 (4000CHAR) plus ISJSON constraints (small documents) or BLOB plus ISJSON constraints (large documents), and disable CLOB; ISJSON is a column-level constraint syntax, not a function call; the JSON_VALUE path must be a string literal; JSON_EXISTS needs to be speeded up with the JSON_VALUE function index.
How to grant flashback permission_GRANT FLASHBACK ON and FLASHBACK ANY TABLE
Apr 03, 2026 pm 11:54 PM
FLASHBACK permissions must be explicitly granted: GRANTFLASHBACKONschema.tableTOuser for a single table, and GRANTFLASHBACKANYTABLETOuser for all tables; basic permissions such as SELECT and ALTER and row movement enablement are also required.





