summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/management/jmx/ManagedConnectionMBeanTest.java
blob: a258e4267df3c4b6fb6df8cc2a31b8c92c713b07 (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
/*
 * 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.management.jmx;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.management.JMException;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.TabularData;

import org.apache.qpid.client.AMQSession;
import org.apache.qpid.management.common.mbeans.ManagedConnection;
import org.apache.qpid.test.utils.JMXTestUtils;
import org.apache.qpid.test.utils.QpidBrokerTestCase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ManagedConnectionMBeanTest extends QpidBrokerTestCase
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ManagedConnectionMBeanTest.class);

    /**
     * JMX helper.
     */
    private JMXTestUtils _jmxUtils;
    private Connection _connection;

    public void setUp() throws Exception
    {
        _jmxUtils = new JMXTestUtils(this);
        _jmxUtils.setUp();
        super.setUp();
        _jmxUtils.open();
        _connection = getConnection();
    }

    public void tearDown() throws Exception
    {
        if (_jmxUtils != null)
        {
            _jmxUtils.close();
        }
        super.tearDown();
    }

    public void testChannels() throws Exception
    {
        final String queueName = getTestQueueName();

        final Session session = _connection.createSession(true, Session.SESSION_TRANSACTED);
        final Destination destination = session.createQueue(queueName);
        final MessageConsumer consumer = session.createConsumer(destination);

        final int numberOfMessages = 2;
        sendMessage(session, destination, numberOfMessages);
        _connection.start();

        for (int i = 0; i < numberOfMessages; i++)
        {
            final Message m = consumer.receive(1000l);
            assertNotNull("Message " + i + " is not received", m);
        }

        List<ManagedConnection> connections = _jmxUtils.getManagedConnections("test");
        assertNotNull("Connection MBean is not found", connections);
        assertEquals("Unexpected number of connection mbeans", 1, connections.size());
        final ManagedConnection mBean = connections.get(0);
        assertNotNull("Connection MBean is null", mBean);

        TabularData channelsData = mBean.channels();
        assertNotNull("Channels data are null", channelsData);
        assertEquals("Unexpected number of rows in channel table", 1, channelsData.size());

        final Iterator<CompositeDataSupport> rowItr = (Iterator<CompositeDataSupport>) channelsData.values().iterator();
        final CompositeDataSupport row = rowItr.next();
        Number unackCount = (Number) row.get(ManagedConnection.UNACKED_COUNT);
        final Boolean transactional = (Boolean) row.get(ManagedConnection.TRANSACTIONAL);
        final Boolean flowBlocked = (Boolean) row.get(ManagedConnection.FLOW_BLOCKED);
        assertNotNull("Channel should have unacknowledged messages", unackCount);
        assertEquals("Unexpected number of unacknowledged messages", 2, unackCount.intValue());
        assertNotNull("Channel should have transaction flag", transactional);
        assertTrue("Unexpected transaction flag", transactional);
        assertNotNull("Channel should have flow blocked flag", flowBlocked);
        assertFalse("Unexpected value of flow blocked flag", flowBlocked);

        final Date initialLastIOTime = mBean.getLastIoTime();
        session.commit();
        assertTrue("Last IO time should have been updated", mBean.getLastIoTime().after(initialLastIOTime));

        channelsData = mBean.channels();
        assertNotNull("Channels data are null", channelsData);
        assertEquals("Unexpected number of rows in channel table", 1, channelsData.size());

        final Iterator<CompositeDataSupport> rowItr2 = (Iterator<CompositeDataSupport>) channelsData.values().iterator();
        final CompositeDataSupport row2 = rowItr2.next();
        unackCount = (Number) row2.get(ManagedConnection.UNACKED_COUNT);
        assertNotNull("Channel should have unacknowledged messages", unackCount);
        assertEquals("Unexpected number of anacknowledged messages", 0, unackCount.intValue());

        _connection.close();

        LOGGER.debug("Querying JMX for number of open connections");
        connections = _jmxUtils.getManagedConnections("test");
        assertNotNull("Connection MBean is not found", connections);
        assertEquals("Unexpected number of connection mbeans after connection closed", 0, connections.size());
    }

    public void testCommit() throws Exception
    {
        final String queueName = getTestQueueName();

        final Session consumerSession = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Session producerSession = _connection.createSession(true, Session.SESSION_TRANSACTED);
        final Destination destination = producerSession.createQueue(queueName);
        final MessageConsumer consumer = consumerSession.createConsumer(destination);
        final MessageProducer producer = producerSession.createProducer(destination);

        _connection.start();

        List<ManagedConnection> connections = _jmxUtils.getManagedConnections("test");
        assertNotNull("Connection MBean is not found", connections);
        assertEquals("Unexpected number of connection mbeans", 1, connections.size());
        final ManagedConnection mBean = connections.get(0);
        assertNotNull("Connection MBean is null", mBean);

        final int numberOfMessages = 2;
        for (int i = 0; i < numberOfMessages; i++)
        {
            producer.send(producerSession.createTextMessage("Test " + i));
        }

        // sync to make sure that messages are received on the broker
        // before we commit via JMX
        ((AMQSession<?, ?>) producerSession).sync();

        Message m = consumer.receive(500l);
        assertNull("Unexpected message received", m);

        Number channelId = getFirstTransactedChannelId(mBean, 2);
        mBean.commitTransactions(channelId.intValue());

        for (int i = 0; i < numberOfMessages; i++)
        {
            m = consumer.receive(1000l);
            assertNotNull("Message " + i + " is not received", m);
            assertEquals("Unexpected message received at " + i, "Test " + i, ((TextMessage) m).getText());
        }
        producerSession.commit();
        m = consumer.receive(500l);
        assertNull("Unexpected message received", m);
    }

    protected Number getFirstTransactedChannelId(final ManagedConnection mBean, int channelNumber) throws IOException, JMException
    {
        TabularData channelsData = mBean.channels();
        assertNotNull("Channels data are null", channelsData);
        assertEquals("Unexpected number of rows in channel table", channelNumber, channelsData.size());
        final Iterator<CompositeDataSupport> rowItr = (Iterator<CompositeDataSupport>) channelsData.values().iterator();
        while (rowItr.hasNext())
        {
            final CompositeDataSupport row = rowItr.next();
            Boolean transacted = (Boolean) row.get(ManagedConnection.TRANSACTIONAL);
            if (transacted.booleanValue())
            {
                return (Number) row.get(ManagedConnection.CHAN_ID);
            }
        }
        return null;
    }

    public void testRollback() throws Exception
    {
        final String queueName = getTestQueueName();

        final Session consumerSession = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Session producerSession = _connection.createSession(true, Session.SESSION_TRANSACTED);
        final Destination destination = producerSession.createQueue(queueName);
        final MessageConsumer consumer = consumerSession.createConsumer(destination);
        final MessageProducer producer = producerSession.createProducer(destination);

        List<ManagedConnection> connections = _jmxUtils.getManagedConnections("test");
        assertNotNull("Connection MBean is not found", connections);
        assertEquals("Unexpected number of connection mbeans", 1, connections.size());
        final ManagedConnection mBean = connections.get(0);
        assertNotNull("Connection MBean is null", mBean);

        final int numberOfMessages = 2;
        for (int i = 0; i < numberOfMessages; i++)
        {
            producer.send(producerSession.createTextMessage("Test " + i));
        }

        // sync to make sure that messages are received on the broker
        // before we rollback via JMX
        ((AMQSession<?, ?>) producerSession).sync();

        Number channelId = getFirstTransactedChannelId(mBean, 2);
        mBean.rollbackTransactions(channelId.intValue());

        Message m = consumer.receive(1000l);
        assertNull("Unexpected message received: " + String.valueOf(m), m);

        producerSession.commit();

        _connection.start();
        m = consumer.receive(1000l);
        assertNull("Unexpected message received after commit " + String.valueOf(m), m);
    }

    public void testAuthorisedId() throws Exception
    {
        List<ManagedConnection> connections = _jmxUtils.getManagedConnections("test");
        assertNotNull("Connection MBean is not found", connections);
        assertEquals("Unexpected number of connection mbeans", 1, connections.size());
        final ManagedConnection mBean = connections.get(0);
        assertNotNull("Connection MBean is null", mBean);
        assertEquals("Unexpected authorized id", "guest", mBean.getAuthorizedId());
    }
}