summaryrefslogtreecommitdiff
path: root/java/perftests/src/main/java/org/apache/qpid/disttest/jms/QpidQueueCreator.java
blob: ef2cfb6cd46f93ce94ee220bc2a319a9ad820233 (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
/*
 * 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.disttest.jms;

import java.util.List;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.qpid.client.AMQDestination;
import org.apache.qpid.client.AMQSession;
import org.apache.qpid.disttest.DistributedTestException;
import org.apache.qpid.disttest.controller.config.QueueConfig;
import org.apache.qpid.framing.AMQShortString;
import org.apache.qpid.framing.FieldTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QpidQueueCreator implements QueueCreator
{
    private static final Logger LOGGER = LoggerFactory.getLogger(QpidQueueCreator.class);
    private static final FieldTable EMPTY_QUEUE_BIND_ARGUMENTS = new FieldTable();
    private static final String QUEUE_CREATOR_DRAIN_POLL_TIMEOUT = "qpid.disttest.queue.creator.drainPollTime";
    private static int _drainPollTimeout = Integer.getInteger(QUEUE_CREATOR_DRAIN_POLL_TIMEOUT, 500);

    @Override
    public void createQueues(Connection connection, Session session, List<QueueConfig> configs)
    {
        AMQSession<?, ?> amqSession = (AMQSession<?, ?>)session;
        for (QueueConfig queueConfig : configs)
        {
            createQueue(amqSession, queueConfig);
        }
    }

    @Override
    public void deleteQueues(Connection connection, Session session, List<QueueConfig> configs)
    {
        AMQSession<?, ?> amqSession = (AMQSession<?, ?>)session;
        for (QueueConfig queueConfig : configs)
        {
            AMQDestination destination = createAMQDestination(amqSession, queueConfig);

            // drainQueue method is added because deletion of queue with a lot
            // of messages takes time and might cause the timeout exception
            drainQueue(connection, destination);

            deleteQueue(amqSession, destination.getAMQQueueName());
        }
    }

    private AMQDestination createAMQDestination(AMQSession<?, ?> amqSession, QueueConfig queueConfig)
    {
        try
        {
            return (AMQDestination) amqSession.createQueue(queueConfig.getName());
        }
        catch (Exception e)
        {
            throw new DistributedTestException("Failed to create amq destionation object:" + queueConfig, e);
        }
    }

    private long getQueueDepth(AMQSession<?, ?> amqSession, AMQDestination destination)
    {
        try
        {
            long queueDepth = amqSession.getQueueDepth(destination);
            return queueDepth;
        }
        catch (Exception e)
        {
            throw new DistributedTestException("Failed to query queue depth:" + destination, e);
        }
    }

    private void drainQueue(Connection connection, AMQDestination destination)
    {
        Session noAckSession = null;
        try
        {
            LOGGER.debug("About to drain the queue {}", destination.getQueueName());
            noAckSession = connection.createSession(false, org.apache.qpid.jms.Session.NO_ACKNOWLEDGE);
            MessageConsumer messageConsumer = noAckSession.createConsumer(destination);

            long currentQueueDepth = getQueueDepth((AMQSession<?,?>)noAckSession, destination);
            int counter = 0;
            while (currentQueueDepth > 0)
            {
                LOGGER.info("Queue {} has {} message(s)", destination.getQueueName(), currentQueueDepth);

                while(messageConsumer.receive(_drainPollTimeout) != null)
                {
                    counter++;
                }

                currentQueueDepth = getQueueDepth((AMQSession<?,?>)noAckSession, destination);
            }
            LOGGER.info("Drained {} message(s) from queue {} ", counter, destination.getQueueName());
            messageConsumer.close();
        }
        catch (Exception e)
        {
            throw new DistributedTestException("Failed to drain queue:" + destination, e);
        }
        finally
        {
            if (noAckSession != null)
            {
                try
                {
                    noAckSession.close();
                }
                catch (JMSException e)
                {
                    throw new DistributedTestException("Failed to close n/a session:" + noAckSession, e);
                }
            }
        }
    }

    private void createQueue(AMQSession<?, ?> session, QueueConfig queueConfig)
    {
        try
        {
            AMQDestination destination = (AMQDestination) session.createQueue(queueConfig.getName());
            boolean autoDelete = false;
            boolean exclusive = false;
            session.createQueue(destination.getAMQQueueName(), autoDelete,
                    queueConfig.isDurable(), exclusive, queueConfig.getAttributes());
            session.bindQueue(destination.getAMQQueueName(), destination.getRoutingKey(),
                    EMPTY_QUEUE_BIND_ARGUMENTS, destination.getExchangeName(),
                    destination, autoDelete);

            LOGGER.debug("Created queue {}", queueConfig);
        }
        catch (Exception e)
        {
            throw new DistributedTestException("Failed to create queue:" + queueConfig, e);
        }
    }

    private void deleteQueue(AMQSession<?, ?> session, AMQShortString queueName)
    {
        try
        {
            // The Qpid AMQSession API currently makes the #deleteQueue method protected and the
            // raw protocol method public.  This should be changed then we should switch the below to
            // use #deleteQueue.
            session.sendQueueDelete(queueName);
            LOGGER.debug("Deleted queue {}", queueName);
        }
        catch (Exception e)
        {
            throw new DistributedTestException("Failed to delete queue:" + queueName, e);
        }
    }
}