use of org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl in project activemq-artemis by apache.
the class ConsumerWindowSizeTest method testNoWindowRoundRobin.
private void testNoWindowRoundRobin(final boolean largeMessages) throws Exception {
ActiveMQServer server = createServer(false, isNetty());
ClientSession sessionA = null;
ClientSession sessionB = null;
try {
final int numberOfMessages = 100;
server.start();
locator.setConsumerWindowSize(-1);
if (largeMessages) {
locator.setMinLargeMessageSize(100);
}
ClientSessionFactory sf = createSessionFactory(locator);
sessionA = sf.createSession(false, true, true);
SimpleString ADDRESS = new SimpleString("some-queue");
sessionA.createQueue(ADDRESS, ADDRESS, true);
sessionB = sf.createSession(false, true, true);
sessionA.start();
sessionB.start();
ClientConsumerInternal consA = (ClientConsumerInternal) sessionA.createConsumer(ADDRESS);
ClientConsumerInternal consB = (ClientConsumerInternal) sessionB.createConsumer(ADDRESS);
{
// We can only guarantee round robing with WindowSize = -1, after the ServerConsumer object received
// SessionConsumerFlowCreditMessage(-1)
// Since that is done asynchronously we verify that the information was received before we proceed on
// sending messages or else the distribution won't be
// even as expected by the test
Bindings bindings = server.getPostOffice().getBindingsForAddress(ADDRESS);
Assert.assertEquals(1, bindings.getBindings().size());
for (Binding binding : bindings.getBindings()) {
Collection<Consumer> consumers = ((QueueBinding) binding).getQueue().getConsumers();
for (Consumer consumer : consumers) {
ServerConsumerImpl consumerImpl = (ServerConsumerImpl) consumer;
long timeout = System.currentTimeMillis() + 5000;
while (timeout > System.currentTimeMillis() && consumerImpl.getAvailableCredits() != null) {
Thread.sleep(10);
}
Assert.assertNull(consumerImpl.getAvailableCredits());
}
}
}
ClientProducer prod = sessionA.createProducer(ADDRESS);
for (int i = 0; i < numberOfMessages; i++) {
ClientMessage msg = createTextMessage(sessionA, "Msg" + i);
if (largeMessages) {
msg.getBodyBuffer().writeBytes(new byte[600]);
}
prod.send(msg);
}
long timeout = System.currentTimeMillis() + TIMEOUT * 1000;
boolean foundA = false;
boolean foundB = false;
do {
foundA = consA.getBufferSize() == numberOfMessages / 2;
foundB = consB.getBufferSize() == numberOfMessages / 2;
Thread.sleep(10);
} while ((!foundA || !foundB) && System.currentTimeMillis() < timeout);
Assert.assertTrue("ConsumerA didn't receive the expected number of messages on buffer (consA=" + consA.getBufferSize() + ", consB=" + consB.getBufferSize() + ") foundA = " + foundA + " foundB = " + foundB, foundA);
Assert.assertTrue("ConsumerB didn't receive the expected number of messages on buffer (consA=" + consA.getBufferSize() + ", consB=" + consB.getBufferSize() + ") foundA = " + foundA + " foundB = " + foundB, foundB);
} finally {
try {
if (sessionA != null) {
sessionA.close();
}
if (sessionB != null) {
sessionB.close();
}
} catch (Exception ignored) {
}
}
}
use of org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl in project activemq-artemis by apache.
the class AMQConsumer method init.
public void init(SlowConsumerDetectionListener slowConsumerDetectionListener, long nativeId) throws Exception {
SimpleString selector = info.getSelector() == null ? null : new SimpleString(info.getSelector());
boolean preAck = false;
if (info.isNoLocal()) {
if (!AdvisorySupport.isAdvisoryTopic(openwireDestination)) {
// tell the connection to add the property
this.session.getConnection().setNoLocal(true);
} else {
preAck = true;
}
String id = info.getClientId() != null ? info.getClientId() : this.getId().getConnectionId();
String noLocalSelector = MessageUtil.CONNECTION_ID_PROPERTY_NAME.toString() + "<>'" + id + "'";
if (selector == null) {
selector = new SimpleString(noLocalSelector);
} else {
selector = new SimpleString(info.getSelector() + " AND " + noLocalSelector);
}
}
SimpleString destinationName = new SimpleString(session.convertWildcard(openwireDestination.getPhysicalName()));
if (openwireDestination.isTopic()) {
SimpleString queueName = createTopicSubscription(info.isDurable(), info.getClientId(), destinationName.toString(), info.getSubscriptionName(), selector, destinationName);
serverConsumer = session.getCoreSession().createConsumer(nativeId, queueName, null, info.isBrowser(), false, -1);
serverConsumer.setlowConsumerDetection(slowConsumerDetectionListener);
// only advisory topic consumers need this.
((ServerConsumerImpl) serverConsumer).setPreAcknowledge(preAck);
} else {
try {
session.getCoreServer().createQueue(destinationName, RoutingType.ANYCAST, destinationName, null, true, false);
} catch (ActiveMQQueueExistsException e) {
// ignore
}
serverConsumer = session.getCoreSession().createConsumer(nativeId, destinationName, selector, info.isBrowser(), false, -1);
serverConsumer.setlowConsumerDetection(slowConsumerDetectionListener);
AddressSettings addrSettings = session.getCoreServer().getAddressSettingsRepository().getMatch(destinationName.toString());
if (addrSettings != null) {
// see PolicyEntry
if (info.getPrefetchSize() != 0 && addrSettings.getQueuePrefetch() == 0) {
// sends back a ConsumerControl
ConsumerControl cc = new ConsumerControl();
cc.setConsumerId(info.getConsumerId());
cc.setPrefetch(0);
session.getConnection().dispatch(cc);
}
}
}
serverConsumer.setProtocolData(this);
}
use of org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl in project activemq-artemis by apache.
the class AMQPSessionCallback method onFlowConsumer.
public void onFlowConsumer(Object consumer, int credits, final boolean drain) {
ServerConsumerImpl serverConsumer = (ServerConsumerImpl) consumer;
if (drain) {
// If the draining is already running, then don't do anything
if (draining.compareAndSet(false, true)) {
final ProtonServerSenderContext plugSender = (ProtonServerSenderContext) serverConsumer.getProtocolContext();
serverConsumer.forceDelivery(1, new Runnable() {
@Override
public void run() {
try {
plugSender.reportDrained();
} finally {
draining.set(false);
}
}
});
}
} else {
serverConsumer.receiveCredits(-1);
}
}
Aggregations