-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathaqorademo01.java
306 lines (249 loc) · 9.77 KB
/
aqorademo01.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* $Header: aqorademo01.java 12-aug-2003.12:51:11 rbhyrava Exp $ */
/* Copyright (c) 2000, 2003, Oracle Corporation. All rights reserved. */
/*
DESCRIPTION
<short description of component this file declares/defines>
PRIVATE CLASSES
<list of private classes defined - with one-line descriptions>
NOTES
<other useful comments, qualifications, etc.>
MODIFIED (MM/DD/YY)
rbhyrava 08/12/03 - stop/drop queue/queuetables
rbhyrava 10/07/02 - import oracle.jdbc.*
rbhyrava 03/20/00 - use driver arg
rbhyrava 03/16/00 - AQ API demos
rbhyrava 03/15/00 - AQ API demo - Enqueue,Dequeue RAW Message
rbhyrava 03/15/00 - Creation
*/
/**
* @version $Header: aqorademo01.java 12-aug-2003.12:51:11 rbhyrava Exp $
* @author rbhyrava
* @since release specific (what release of product did this appear in)
*/
/***
* This is a sample java file which uses Oracle AQ API to enqueue and
* dequeue RAW messages
* -- Create a Queue
* -- Enqueue RAW Message
* Dequeue RAW message
*
* The following instructions describe how to compile and execute
* this sample on the client machine.
*
* System requirements:
* ====================
* 1) Oracle 8.1.6 database or higher
* 2) The client machine should have JDK 1.1.x or JDK1.2 or higher installed
* 3) The following jar/zip files should be in the CLASSPATH on the client
* machine.
* For JDK1.2.x
* classes12.zip
* aqapi.jar
* For JDK1.1.x
* classes111.zip
* aqapi11.jar
* Set up CLASSPATH, PATH, LD_LIBRARY_PATH based on JDK version and platform.
* Compilation and Running:
* ========================
* 4) If you already have the jars in step 3) in classpath
* javac aqorademo01.java
*
* 5) java aqorademo01 <SID> <HOST> <PORT> <DRIVER>
* Example usage:
* java aqorademo01 orcl82 dlsun666 1521 thin
*
* Thin driver is used in the demo. Modify the connect string to use oci8
* jdbc driver
***/
/* Set up main class from which we will call subsequent examples and handle
exceptions: */
import java.sql.*;
import oracle.AQ.*;
public class aqorademo01
{
public static void main(String args[])
{
AQSession aq_sess = null;
try
{
if (args.length < 4 )
System.out.println("Usage:java filename [SID] [HOST] [PORT] [DRIVER]");
else {
aq_sess = createSession(args);
/* now Enqueue and Dequeue Messages */
createQTable(aq_sess) ;
enqRawMsg(aq_sess);
deqRawMsg(aq_sess);
dropQTable(aq_sess);
System.out.println("End of Demo") ;
}
}
catch (Exception ex)
{
System.out.println("Exception-1: " + ex);
ex.printStackTrace();
}
}
/* Create an Java AQ Session for the 'aqjava' user as shown in the
AQDriverManager section above: */
public static AQSession createSession(String args[]) throws Exception
{
Connection db_conn;
AQSession aq_sess = null;
try
{
Class.forName("oracle.jdbc.OracleDriver");
String drv,url;
drv=args[3];
if ( drv.toLowerCase().compareTo("thin") == 0)
url="jdbc:oracle:"+args[3]+":@"+args[1]+":"+args[2]+":"+args[0];
else {
url= new String ("jdbc:oracle:oci8:");
url= url.concat("@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)" +
"(PORT=" + args[2] +
")(HOST=" + args[1] + "))(CONNECT_DATA=(SID=" +
args[0] + ")))");
}
//"jdbc:oracle:thin:@host:port:sid"
System.out.println("Connect String is :"+url) ;
db_conn = DriverManager.getConnection(url, "aqjava", "aqjava");
System.out.println("JDBC Connection opened ");
db_conn.setAutoCommit(false);
/* Load the Oracle8i AQ driver: */
Class.forName("oracle.AQ.AQOracleDriver");
/* Create an AQ Session: */
aq_sess = AQDriverManager.createAQSession(db_conn);
System.out.println("Successfully created AQSession ");
}
catch (Exception ex)
{
System.out.println("Exception: " + ex);
throw ex;
}
return aq_sess;
}
public static void createQTable(AQSession aq_sess) throws Exception
{
AQQueueTableProperty qtable_prop;
AQQueueProperty queue_prop;
AQQueueTable q_table;
AQQueue queue;
try {
/* Create a AQQueueTableProperty object (payload type - RAW): */
qtable_prop = new AQQueueTableProperty("RAW");
/* Drop the queue if already exists */
try {
q_table = aq_sess.getQueueTable ("aqjava", "aq_table4" );
q_table.drop(true);
} catch (Exception e) {} ;
/* Create a queue table called aq_table4 in aqjava schema: */
q_table=aq_sess.createQueueTable ("aqjava", "aq_table4", qtable_prop);
System.out.println("Successfully created aq_table4 in aqjava schema");
/* Create a new AQQueueProperty object */
queue_prop = new AQQueueProperty();
/* Create a queue called aq_queue4 in aq_table4: */
queue = aq_sess.createQueue (q_table, "aq_queue4", queue_prop);
System.out.println("Successfully created aq_queue4 in aq_table4");
/* Enable enqueue/dequeue on this queue: */
queue.start();
System.out.println("Successful start queue");
} catch (Exception e) {
System.out.println("Error in createQTable:"+ e);
throw e;
}
}
public static void enqRawMsg(AQSession aq_sess) throws Exception
{
AQQueueTable q_table;
AQQueue queue;
AQMessage message;
AQRawPayload raw_payload;
AQEnqueueOption enq_option;
String test_data = "new message";
byte[] b_array;
Connection db_conn;
try {
db_conn = ((AQOracleSession)aq_sess).getDBConnection();
/* Get a handle to queue table - aq_table4 in aqjava schema: */
q_table = aq_sess.getQueueTable ("aqjava", "aq_table4");
System.out.println("Successful getQueueTable");
/* Get a handle to a queue - aq_queue4 in aquser schema: */
queue = aq_sess.getQueue ("aqjava", "aq_queue4");
System.out.println("Successful getQueue");
/* Create a message to contain raw payload: */
message = queue.createMessage();
/* Get handle to the AQRawPayload object and populate it with raw data: */
b_array = test_data.getBytes();
raw_payload = message.getRawPayload();
raw_payload.setStream(b_array, b_array.length);
/* Create a AQEnqueueOption object with default options: */
enq_option = new AQEnqueueOption();
/* Enqueue the message: */
queue.enqueue(enq_option, message);
db_conn.commit();
} catch (Exception e) {
System.out.println("Exception during Enqueue: " + e) ;
throw e;
}
}
public static void deqRawMsg(AQSession aq_sess) throws Exception
{
AQQueueTable q_table;
AQQueue queue;
AQMessage message;
AQRawPayload raw_payload;
AQEnqueueOption enq_option;
String test_data = "new message";
AQDequeueOption deq_option;
byte[] b_array;
Connection db_conn;
try {
db_conn = ((AQOracleSession)aq_sess).getDBConnection();
/* Get a handle to queue table - aq_table4 in aqjava schema: */
q_table = aq_sess.getQueueTable ("aqjava", "aq_table4");
System.out.println("Successful getQueueTable");
/* Get a handle to a queue - aq_queue4 in aquser schema: */
queue = aq_sess.getQueue ("aqjava", "aq_queue4");
System.out.println("Successful getQueue");
/* Create a message to contain raw payload: */
message = queue.createMessage();
/* Get handle to the AQRawPayload object and populate it with raw data: */
b_array = test_data.getBytes();
raw_payload = message.getRawPayload();
raw_payload.setStream(b_array, b_array.length);
/* Create a AQEnqueueOption object with default options: */
enq_option = new AQEnqueueOption();
/* Enqueue the message: */
queue.enqueue(enq_option, message);
System.out.println("Successful enqueue");
db_conn.commit();
/* Create a AQDequeueOption object with default options: */
deq_option = new AQDequeueOption();
/* Dequeue a message: */
message = queue.dequeue(deq_option);
System.out.println("Successful dequeue");
/* Retrieve raw data from the message: */
raw_payload = message.getRawPayload();
b_array = raw_payload.getBytes();
db_conn.commit();
} catch (Exception e) {
System.out.println("Exception during Dequeue: " + e) ;
throw e;
}
}
public static void dropQTable(AQSession aq_sess) throws Exception
{
AQQueueTable q_table;
AQQueue queue ;
try {
q_table = aq_sess.getQueueTable ("aqjava", "aq_table4" );
queue = aq_sess.getQueue("aqjava", "aq_queue4");
queue.stop(true, true, true) ;
q_table.dropQueue("aq_queue4");
q_table.drop(true);
} catch (Exception e) {
System.out.println("Error in dropQTable:"+ e);
} ;
System.out.println("Successfully dropped aq_table4 in aqjava schema");
}
}