Search in sources :

Example 1 with ServerConsumerImpl

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) {
        }
    }
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) QueueBinding(org.apache.activemq.artemis.core.postoffice.QueueBinding) ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) QueueBinding(org.apache.activemq.artemis.core.postoffice.QueueBinding) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Bindings(org.apache.activemq.artemis.core.postoffice.Bindings) IOException(java.io.IOException) ServerConsumerImpl(org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Consumer(org.apache.activemq.artemis.core.server.Consumer) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Collection(java.util.Collection) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 2 with ServerConsumerImpl

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);
}
Also used : ServerConsumerImpl(org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ConsumerControl(org.apache.activemq.command.ConsumerControl) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Example 3 with ServerConsumerImpl

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);
    }
}
Also used : ServerConsumerImpl(org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl) ProtonServerSenderContext(org.apache.activemq.artemis.protocol.amqp.proton.ProtonServerSenderContext)

Aggregations

ServerConsumerImpl (org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl)3 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 IOException (java.io.IOException)1 Collection (java.util.Collection)1 ActiveMQQueueExistsException (org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException)1 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)1 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)1 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)1 ClientConsumerInternal (org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal)1 Binding (org.apache.activemq.artemis.core.postoffice.Binding)1 Bindings (org.apache.activemq.artemis.core.postoffice.Bindings)1 QueueBinding (org.apache.activemq.artemis.core.postoffice.QueueBinding)1 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)1 Consumer (org.apache.activemq.artemis.core.server.Consumer)1 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)1 ProtonServerSenderContext (org.apache.activemq.artemis.protocol.amqp.proton.ProtonServerSenderContext)1 ConsumerControl (org.apache.activemq.command.ConsumerControl)1