forked from oleksiivorobiov/oracle_oci_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaqjmsdemo06.java
274 lines (235 loc) · 9.19 KB
/
aqjmsdemo06.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
/* $Header: aqjmsdemo06.java 05-jun-2007.15:10:24 aatam Exp $ */
/* Copyright (c) 2000, 2007, Oracle. 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)
aatam 06/05/07 - password need to be consistent
jleinawe 07/11/05 -
jleinawe 03/12/03 - add unschedulePropagation call
jleinawe 12/20/02 - update instructions
rbhyrava 01/09/01 - bug 1419924
rbhyrava 07/13/00 - fix typo
rbhyrava 03/16/00 - AQ jms demos
rbhyrava 03/15/00 - AQ JMS demo - Propagation
rbhyrava 03/15/00 - Creation
*/
/**
* @version $Header: aqjmsdemo06.java 05-jun-2007.15:10:24 aatam Exp $
* @author rbhyrava
* @since release specific (what release of product did this appear in)
*/
/***
* This is a sample java file which uses Oracle JMS - Java Messaging Service
* API to schedule Propagation between Queues in Oracle database
*
* This demo is functionally same a newaqdemo01.sql and does the following:
* -- Create two queue tables - input_queue_table, prop_queue_table
* -- Create two queues - input_queue belonging to input_queue_table,
* prop_queue belonging to prop_queue_table
* -- Create two subscribers to input_queue - prog1, prog2
* -- Create one subscribers to input_queue - prog3 at prop_queue
* -- Schedule propagation between input_queue and other queues in
* the database
*
* Instructions for setting up and running this demo are found in
* aqjmsREADME.txt.
*
***/
/* Object Message */
/* import useful packages */
import oracle.AQ.*;
import oracle.jms.*;
import javax.jms.*;
import java.lang.*;
public class aqjmsdemo06
{
public static void main (String args [])
throws java.sql.SQLException, ClassNotFoundException, JMSException
{
TopicSession tsess = null;
TopicConnectionFactory tcfact=null;
TopicConnection tconn=null;
try
{
if (args.length < 4 )
System.out.println("Usage:java filename [SID] [HOST] [PORT] [DRIVER]");
else {
tcfact = AQjmsFactory.getTopicConnectionFactory(
args[1], args[0], Integer.parseInt(args[2]), args[3]);
tconn = tcfact.createTopicConnection( "jmsuser","JMSUSER");
/* Create a Topic Session */
tsess = tconn.createTopicSession(true, Session.CLIENT_ACKNOWLEDGE);
tconn.start() ;
setupTopic(tsess) ;
performJmsOperations(tsess);
tsess.close();
tconn.close();
System.out.println("End of Demo") ;
}
}
catch (Exception ex)
{
System.out.println("Exception-1: " + ex);
ex.printStackTrace();
}
}
public static void setupTopic(TopicSession tsess) throws Exception
{
AQQueueTableProperty qtprop1,qtprop2 ;
AQQueueTable qtable,table1, table2;
AQjmsDestinationProperty dprop;
Topic topic1, topic2;
try {
/* Create Queue Tables */
System.out.println("Creating Input Queue Table...") ;
/* Drop the queue if already exists */
try {
qtable=((AQjmsSession)tsess).getQueueTable("jmsuser",
"INPUT_QUEUE_TABLE" );
qtable.drop(true);
} catch (Exception e) {} ;
try {
qtable=((AQjmsSession)tsess).getQueueTable("jmsuser",
"PROP_QUEUE_TABLE" );
qtable.drop(true);
} catch (Exception e) {} ;
qtprop1 = new AQQueueTableProperty ("SYS.AQ$_JMS_OBJECT_MESSAGE") ;
qtprop1.setComment("input queue") ;
qtprop1.setMultiConsumer(true) ;
qtprop1.setCompatible("8.1") ;
qtprop1.setPayloadType("SYS.AQ$_JMS_OBJECT_MESSAGE") ;
table1 = ((AQjmsSession)tsess).createQueueTable("JMSUSER",
"INPUT_QUEUE_TABLE", qtprop1) ;
System.out.println("Creating Propagation Queue Table...") ;
qtprop2 = new AQQueueTableProperty ("SYS.AQ$_JMS_OBJECT_MESSAGE") ;
qtprop2.setComment("Popagation queue") ;
qtprop2.setPayloadType("SYS.AQ$_JMS_OBJECT_MESSAGE") ;
qtprop2.setMultiConsumer(true) ;
qtprop2.setCompatible("8.1") ;
table2 = ((AQjmsSession)tsess).createQueueTable("JMSUSER",
"PROP_QUEUE_TABLE", qtprop2) ;
System.out.println ("Creating Topic input_queue...");
dprop = new AQjmsDestinationProperty() ;
dprop.setComment("create topic 1") ;
topic1=((AQjmsSession)tsess).createTopic(table1,"INPUT_QUEUE",dprop) ;
dprop.setComment("create topic 2") ;
topic2=((AQjmsSession)tsess).createTopic( table2,"PROP_QUEUE",dprop) ;
/* Start the topic */
((AQjmsDestination)topic1).start(tsess, true, true);
((AQjmsDestination)topic2).start(tsess, true, true);
System.out.println("Successfully setup Topics");
} catch (Exception ex) {
System.out.println("Error in setupTopic: " + ex);
throw ex;
}
}
public static void performJmsOperations(TopicSession tsess)
throws Exception
{
Topic topic1,topic2;
TopicSubscriber[] subs;
AQjmsAgent agt;
ObjectMessage objmsg = null, robjmsg=null;
TopicPublisher publisher ;
Message sobj , rmsg;
String[] cities={"BELMONT","REDWOOD SHORES", "SUNNYVALE", "BURLINGAME" };
try
{
System.out.println("Get Topics...") ;
topic1 = ((AQjmsSession)tsess).getTopic("JMSUSER", "INPUT_QUEUE") ;
topic2 = ((AQjmsSession)tsess).getTopic("JMSUSER", "PROP_QUEUE") ;
System.out.println("Creating Topic Subscribers...") ;
subs = new TopicSubscriber[3] ;
subs[0] = ((AQjmsSession)tsess).createDurableSubscriber(
topic1, "PROG1", null, false);
subs[1] = ((AQjmsSession)tsess).createDurableSubscriber(
topic1, "PROG2", "JMSPriority > 2", false);
subs[2] = tsess.createDurableSubscriber(
topic2, "PROG3", null , false) ;
agt = new AQjmsAgent("PROG3", "PROP_QUEUE" ) ;
System.out.println("Creating Remote Subscriber...") ;
((AQjmsSession)tsess).createRemoteSubscriber(
topic1, agt,"JMSPriority = 2");
/* Schedule Propagation with latency 0 */
System.out.println("Schedule Propagation...") ;
((AQjmsDestination)topic1).schedulePropagation(
tsess, null, null, null, null, new Double(0)) ;
System.out.println("Publish messages...") ;
objmsg = ((AQjmsSession)tsess).createObjectMessage() ;
publisher = tsess.createPublisher(topic1);
/*publish 100 messages*/
for ( int i = 1 ; i <= 100 ; i++)
{
objmsg.setIntProperty("Id",i) ;
if ( ( i % 3 ) == 0 ) {
objmsg.setStringProperty("City",cities[0]) ;
}
else if ((i % 4 ) == 0) {
objmsg.setStringProperty("City",cities[1]) ;
}
else if (( i % 2) == 0) {
objmsg.setStringProperty("City",cities[2]) ;
}
else {
objmsg.setStringProperty("City",cities[3]) ;
}
objmsg.setIntProperty("Priority",(1+ (i%3))) ;
sobj = new Message() ;
sobj.setId(i) ;
sobj.setName("message# "+i) ;
sobj.setData(500);
objmsg.setObject(sobj) ;
objmsg.setJMSCorrelationID(""+i) ;
objmsg.setJMSPriority(1+(i%3)) ;
publisher.publish(topic1,objmsg, DeliveryMode.PERSISTENT,
1 +(i%3), AQjmsConstants.EXPIRATION_NEVER);
}
System.out.println("Commit now...") ;
tsess.commit() ;
Thread.sleep(50000);
/* Receive messages for each subscriber */
System.out.println("Receive Messages...") ;
for (int i=0; i< subs.length ; i++)
{
System.out.println ("Messages for subscriber : " +i) ;
if (subs[i].getMessageSelector() != null)
{
System.out.println(" with selector: " +
subs[i].getMessageSelector());
}
boolean done = false;
while(!done) {
try {
robjmsg = (ObjectMessage)( subs[i].receiveNoWait() );
if (robjmsg != null)
{
rmsg = (Message)robjmsg.getObject();
System.out.print("Name : " +rmsg.getName()) ;
System.out.print(" Pri: " +robjmsg.getJMSPriority()) ;
System.out.print(" Message: " +robjmsg.getIntProperty("Id")) ;
System.out.print(" " +robjmsg.getStringProperty("City")) ;
System.out.println(" " +robjmsg.getIntProperty("Priority")) ;
}
else {
System.out.println ("No more messages.") ;
done=true;
}
tsess.commit();
} catch (Exception e) {
System.out.println("Error in performJmsOperations: " + e) ;
done=true;
}
} /* while loop*/
}
((AQjmsDestination)topic1).unschedulePropagation(tsess,null);
} catch (Exception e) {
System.out.println("Error in performJmsOperations: " + e) ;
throw e;
}
} /* end of demo06 */
}