Search in sources :

Example 21 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class FailoverOnFlowControlTest method testOverflowSend.

@Test
public void testOverflowSend() throws Exception {
    ServerLocator locator = getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(300).setProducerWindowSize(1000).setRetryInterval(100);
    final ArrayList<ClientSession> sessionList = new ArrayList<>();
    Interceptor interceptorClient = new Interceptor() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            log.debug("Intercept..." + packet.getClass().getName());
            if (packet instanceof SessionProducerCreditsMessage) {
                SessionProducerCreditsMessage credit = (SessionProducerCreditsMessage) packet;
                log.debug("Credits: " + credit.getCredits());
                if (count.incrementAndGet() == 2) {
                    log.debug("### crashing server");
                    try {
                        InVMConnection.setFlushEnabled(false);
                        crash(false, sessionList.get(0));
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        InVMConnection.setFlushEnabled(true);
                    }
                    return false;
                }
            }
            return true;
        }
    };
    locator.addIncomingInterceptor(interceptorClient);
    ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 2);
    ClientSession session = sf.createSession(true, true);
    sessionList.add(session);
    session.createQueue(ADDRESS, ADDRESS, null, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    final int numMessages = 10;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[5000]);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    session.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) SessionProducerCreditsMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionProducerCreditsMessage) ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ArrayList(java.util.ArrayList) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 22 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class FailoverTest method testTimeoutOnFailoverConsume.

// https://issues.jboss.org/browse/HORNETQ-685
@Test(timeout = 120000)
public void testTimeoutOnFailoverConsume() throws Exception {
    locator.setCallTimeout(5000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setAckBatchSize(0).setBlockOnAcknowledge(true).setReconnectAttempts(300).setRetryInterval(100).setAckBatchSize(0);
    if (nodeManager instanceof InVMNodeManager) {
        ((InVMNodeManager) nodeManager).failoverPause = 5000L;
    }
    ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
    final ClientSession session = createSession(sf1, true, true);
    session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
    final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    for (int i = 0; i < 500; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(1);
    final ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    final Map<Integer, ClientMessage> received = new HashMap<>();
    consumer.setMessageHandler(new MessageHandler() {

        @Override
        public void onMessage(ClientMessage message) {
            Integer counter = message.getIntProperty("counter");
            received.put(counter, message);
            try {
                log.debug("acking message = id = " + message.getMessageID() + ", counter = " + message.getIntProperty("counter"));
                message.acknowledge();
            } catch (ActiveMQException e) {
                e.printStackTrace();
                return;
            }
            log.debug("Acked counter = " + counter);
            if (counter.equals(10)) {
                latch.countDown();
            }
            if (received.size() == 500) {
                endLatch.countDown();
            }
        }
    });
    latch.await(10, TimeUnit.SECONDS);
    log.info("crashing session");
    crash(session);
    endLatch.await(60, TimeUnit.SECONDS);
    Assert.assertTrue("received only " + received.size(), received.size() == 500);
    session.close();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) HashMap(java.util.HashMap) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 23 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class MultipleBackupsFailoverTestBase method createSessionFactoryAndWaitForTopology.

protected ClientSessionFactoryInternal createSessionFactoryAndWaitForTopology(ServerLocator locator, int topologyMembers, ActiveMQServer server) throws Exception {
    ClientSessionFactoryInternal sf;
    CountDownLatch countDownLatch = new CountDownLatch(topologyMembers);
    FailoverTestBase.LatchClusterTopologyListener topListener = new FailoverTestBase.LatchClusterTopologyListener(countDownLatch);
    locator.addClusterTopologyListener(topListener);
    sf = (ClientSessionFactoryInternal) locator.createSessionFactory();
    addSessionFactory(sf);
    boolean ok = countDownLatch.await(5, TimeUnit.SECONDS);
    locator.removeClusterTopologyListener(topListener);
    if (!ok) {
        if (server != null) {
            log.info("failed topology, Topology on server = " + server.getClusterManager().describe());
        }
    }
    Assert.assertTrue("expected " + topologyMembers + " members", ok);
    return sf;
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class ActiveMQTestBase method waitForRemoteBackup.

/**
 * @param sessionFactoryP
 * @param seconds
 * @param waitForSync
 * @param backup
 */
public static final void waitForRemoteBackup(ClientSessionFactory sessionFactoryP, int seconds, boolean waitForSync, final ActiveMQServer backup) {
    ClientSessionFactoryInternal sessionFactory = (ClientSessionFactoryInternal) sessionFactoryP;
    final ActiveMQServerImpl actualServer = (ActiveMQServerImpl) backup;
    final long toWait = seconds * 1000;
    final long time = System.currentTimeMillis();
    int loop = 0;
    while (true) {
        Activation activation = actualServer.getActivation();
        boolean isReplicated = !backup.getHAPolicy().isSharedStore();
        boolean isRemoteUpToDate = true;
        if (isReplicated) {
            if (activation instanceof SharedNothingBackupActivation) {
                isRemoteUpToDate = backup.isReplicaSync();
            } else {
                // we may have already failed over and changed the Activation
                if (actualServer.isStarted()) {
                    // let it fail a few time to have time to start stopping in the case of waiting to failback
                    isRemoteUpToDate = loop++ > 10;
                } else {
                    // we could be waiting to failback or restart if the server is stopping
                    isRemoteUpToDate = false;
                }
            }
        }
        if ((sessionFactory == null || sessionFactory.getBackupConnector() != null) && (isRemoteUpToDate || !waitForSync) && (!waitForSync || actualServer.getBackupManager() != null && actualServer.getBackupManager().isBackupAnnounced())) {
            break;
        }
        if (System.currentTimeMillis() > (time + toWait)) {
            fail("backup started? (" + actualServer.isStarted() + "). Finished synchronizing (" + (activation) + "). SessionFactory!=null ? " + (sessionFactory != null) + " || sessionFactory.getBackupConnector()==" + (sessionFactory != null ? sessionFactory.getBackupConnector() : "not-applicable"));
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            fail(e.getMessage());
        }
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) Activation(org.apache.activemq.artemis.core.server.impl.Activation) SharedNothingBackupActivation(org.apache.activemq.artemis.core.server.impl.SharedNothingBackupActivation) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) SharedNothingBackupActivation(org.apache.activemq.artemis.core.server.impl.SharedNothingBackupActivation)

Example 25 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class LiveOnlyActivation method connectToScaleDownTarget.

public void connectToScaleDownTarget(ScaleDownPolicy scaleDownPolicy) {
    try {
        scaleDownServerLocator = ScaleDownPolicy.getScaleDownConnector(scaleDownPolicy, activeMQServer);
        // use a Node Locator to connect to the cluster
        scaleDownServerLocator.setProtocolManagerFactory(ActiveMQServerSideProtocolManagerFactory.getInstance(scaleDownServerLocator));
        LiveNodeLocator nodeLocator = scaleDownPolicy.getGroupName() == null ? new AnyLiveNodeLocatorForScaleDown(activeMQServer) : new NamedLiveNodeLocatorForScaleDown(scaleDownPolicy.getGroupName(), activeMQServer);
        scaleDownServerLocator.addClusterTopologyListener(nodeLocator);
        nodeLocator.connectToCluster(scaleDownServerLocator);
        // a timeout is necessary here in case we use a NamedLiveNodeLocatorForScaleDown and there's no matching node in the cluster
        // should the timeout be configurable?
        nodeLocator.locateNode(ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT);
        ClientSessionFactoryInternal clientSessionFactory = null;
        while (clientSessionFactory == null) {
            Pair<TransportConfiguration, TransportConfiguration> possibleLive = null;
            try {
                possibleLive = nodeLocator.getLiveConfiguration();
                if (// we've tried every connector
                possibleLive == null)
                    break;
                clientSessionFactory = (ClientSessionFactoryInternal) scaleDownServerLocator.createSessionFactory(possibleLive.getA(), 0, false);
            } catch (Exception e) {
                logger.trace("Failed to connect to " + possibleLive.getA());
                nodeLocator.notifyRegistrationFailed(false);
                if (clientSessionFactory != null) {
                    clientSessionFactory.close();
                }
                clientSessionFactory = null;
            // should I try the backup (i.e. getB()) from possibleLive?
            }
        }
        if (clientSessionFactory != null) {
            scaleDownClientSessionFactory = clientSessionFactory;
        } else {
            throw new ActiveMQException("Unable to connect to server for scale-down");
        }
    } catch (Exception e) {
        ActiveMQServerLogger.LOGGER.failedToScaleDown(e);
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) LiveNodeLocator(org.apache.activemq.artemis.core.server.LiveNodeLocator) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Aggregations

ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)43 Test (org.junit.Test)30 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)28 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)20 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)19 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)17 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)16 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)12 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)12 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)11 InVMNodeManager (org.apache.activemq.artemis.core.server.impl.InVMNodeManager)8 ActiveMQObjectClosedException (org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException)6 SessionFailureListener (org.apache.activemq.artemis.api.core.client.SessionFailureListener)6 ServerLocatorInternal (org.apache.activemq.artemis.core.client.impl.ServerLocatorInternal)6 XAException (javax.transaction.xa.XAException)5 ActiveMQDuplicateIdException (org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException)4 ActiveMQTransactionOutcomeUnknownException (org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException)4 ActiveMQTransactionRolledBackException (org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException)4