-
Notifications
You must be signed in to change notification settings - Fork 912
/
MultipleProducersPagingTest.java
166 lines (147 loc) · 5.99 KB
/
MultipleProducersPagingTest.java
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
/*
* 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.activemq.artemis.tests.integration.paging;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptorFactory;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.settings.impl.AddressFullMessagePolicy;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.apache.activemq.artemis.utils.ActiveMQThreadFactory;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class MultipleProducersPagingTest extends ActiveMQTestBase {
private static final int CONSUMER_WAIT_TIME_MS = 250;
private static final int PRODUCERS = 5;
private static final long MESSAGES_PER_PRODUCER = 2000;
private static final long TOTAL_MSG = MESSAGES_PER_PRODUCER * PRODUCERS;
private ExecutorService executor;
private CountDownLatch runnersLatch;
private CyclicBarrier barrierLatch;
private AtomicLong msgReceived;
private AtomicLong msgSent;
private final Set<Connection> connections = new HashSet<>();
private ActiveMQServer server;
private ConnectionFactory cf;
private Queue queue;
@Override
@Before
public void setUp() throws Exception {
super.setUp();
executor = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory());
server = createServer(createBasicConfig()
.setPersistenceEnabled(false)
.setAddressSettings(Collections.singletonMap("#", new AddressSettings()
.setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE)
.setPageSizeBytes(50000)
.setMaxSizeBytes(404850)))
.setAcceptorConfigurations(Collections.singleton(new TransportConfiguration(NettyAcceptorFactory.class.getName()))));
server.start();
cf = ActiveMQJMSClient.createConnectionFactory("tcp://127.0.0.1:61616", "cf");
queue = ActiveMQJMSClient.createQueue("simple");
barrierLatch = new CyclicBarrier(PRODUCERS + 1);
runnersLatch = new CountDownLatch(PRODUCERS + 1);
msgReceived = new AtomicLong(0);
msgSent = new AtomicLong(0);
}
@Test
public void testQueue() throws InterruptedException {
executor.execute(new ConsumerRun());
for (int i = 0; i < PRODUCERS; i++) {
executor.execute(new ProducerRun());
}
Assert.assertTrue("must take less than a minute to run", runnersLatch.await(1, TimeUnit.MINUTES));
Assert.assertEquals("number sent", TOTAL_MSG, msgSent.longValue());
Assert.assertEquals("number received", TOTAL_MSG, msgReceived.longValue());
}
private synchronized Session createSession() throws JMSException {
Connection connection = cf.createConnection();
connections.add(connection);
connection.start();
return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
final class ConsumerRun implements Runnable {
@Override
public void run() {
try {
Session session = createSession();
MessageConsumer consumer = session.createConsumer(queue);
barrierLatch.await();
while (true) {
Message msg = consumer.receive(CONSUMER_WAIT_TIME_MS);
if (msg == null)
break;
msgReceived.incrementAndGet();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
runnersLatch.countDown();
}
}
}
final class ProducerRun implements Runnable {
@Override
public void run() {
try {
Session session = createSession();
MessageProducer producer = session.createProducer(queue);
barrierLatch.await();
for (int i = 0; i < MESSAGES_PER_PRODUCER; i++) {
producer.send(session.createTextMessage(this.hashCode() + " counter " + i));
msgSent.incrementAndGet();
}
} catch (Exception cause) {
throw new RuntimeException(cause);
} finally {
runnersLatch.countDown();
}
}
}
@Override
@After
public void tearDown() throws Exception {
executor.shutdown();
for (Connection conn : connections) {
conn.close();
}
connections.clear();
if (server != null)
server.stop();
super.tearDown();
}
}