summaryrefslogtreecommitdiff
path: root/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/transacted/QueueToTopic.java
blob: f3bf9f8686dcf1001717d991c4aa727fbad4811a (plain)
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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.example.jmsexample.transacted;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;

/**
 * Transactional message example sends a number of messages to a Queue
 * and then uses a transacted session to move them from the Queue to a Topic.
 * <p/>
 * <p>The program completes the following steps:
 * <ul>
 * <li>Publish the specified number of messages to the queue.</li>
 * <li>Within a transacted session consume all messages from the queue
 * and publish them to the topic.</li>
 * <li>By default commit the transacted session, unless the "<code>-rollback true</code>"
 * option is specified in which case roll it back.</li>
 * <li>Check for outstanding messages on the queue.</li>
 * <li>Check for outstanding messages on the topic.</li>
 * </ul>
 * <p/>
 */
public class QueueToTopic
{
    /* Used in log output. */
    private static final String CLASS="QueueToTopic";


    /* Specify if the transaction is committed */
    private boolean _commit;

    /**
     * Create a QueueToTopic client.
     *
     * @param commit Specifies if the transaction should be committed.
     */
    public QueueToTopic(boolean commit)
    {
        _commit=commit;
    }

    /**
     * Run the message mover example.
     *
     * @param args Command line arguments.
     */
    public static void main(String[] args)
    {
        boolean commit=true;
        if (args.length > 1)
        {
            if (args[0].equalsIgnoreCase("-rollback"))
            {
                commit=!Boolean.getBoolean(args[1]);
            }
        }
        QueueToTopic mover=new QueueToTopic(commit);
        mover.runTest();
    }

    private void runTest()
    {
        try
        {
            // Load JNDI properties
            Properties properties=new Properties();
            properties.load(this.getClass().getResourceAsStream("transacted.properties"));

            //Create the initial context
            Context ctx=new InitialContext(properties);

            // Lookup the connection factory
            ConnectionFactory conFac=(ConnectionFactory) ctx.lookup("qpidConnectionfactory");
            // create the connection
            Connection connection=conFac.createConnection();

            // As this application is using a MessageConsumer we need to set an ExceptionListener on the connection
            // so that errors raised within the JMS client library can be reported to the application
            System.out.println(
                    CLASS + ": Setting an ExceptionListener on the connection as sample uses a MessageConsumer");

            connection.setExceptionListener(new ExceptionListener()
            {
                public void onException(JMSException jmse)
                {
                    // The connection may have broken invoke reconnect code if available.
                    System.err.println(CLASS + ": The sample received an exception through the ExceptionListener");
                    System.exit(0);
                }
            });

            // Start the connection
            connection.start();

            /**
             * Create nonTransactedSession. This non-transacted auto-ack session is used to create the MessageProducer
             * that is used to populate the queue and the MessageConsumer that is used to consume the messages
             * from the topic.
             */
            System.out.println(CLASS + ": Creating a non-transacted, auto-acknowledged session");
            Session nonTransactedSession=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Lookup the queue
            Queue queue=(Queue) ctx.lookup("transactedQueue");

            // Lookup the topic
            Topic topic=(Topic) ctx.lookup("transactedTopic");

            // Make sure that the queue is empty
            System.out.print(CLASS + ": Purging messages from queue...");
            MessageConsumer queueMessageConsumer=nonTransactedSession.createConsumer(queue);
            Message purgedMessage;
            int numberPurged=-1;
            do
            {
                purgedMessage=queueMessageConsumer.receiveNoWait();
                numberPurged++;
            }
            while (purgedMessage != null);
            System.out.println(numberPurged + " message(s) purged.");

            // Create the MessageProducer for the queue
            System.out.println(CLASS + ": Creating a MessageProducer for the queue");
            MessageProducer messageProducer=nonTransactedSession.createProducer(queue);

            // Now create the MessageConsumer for the topic
            System.out.println(CLASS + ": Creating a MessageConsumer for the topic");
            MessageConsumer topicMessageConsumer=nonTransactedSession.createConsumer(topic);

            // Create a textMessage. We're using a TextMessage for this example.
            System.out.println(CLASS + ": Creating a TestMessage to send to the destination");
            TextMessage textMessage=nonTransactedSession.createTextMessage("Sample text message");

            // Loop to publish the requested number of messages to the queue.
            for (int i=1; i <= 5; i++)
            {
                messageProducer
                        .send(textMessage, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY,
                                Message.DEFAULT_TIME_TO_LIVE);

                // Print out details of textMessage just sent
                System.out.println(CLASS + ": Message sent: " + i + " " + textMessage.getJMSMessageID());
            }

            // Create a new transacted Session to move the messages from the queue to the topic
            Session transactedSession=connection.createSession(true, Session.SESSION_TRANSACTED);

            // Create a new message consumer from the queue
            MessageConsumer transactedConsumer=transactedSession.createConsumer(queue);

            // Create a new message producer for the topic
            MessageProducer transactedProducer=transactedSession.createProducer(topic);

            // Loop to consume the messages from the queue and publish them to the topic
            Message receivedMessage;
            for (int i=1; i <= 5; i++)
            {
                // Receive a message
                receivedMessage=transactedConsumer.receive();
                System.out.println(CLASS + ": Moving message: " + i + " " + receivedMessage.getJMSMessageID());
                // Publish it to the topic
                transactedProducer.send(receivedMessage);
            }

            // Either commit or rollback the transacted session based on the command line args.
            if (_commit)
            {
                System.out.println(CLASS + ": Committing transacted session.");
                transactedSession.commit();
            }
            else
            {
                System.out.println(CLASS + ": Rolling back transacted session.");
                transactedSession.rollback();
            }

            // Now consume any outstanding messages on the queue
            System.out.print(CLASS + ": Mopping up messages from queue");
            if (_commit)
            {
                System.out.print(" (expecting none)...");
            }
            else
            {
                System.out.print(" (expecting " + 5 + ")...");
            }

            Message moppedMessage;
            int numberMopped=0;
            do
            {
                moppedMessage=queueMessageConsumer.receiveNoWait();
                if (moppedMessage != null)
                {
                    numberMopped++;
                }
            }
            while (moppedMessage != null);
            System.out.println(numberMopped + " message(s) mopped.");

            // Now consume any outstanding messages for the topic subscriber
            System.out.print(CLASS + ": Mopping up messages from topic");

            if (_commit)
            {
                System.out.print(" (expecting " + 5 + ")...");
            }
            else
            {
                System.out.print(" (expecting none)...");
            }

            numberMopped=0;
            do
            {
                moppedMessage=topicMessageConsumer.receiveNoWait();
                if (moppedMessage != null)
                {
                    numberMopped++;
                }
            }
            while (moppedMessage != null);
            System.out.println(numberMopped + " message(s) mopped.");

            // Close the QueueConnection to the server
            System.out.println(CLASS + ": Closing connection");
            connection.close();

            // Close the JNDI reference
            System.out.println(CLASS + ": Closing JNDI context");
            ctx.close();
        }
        catch (Exception exp)
        {
            System.err.println(CLASS + ": Caught an Exception: " + exp);
        }
    }
}