forked from oleksiivorobiov/oracle_oci_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaqjmsdemo07.java
418 lines (363 loc) · 11.9 KB
/
aqjmsdemo07.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* $Header: aqjmsdemo07.java 05-jun-2007.15:10:25 aatam Exp $ */
/* Copyright (c) 2002, 2007, Oracle. All rights reserved. */
/*
DESCRIPTION
AQ/OJMS XML payload demo
PRIVATE CLASSES
<list of private classes defined - with one-line descriptions>
NOTES
see below
MODIFIED (MM/DD/YY)
aatam 06/05/07 - password need to be consistent
jleinawe 07/11/05 -
jleinawe 04/18/05 - update lcr format
jleinawe 12/20/02 - update instructions
rbhyrava 10/07/02 - import oracle.jdbc.*
jleinawe 09/04/02 - jleinawe_new_aq_demos
jleinawe 08/30/02 - Creation
*/
/**
* @version $Header: aqjmsdemo07.java 05-jun-2007.15:10:25 aatam Exp $
* @author jleinawe
* @since release specific (what release of product did this appear in)
*/
/****
* This is a sample java file which uses Oracle JMS - Java Message Service
* API to Enqueue/Dequeue an XML message using a sys.Anydata queue.
*
* This demo does the following:
* - creates an Anydata Queue
* - creates a QueueSender
* - sends a message containing an XML message body
* - create a QueueReceiver
* - receives and prints the XML message
* - drops the Anydata Queue
*
* Instructions for setting up and running this demo are found in
* aqjmsREADME.txt.
*
***/
import javax.jms.*;
import java.sql.*;
import oracle.AQ.*;
import oracle.jms.*;
import oracle.xdb.*;
public class aqjmsdemo07
{
public static void main (String args [])
throws java.sql.SQLException, ClassNotFoundException, JMSException
{
try
{
String ora_sid;
String host;
int port;
try
{
ora_sid = args[0];
host = args[1];
port = Integer.parseInt(args[2]);
System.out.println ("ora_sid: " + ora_sid);
System.out.println ("host: " + host);
System.out.println ("port: " + port);
init(ora_sid, host, port);
EnqueueAnydata(ora_sid, host, port);
DequeueAnydata(ora_sid, host, port);
tearDown(ora_sid, host, port);
System.out.println("End of Demo");
}
catch (ArrayIndexOutOfBoundsException ex)
{
System.out.println ("Not enough parameters. " +
"Usage: java filename [SID] [HOST] [PORT]\n" +
"\tExample: java aqjmsdemo07 orcl myserver 1521");
return;
}
}
catch (Exception ex)
{
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
public static void EnqueueAnydata(String ora_sid, String host, int port)
{
QueueConnectionFactory qc_fact = null;
QueueConnection q_conn = null;
QueueSession q_sess = null;
java.sql.Connection db_conn = null;
AQQueueTableProperty qt_prop = null;
AQQueueTable q_table = null;
AQjmsDestinationProperty dest_prop = null;
javax.jms.Queue queue = null;
AdtMessage adt_msg = null;
QueueSender q_sender = null;
Message jms_msg = null;
oracle.xdb.XMLType xtype = null;
String data = null;
try
{
qc_fact = AQjmsFactory.getQueueConnectionFactory(host,
ora_sid, port, "oci8");
q_conn = qc_fact.createQueueConnection("jmsuser", "JMSUSER");
q_sess = q_conn.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
q_conn.start();
db_conn = ((AQjmsSession)q_sess).getDBConnection();
queue = ((AQjmsSession)q_sess).getQueue("jmsuser", "xmlq");
//
// create sender
//
q_sender = q_sess.createSender(queue);
//
// create ade message
//
adt_msg = ((AQjmsSession)q_sess).createAdtMessage();
System.out.println("start xml convert");
data = "<ROW_LCR " +
"xmlns=\"http://xmlns.oracle.com/streams/schemas/lcr\" \n" +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
"xsi:schemaLocation=\"http://xmlns.oracle.com/streams/schemas/lcr http://xmlns.oracle.com/streams/schemas/lcr/streamslcr.xsd\"" +
"> \n" +
"<source_database_name>source_dbname</source_database_name> \n" +
"<command_type>INSERT</command_type> \n" +
"<object_owner>RAM</object_owner> \n" +
"<object_name>EMP</object_name> \n" +
"<tag>0ABC</tag> \n" +
"<transaction_id>0.0.0</transaction_id> \n" +
"<scn>0</scn> \n" +
"<old_values> \n" +
"<old_value> \n" +
"<column_name>C01</column_name> \n" +
"<data><varchar2>Clob old</varchar2></data> \n" +
"</old_value> \n" +
"<old_value> \n" +
"<column_name>C02</column_name> \n" +
"<data><varchar2>A123FF</varchar2></data> \n" +
"</old_value> \n" +
"<old_value> \n" +
"<column_name>C03</column_name> \n" +
"<data> \n" +
"<date><value>1997/11/24 00:00:00</value><format>SYYYY/MM/DD HH24:MI:SS</format></date> \n" +
"</data> \n" +
"</old_value> \n" +
"<old_value> \n" +
"<column_name>C04</column_name> \n" +
"<data> \n" +
"<timestamp><value>1999/05/31 13:20:00.000000000</value><format>SYYYY/MM/DD HH24:MI:SSXFF9</format></timestamp> \n" +
"</data> \n" +
"</old_value> \n" +
"<old_value> \n" +
"<column_name>C05</column_name> \n" +
"<data><raw>0ABCDE</raw></data> \n" +
"</old_value> \n" +
"</old_values> \n" +
"<new_values> \n" +
"<new_value> \n" +
"<column_name>C01</column_name> \n" +
"<data><varchar2>A123FF</varchar2></data> \n" +
"</new_value> \n" +
"<new_value> \n" +
"<column_name>C02</column_name> \n" +
"<data><number>35.23</number></data> \n" +
"</new_value> \n" +
"<new_value> \n" +
"<column_name>C03</column_name> \n" +
"<data><number>-100000</number></data> \n" +
"</new_value> \n" +
"<new_value> \n" +
"<column_name>C04</column_name> \n" +
"<data><varchar2>Hel lo</varchar2></data> \n" +
"</new_value> \n" +
"<new_value> \n" +
"<column_name>C05</column_name> \n" +
"<data><char>wor ld</char></data> \n" +
"</new_value> \n" +
"</new_values> \n" +
"</ROW_LCR>";
xtype = oracle.xdb.XMLType.createXML(db_conn, data);
adt_msg.setAdtPayload(xtype);
System.out.println("Enqueue anydata(xmltype) message");
//
// send the message
//
((AQjmsQueueSender)q_sender).send(queue, adt_msg,
DeliveryMode.PERSISTENT, 1,
AQjmsConstants.EXPIRATION_NEVER);
q_sess.commit();
q_sess.close();
q_conn.close();
}
catch (java.sql.SQLException sql_ex)
{
System.out.println("Exception: " + sql_ex);
sql_ex.printStackTrace();
}
catch (JMSException aq_ex)
{
System.out.println("Exception: " + aq_ex);
aq_ex.printStackTrace();
if(aq_ex.getLinkedException() != null)
{
aq_ex.getLinkedException().printStackTrace();
}
}
}
public static void DequeueAnydata(String ora_sid, String host, int port)
{
QueueConnectionFactory qc_fact = null;
QueueConnection q_conn = null;
QueueSession q_sess = null;
java.sql.Connection db_conn = null;
AQQueueTableProperty qt_prop = null;
AQQueueTable q_table = null;
AQjmsDestinationProperty dest_prop = null;
javax.jms.Queue queue = null;
AdtMessage adt_msg = null;
AdtMessage adt_msg2 = null;
QueueReceiver q_receiver= null;
TextMessage t_msg = null;
javax.jms.Message jms_msg = null;
oracle.xdb.XMLType xtype2 = null;
try
{
qc_fact = AQjmsFactory.getQueueConnectionFactory(host,
ora_sid, port, "oci8");
q_conn = qc_fact.createQueueConnection("jmsuser", "JMSUSER");
q_sess = q_conn.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
q_conn.start();
db_conn = ((AQjmsSession)q_sess).getDBConnection();
//
// get queue xmlq
//
queue = ((AQjmsSession)q_sess).getQueue("jmsuser", "xmlq");
//
// Create receiver
//
q_receiver = ((AQjmsSession)q_sess).createReceiver(queue);
//
// Receive message
//
jms_msg = (javax.jms.Message) q_receiver.receive();
//
// output message contents
//
if(jms_msg == null)
System.out.println("Null message received");
else
{
if(jms_msg instanceof AdtMessage)
{
adt_msg2 = (AdtMessage)jms_msg;
System.out.println("Retrieved message: " + adt_msg2.getAdtPayload());
if(adt_msg2.getAdtPayload() instanceof oracle.xdb.XMLType)
{
xtype2 = (XMLType)adt_msg2.getAdtPayload();
System.out.println("Data: \n" + xtype2.getStringVal());
}
System.out.println("Msg id: " + adt_msg2.getJMSMessageID());
q_sess.commit();
}
else
System.out.println("Invalid message type");
}
q_conn.close();
}
catch (java.sql.SQLException sql_ex)
{
System.out.println("Exception: " + sql_ex);
sql_ex.printStackTrace();
}
catch (JMSException aq_ex)
{
System.out.println("Exception: " + aq_ex);
aq_ex.printStackTrace();
if(aq_ex.getLinkedException() != null)
{
aq_ex.getLinkedException().printStackTrace();
}
}
catch (Exception ex)
{
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
public static void init(String ora_sid, String host, int port)
{
java.sql.Connection conn = null;
String url = null;
String jmsCall = null;
CallableStatement stmt = null;
try
{
//
// setup connection (either thin or oci8)
//
url = new String ("jdbc:oracle:oci8:");
url = url.concat("@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(PORT=" +port);
url = url.concat(")(HOST=" +host+ "))(CONNECT_DATA=(SID=" +
ora_sid+ ")))");
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
conn = DriverManager.getConnection(url, "jmsuser", "JMSUSER");
//
// create queue table xmlq_t and enable it for jms types
//
jmsCall ="{call dbms_aqadm.create_queue_table( queue_table => 'xmlq_t',"+
" multiple_consumers => false, "+
"queue_payload_type => 'sys.ANYDATA',compatible => '8.1.3')}";
stmt = conn.prepareCall(jmsCall);
stmt.execute();
// enable jms types
jmsCall = "{call dbms_aqadm.enable_jms_types('xmlq_t')}";
stmt = conn.prepareCall(jmsCall);
stmt.execute();
//
// create queue xmlq
//
jmsCall = "{call dbms_aqadm.create_queue(queue_name => 'xmlq',"+
" queue_table => 'xmlq_t')}";
stmt = conn.prepareCall(jmsCall);
stmt.execute();
//
// start queue xmlq
//
jmsCall = "{call dbms_aqadm.start_queue(queue_name => 'xmlq')}";
stmt = conn.prepareCall(jmsCall);
stmt.execute();
conn.close();
}
catch (SQLException s)
{
System.out.println("Exception: "+s);
s.printStackTrace();
}
}
public static void tearDown(String ora_sid, String host, int port)
{
java.sql.Connection conn = null;
String url = null;
String jmsCall = null;
CallableStatement stmt = null;
try
{
//
// Drop queue table 'xmlq_t'
//
url = new String ("jdbc:oracle:oci8:");
url = url.concat("@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(PORT=" +port);
url = url.concat(")(HOST=" +host+ "))(CONNECT_DATA=(SID=" +
ora_sid+ ")))");
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
conn = DriverManager.getConnection(url, "jmsuser", "JMSUSER");
jmsCall = "{call dbms_aqadm.drop_queue_table( queue_table => 'xmlq_t',"+
" force => TRUE)}";
stmt = conn.prepareCall(jmsCall);
stmt.execute();
conn.close();
}
catch (SQLException s)
{
s.printStackTrace();
}
}
}