Search in sources :

Example 41 with ClientSessionFactoryInternal

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

the class ActiveMQMessageHandlerTest method testServerShutdownAndReconnect.

@Test
public void testServerShutdownAndReconnect() throws Exception {
    ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
    qResourceAdapter.setReconnectAttempts(-1);
    qResourceAdapter.setCallTimeout(500L);
    qResourceAdapter.setRetryInterval(500L);
    MyBootstrapContext ctx = new MyBootstrapContext();
    qResourceAdapter.start(ctx);
    // This is just to register a listener
    final CountDownLatch failedLatch = new CountDownLatch(1);
    ClientSessionFactoryInternal factoryListener = (ClientSessionFactoryInternal) qResourceAdapter.getDefaultActiveMQConnectionFactory().getServerLocator().createSessionFactory();
    factoryListener.addFailureListener(new SessionFailureListener() {

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver) {
        }

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {
            connectionFailed(exception, failedOver);
        }

        @Override
        public void beforeReconnect(ActiveMQException exception) {
            failedLatch.countDown();
        }
    });
    ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Queue");
    spec.setDestination(MDBQUEUE);
    CountDownLatch latch = new CountDownLatch(1);
    DummyMessageEndpoint endpoint = new DummyMessageEndpoint(latch);
    DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, false);
    qResourceAdapter.endpointActivation(endpointFactory, spec);
    ClientSession session = locator.createSessionFactory().createSession();
    ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED);
    ClientMessage message = session.createMessage(true);
    message.getBodyBuffer().writeString("teststring");
    clientProducer.send(message);
    session.close();
    latch.await(5, TimeUnit.SECONDS);
    assertNotNull(endpoint.lastMessage);
    assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "teststring");
    server.stop();
    assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
    qResourceAdapter.endpointDeactivation(endpointFactory, spec);
    qResourceAdapter.stop();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) ActiveMQActivationSpec(org.apache.activemq.artemis.ra.inflow.ActiveMQActivationSpec) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 42 with ClientSessionFactoryInternal

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

the class SharedNothingLiveActivation method isNodeIdUsed.

/**
 * Determines whether there is another server already running with this server's nodeID.
 * <p>
 * This can happen in case of a successful fail-over followed by the live's restart
 * (attempting a fail-back).
 *
 * @throws Exception
 */
private boolean isNodeIdUsed() throws Exception {
    if (activeMQServer.getConfiguration().getClusterConfigurations().isEmpty())
        return false;
    SimpleString nodeId0;
    try {
        nodeId0 = activeMQServer.getNodeManager().readNodeId();
    } catch (ActiveMQIllegalStateException e) {
        nodeId0 = null;
    }
    ClusterConnectionConfiguration config = ConfigurationUtils.getReplicationClusterConfiguration(activeMQServer.getConfiguration(), replicatedPolicy.getClusterName());
    NodeIdListener listener = new NodeIdListener(nodeId0, activeMQServer.getConfiguration().getClusterUser(), activeMQServer.getConfiguration().getClusterPassword());
    try (ServerLocatorInternal locator = getLocator(config)) {
        locator.addClusterTopologyListener(listener);
        locator.setReconnectAttempts(0);
        try (ClientSessionFactoryInternal factory = locator.connectNoWarnings()) {
            // Just try connecting
            listener.latch.await(5, TimeUnit.SECONDS);
        } catch (Exception notConnected) {
            return false;
        }
        return listener.isNodePresent;
    }
}
Also used : ClusterConnectionConfiguration(org.apache.activemq.artemis.core.config.ClusterConnectionConfiguration) ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ServerLocatorInternal(org.apache.activemq.artemis.core.client.impl.ServerLocatorInternal) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQDisconnectedException(org.apache.activemq.artemis.api.core.ActiveMQDisconnectedException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) ActiveMQAlreadyReplicatingException(org.apache.activemq.artemis.api.core.ActiveMQAlreadyReplicatingException)

Example 43 with ClientSessionFactoryInternal

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

the class ActiveMQTestBase method waitForRemoteBackup.

public static final void waitForRemoteBackup(ClientSessionFactory sessionFactory, int seconds) {
    ClientSessionFactoryInternal factoryInternal = (ClientSessionFactoryInternal) sessionFactory;
    final long toWait = seconds * 1000;
    final long time = System.currentTimeMillis();
    while (true) {
        if (factoryInternal.getBackupConnector() != null) {
            break;
        }
        if (System.currentTimeMillis() > (time + toWait)) {
            fail("Backup wasn't located");
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            fail(e.getMessage());
        }
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)

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