-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathaqjmsdemo01.java
222 lines (190 loc) · 7.43 KB
/
aqjmsdemo01.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
/* $Header: aqjmsdemo01.java 05-jun-2007.15:10:19 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 - jdk 1.5
jleinawe 12/20/02 - update instructions
rbhyrava 01/09/01 - bug 1419924
rbhyrava 11/14/00 - remove pwd at the end
rbhyrava 07/13/00 - fix compilation error
rbhyrava 03/16/00 - AQ jms demos
rbhyrava 03/15/00 - AQ JMS demo - Enqueue,Dequeue Text Message
rbhyrava 03/15/00 - Creation
*/
/**
* @version $Header: aqjmsdemo01.java 05-jun-2007.15:10:19 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 Enqueue/Dequeue text Message. Rule-based subscription on message properties and/or the message content of a topic.
*
* This demo does the following:
* -- Setup a topic
* -- Create two Durable Subscribers.Specify a selector that represents
* a specification (selects) for the messages that the subscriber wishes
* to receive.
* -- Publish sereral Messages to the Topic
* -- Receive the Messages for each subscriber
*
* Instructions for setting up and running this demo are found in
* aqjmsREADME.txt.
*
***/
/* import useful packages */
import oracle.AQ.*;
import oracle.jms.*;
import javax.jms.*;
import java.lang.*;
public class aqjmsdemo01
{
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 qtprop ;
AQQueueTable qtable;
AQjmsDestinationProperty dprop;
Topic topic;
try {
/* Create Queue Tables */
System.out.println("Creating Input Queue Table...") ;
/* Drop the queue if already exists */
try {
qtable=((AQjmsSession)tsess).getQueueTable("jmsuser", "jmsqtable" );
qtable.drop(true);
} catch (Exception e) {} ;
qtprop = new AQQueueTableProperty ("SYS.AQ$_JMS_TEXT_MESSAGE") ;
qtprop.setMultiConsumer(true) ;
qtprop.setCompatible("8.1") ;
qtprop.setPayloadType("SYS.AQ$_JMS_TEXT_MESSAGE") ;
qtable = ((AQjmsSession)tsess).createQueueTable("JMSUSER",
"jmsqtable", qtprop) ;
System.out.println ("Creating Topic input_queue...");
dprop = new AQjmsDestinationProperty() ;
topic=((AQjmsSession)tsess).createTopic( qtable,"jmstopic",dprop) ;
/* Start the topic */
((AQjmsDestination)topic).start(tsess, true, true);
System.out.println("Successfully setup Topic");
} catch (Exception ex) {
System.out.println("Error in setupTopic: " + ex);
throw ex;
}
}
public static void performJmsOperations(TopicSession tsess)
{
Topic topic =null;
TopicSubscriber tsub1,tsub2;
TopicPublisher publisher;
TextMessage txtmsg, dtxtmsg;
try
{
System.out.println ("Get the Topic...");
topic = ((AQjmsSession)tsess).getTopic("JMSUSER","jmstopic") ;
System.out.println("Creating Topic Subscribers...") ;
tsub1 = tsess.createDurableSubscriber(topic, "dallas",
"(year = 1998 OR color NOT IN ('GREEN','RED','WHITE')) "+
" AND make IN ('ACURA ', 'BMW', 'MERCEDES')", false);
tsub2 = tsess.createDurableSubscriber(topic, "atlanta",
"price < 20000", false );
System.out.println("Publish messages...") ;
publisher = tsess.createPublisher(topic);
txtmsg = tsess.createTextMessage() ;
txtmsg.setText("Cars Distribution") ;
txtmsg.setObjectProperty("carno", new Integer(12345)) ;
txtmsg.setStringProperty("color", "BLUE") ;
txtmsg.setIntProperty("year", 1999) ;
txtmsg.setStringProperty("make", "BMW") ;
txtmsg.setDoubleProperty("price", 25995) ;
txtmsg.setJMSCorrelationID("dallas") ;
publisher.publish(topic, txtmsg) ;
txtmsg.clearProperties() ;
txtmsg.setObjectProperty("carno", new Integer(55)) ;
txtmsg.setStringProperty("color", "CYAN") ;
txtmsg.setIntProperty("year", 2000) ;
txtmsg.setStringProperty("make", "MERCEDES") ;
txtmsg.setDoubleProperty("price", 19000) ;
txtmsg.setJMSCorrelationID("atlanta") ;
publisher.publish(topic, txtmsg) ;
txtmsg.clearProperties() ;
txtmsg.setObjectProperty("carno", new Integer(99099)) ;
txtmsg.setStringProperty("color", "RED") ;
txtmsg.setIntProperty("year", 1998) ;
txtmsg.setStringProperty("make", "ACURA") ;
txtmsg.setDoubleProperty("price", 19995) ;
txtmsg.setJMSCorrelationID("atlanta") ;
publisher.publish(topic, txtmsg) ;
tsess.commit() ;
/* Receive the text Message for two subscribers */
boolean done=false ;
System.out.println ("Dequeue Message for Subscriber 1") ;
while (!done) {
dtxtmsg = (TextMessage) (tsub1.receiveNoWait() ) ;
if (dtxtmsg == null) {
done=true;
} else
{
System.out.print(" Color: " + dtxtmsg.getStringProperty("color"));
System.out.print(" Make: " + dtxtmsg.getStringProperty("make"));
System.out.print(" Year: " + dtxtmsg.getStringProperty("year"));
System.out.print(" Price: " + dtxtmsg.getStringProperty("price"));
System.out.println(" Carno: " + dtxtmsg.getStringProperty("carno"));
}
}
System.out.println ("Dequeue Message for Subscriber 2") ;
done=false ;
while (!done) {
dtxtmsg = (TextMessage) (tsub2.receive(1) ) ;
if ( dtxtmsg == null ) {
done=true ;
} else {
System.out.print(" Color: " + dtxtmsg.getStringProperty("color"));
System.out.print(" Make: " + dtxtmsg.getStringProperty("make"));
System.out.print(" Year: " + dtxtmsg.getStringProperty("year"));
System.out.print(" Price: " + dtxtmsg.getStringProperty("price"));
System.out.println(" Carno: " + dtxtmsg.getStringProperty("carno"));
}
}
tsess.commit() ;
((AQjmsDestination)topic).stop(tsess, true, true, false);
} catch (Exception e) {
System.out.println("Error in performJmsOperations: " + e) ;
}
}
}