summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/test/framework/MessageMonitor.java
blob: 5265c0416f465c55cd009484a67d944aec0e9f3a (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
/*
 *
 * 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.test.framework;

import org.apache.log4j.Logger;

import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * MessageMonitor is used to record information about messages received. This will provide methods to check various
 * properties, such as the type, number and content of messages received in order to verify the correct behaviour of
 * tests.
 *
 * <p/><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Count incoming messages.
 * <tr><td> Record time ellapsed since the arrival of the first message.
 * <tr><td> Reset all counts and timings.
 * </table>
 */
public class MessageMonitor implements MessageListener
{
    /** Used for debugging. */
    private final Logger log = Logger.getLogger(MessageMonitor.class);

    /** Holds the count of messages received since the last query. */
    protected AtomicInteger numMessages = new AtomicInteger();

    /** Holds the time of arrival of the first message. */
    protected Long firstMessageTime = null;

    /**
     * Handles received messages. Does Nothing.
     *
     * @param message The message. Ignored.
     */
    public void onMessage(Message message)
    {
        // log.debug("public void onMessage(Message message): called");

        numMessages.getAndIncrement();
    }

    /**
     * Gets the count of messages.
     *
     * @return The count of messages.
     */
    public int getNumMessage()
    {
        if (firstMessageTime == null)
        {
            firstMessageTime = System.nanoTime();
        }

        return numMessages.get();
    }

    /**
     * Gets the time elapsed since the first message arrived, in nanos, or zero if no messages have arrived yet.
     *
     * @return The time elapsed since the first message arrived, in nanos, or zero if no messages have arrived yet.
     */
    public long getTime()
    {
        if (firstMessageTime != null)
        {
            return System.nanoTime() - firstMessageTime;
        }
        else
        {
            return 0L;
        }
    }

    /**
     * Resets the message count and timer to zero.
     */
    public void reset()
    {
        numMessages.set(0);
        firstMessageTime = null;
    }
}