summaryrefslogtreecommitdiff
path: root/qpid/java/perftests/src/main/java/org/apache/qpid/topic/Listener.java
blob: 6dcea42bfee3db0c539718baf015ace345532cb2 (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
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
/*
 *
 * 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.topic;

import java.util.Random;

import javax.jms.*;

import org.apache.log4j.Logger;
import org.apache.log4j.NDC;

import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQQueue;
import org.apache.qpid.client.AMQSession;
import org.apache.qpid.client.AMQTopic;
import org.apache.qpid.exchange.ExchangeDefaults;

/**
 * This class has not kept up to date with the topic_listener in the cpp tests. It should provide identical behaviour for
 * cross testing the java and cpp clients.
 *
 * <p/>How the cpp topic_publisher operates:
 * It publishes text messages to the default topic exchange, on virtual host "/test", on the topic "topic_control", for
 * the specified number of test messages to be sent.
 * It publishes a report request message (on same topic), with the header text field "TYPE", value "REPORT_REQUEST",
 * optionally within a transaction, and waits for the specified number of consumers to reply to this request. The
 * listeners should reply to this message on a queue named "response", on virtual host "/test", with some sort of message
 * about the number of messages received and how long it took, although the publisher never looks at the message content.
 * The publisher then send a message (on the same topic), with the header text field "TYPE", value "TERMINATION_REQUEST",
 * which the listener should close its connection and terminate upon receipt of.
 *
 * @todo I've added lots of field table types in the report message, just to check if the other end can decode them
 *       correctly. Not really the right place to test this, so remove them from
 *       {@link #createReportResponseMessage(String)} once a better test exists.
 */
public class Listener implements MessageListener
{
    private static Logger log = Logger.getLogger(Listener.class);

    public static final String CONTROL_TOPIC = "topic_control";
    public static final String RESPONSE_QUEUE = "response";

    private final Topic _topic;
    //private final Topic _control;

    private final Queue _response;

    /** Holds the connection to listen on. */
    private final Connection _connection;

    /** Holds the producer to send control messages on. */
    private final MessageProducer _controller;

    /** Holds the JMS session. */
    private final javax.jms.Session _session;

    /** Holds a flag to indicate that a timer has begun on the first message. Reset when report is sent. */
    private boolean init;

    /** Holds the count of messages received by this listener. */
    private int count;

    /** Used to hold the start time of the first message. */
    private long start;
    private static String clientId;

    Listener(Connection connection, int ackMode, String name) throws Exception
    {
        log.debug("Listener(Connection connection = " + connection + ", int ackMode = " + ackMode + ", String name = " + name
                  + "): called");

        _connection = connection;
        _session = connection.createSession(false, ackMode);

        if (_session instanceof AMQSession)
        {
            _topic = new AMQTopic(ExchangeDefaults.TOPIC_EXCHANGE_NAME, CONTROL_TOPIC);
            //_control = new AMQTopic(CONTROL_TOPIC);
            _response = new AMQQueue(ExchangeDefaults.DIRECT_EXCHANGE_NAME, RESPONSE_QUEUE);
        }
        else
        {
            _topic = _session.createTopic(CONTROL_TOPIC);
            //_control = _session.createTopic(CONTROL_TOPIC);
            _response = _session.createQueue(RESPONSE_QUEUE);
        }

        //register for events
        if (name == null)
        {
            log.debug("Calling _factory.createTopicConsumer().setMessageListener(this)");
            createTopicConsumer().setMessageListener(this);
        }
        else
        {
            log.debug("Calling createDurableTopicConsumer(name).setMessageListener(this)");
            createDurableTopicConsumer(name).setMessageListener(this);
        }

        _connection.start();

        _controller = createControlPublisher();
        System.out.println("Waiting for messages " + Config.getAckModeDescription(ackMode)
                           +
                           ((name == null)
                            ? "" : (" (subscribed with name " + name + " and client id " + connection.getClientID() + ")"))
                           + "...");
    }

    public static void main(String[] argv) throws Exception
    {
        clientId = "Listener-" + System.currentTimeMillis();

        NDC.push(clientId);

        Config config = new Config();
        config.setOptions(argv);

        //Connection con = config.createConnection();
        Connection con =
            new AMQConnection("amqp://guest:guest@testid/test?brokerlist='" + config.getHost() + ":" + config.getPort()
                              + "'");

        if (config.getClientId() != null)
        {
            con.setClientID(config.getClientId());
        }

        new Listener(con, config.getAckMode(), config.getSubscriptionId());

        NDC.pop();
        NDC.remove();
    }

    /**
     * Checks whether or not a text field on a message has the specified value.
     *
     * @param m         The message to check.
     * @param fieldName The name of the field to check.
     * @param value     The expected value of the field to compare with.
     *
     * @return <tt>true</tt>If the specified field has the specified value, <tt>fals</tt> otherwise.
     *
     * @throws JMSException Any JMSExceptions are allowed to fall through.
     */
    private static boolean checkTextField(Message m, String fieldName, String value) throws JMSException
    {
        log.debug("private static boolean checkTextField(Message m = " + m + ", String fieldName = " + fieldName
                  + ", String value = " + value + "): called");

        String comp = m.getStringProperty(fieldName);
        log.debug("comp = " + comp);

        boolean result = (comp != null) && comp.equals(value);
        log.debug("result = " + result);

        return result;
    }

    public void onMessage(Message message)
    {
        NDC.push(clientId);

        log.debug("public void onMessage(Message message = " + message + "): called");

        if (!init)
        {
            start = System.nanoTime() / 1000000;
            count = 0;
            init = true;
        }

        try
        {
            if (isShutdown(message))
            {
                log.debug("Got a shutdown message.");
                shutdown();
            }
            else if (isReport(message))
            {
                log.debug("Got a report request message.");

                // Send the report.
                report();
                init = false;
            }
        }
        catch (JMSException e)
        {
            log.warn("There was a JMSException during onMessage.", e);
        }
        finally
        {
            NDC.pop();
        }
    }

    Message createReportResponseMessage(String msg) throws JMSException
    {
        Message message = _session.createTextMessage(msg);

        // Shove some more field table type in the message just to see if the other end can handle it.
        message.setBooleanProperty("BOOLEAN", true);
        message.setByteProperty("BYTE", (byte) 5);
        message.setDoubleProperty("DOUBLE", Math.PI);
        message.setFloatProperty("FLOAT", 1.0f);
        message.setIntProperty("INT", 1);
        message.setShortProperty("SHORT", (short) 1);
        message.setLongProperty("LONG", (long) 1827361278);
        message.setStringProperty("STRING", "hello");

        return message;
    }

    boolean isShutdown(Message m) throws JMSException
    {
        boolean result = checkTextField(m, "TYPE", "TERMINATION_REQUEST");

        //log.debug("isShutdown = " + result);

        return result;
    }

    boolean isReport(Message m) throws JMSException
    {
        boolean result = checkTextField(m, "TYPE", "REPORT_REQUEST");

        //log.debug("isReport = " + result);

        return result;
    }

    MessageConsumer createTopicConsumer() throws Exception
    {
        return _session.createConsumer(_topic);
    }

    MessageConsumer createDurableTopicConsumer(String name) throws Exception
    {
        return _session.createDurableSubscriber(_topic, name);
    }

    MessageProducer createControlPublisher() throws Exception
    {
        return _session.createProducer(_response);
    }

    private void shutdown()
    {
        try
        {
            _session.close();
            _connection.stop();
            _connection.close();
        }
        catch (Exception e)
        {
            e.printStackTrace(System.out);
        }
    }

    private void report()
    {
        log.debug("private void report(): called");

        try
        {
            String msg = getReport();
            _controller.send(createReportResponseMessage(msg));
            log.debug("Sent report: " + msg);
        }
        catch (Exception e)
        {
            e.printStackTrace(System.out);
        }
    }

    private String getReport()
    {
        long time = ((System.nanoTime() / 1000000) - start);

        return "Received " + count + " in " + time + "ms";
    }
}