Search in sources :

Example 31 with ActiveMQResourceAdapter

use of org.apache.activemq.artemis.ra.ActiveMQResourceAdapter in project activemq-artemis by apache.

the class ActiveMQMessageHandlerXATest method testXACommitInterruptsWhenStopping.

@Test
public void testXACommitInterruptsWhenStopping() throws Exception {
    ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
    MyBootstrapContext ctx = new MyBootstrapContext();
    qResourceAdapter.start(ctx);
    ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
    spec.setCallTimeout(500L);
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Queue");
    spec.setDestination(MDBQUEUE);
    spec.setMaxSession(1);
    qResourceAdapter.setConnectorClassName(INVM_CONNECTOR_FACTORY);
    CountDownLatch latch = new CountDownLatch(1);
    CountDownLatch beforeDeliveryLatch = new CountDownLatch(1);
    PausingXADummyEndpoint endpoint = new PausingXADummyEndpoint(latch, beforeDeliveryLatch);
    DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, true);
    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);
    ClientMessage message2 = session.createMessage(true);
    message2.getBodyBuffer().writeString("teststring2");
    clientProducer.send(message2);
    session.close();
    beforeDeliveryLatch.await(5, TimeUnit.SECONDS);
    qResourceAdapter.endpointDeactivation(endpointFactory, spec);
    qResourceAdapter.stop();
    assertTrue(endpoint.interrupted);
    assertNotNull(endpoint.lastMessage);
    assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "teststring");
    Binding binding = server.getPostOffice().getBinding(MDBQUEUEPREFIXEDSIMPLE);
    Wait.waitFor(() -> getMessageCount((Queue) binding.getBindable()) == 1);
    long messageCount = getMessageCount((Queue) binding.getBindable());
    assertEquals(1, messageCount);
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQActivationSpec(org.apache.activemq.artemis.ra.inflow.ActiveMQActivationSpec) CountDownLatch(java.util.concurrent.CountDownLatch) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 32 with ActiveMQResourceAdapter

use of org.apache.activemq.artemis.ra.ActiveMQResourceAdapter in project activemq-artemis by apache.

the class JMSContextTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    useDummyTransactionManager();
    super.setUp();
    ActiveMQJAASSecurityManager securityManager = (ActiveMQJAASSecurityManager) server.getSecurityManager();
    securityManager.getConfiguration().addUser("testuser", "testpassword");
    securityManager.getConfiguration().addUser("guest", "guest");
    securityManager.getConfiguration().setDefaultUser("guest");
    securityManager.getConfiguration().addRole("testuser", "arole");
    securityManager.getConfiguration().addRole("guest", "arole");
    Role role = new Role("arole", true, true, true, true, true, true, true, true, true, true);
    Set<Role> roles = new HashSet<>();
    roles.add(role);
    server.getSecurityRepository().addMatch(MDBQUEUEPREFIXED, roles);
    resourceAdapter = new ActiveMQResourceAdapter();
    resourceAdapter.setConnectorClassName(InVMConnectorFactory.class.getName());
    MyBootstrapContext ctx = new MyBootstrapContext();
    resourceAdapter.start(ctx);
    ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
    mcf.setResourceAdapter(resourceAdapter);
    qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
}
Also used : Role(org.apache.activemq.artemis.core.security.Role) ActiveMQRAManagedConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory) ActiveMQJAASSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) HashSet(java.util.HashSet) ActiveMQRAConnectionFactoryImpl(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl) Before(org.junit.Before)

Example 33 with ActiveMQResourceAdapter

use of org.apache.activemq.artemis.ra.ActiveMQResourceAdapter in project activemq-artemis by apache.

the class OutgoingConnectionNoJTATest method setUp.

@Override
@Before
public void setUp() throws Exception {
    useDummyTransactionManager();
    super.setUp();
    ((ActiveMQJAASSecurityManager) server.getSecurityManager()).getConfiguration().addUser("testuser", "testpassword");
    ((ActiveMQJAASSecurityManager) server.getSecurityManager()).getConfiguration().addUser("guest", "guest");
    ((ActiveMQJAASSecurityManager) server.getSecurityManager()).getConfiguration().setDefaultUser("guest");
    ((ActiveMQJAASSecurityManager) server.getSecurityManager()).getConfiguration().addRole("testuser", "arole");
    ((ActiveMQJAASSecurityManager) server.getSecurityManager()).getConfiguration().addRole("guest", "arole");
    Role role = new Role("arole", true, true, true, true, true, true, true, true, true, true);
    Set<Role> roles = new HashSet<>();
    roles.add(role);
    server.getSecurityRepository().addMatch(MDBQUEUEPREFIXED, roles);
    resourceAdapter = new ActiveMQResourceAdapter();
    resourceAdapter.setEntries("[\"java://jmsXA\"]");
    resourceAdapter.setConnectorClassName(InVMConnectorFactory.class.getName());
    MyBootstrapContext ctx = new MyBootstrapContext();
    resourceAdapter.start(ctx);
    mcf = new ActiveMQRAManagedConnectionFactory();
    mcf.setAllowLocalTransactions(true);
    mcf.setResourceAdapter(resourceAdapter);
    qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
}
Also used : Role(org.apache.activemq.artemis.core.security.Role) ActiveMQRAManagedConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) HashSet(java.util.HashSet) ActiveMQRAConnectionFactoryImpl(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl) Before(org.junit.Before)

Example 34 with ActiveMQResourceAdapter

use of org.apache.activemq.artemis.ra.ActiveMQResourceAdapter in project activemq-artemis by apache.

the class MDBMultipleHandlersServerDisconnectTest method testReconnectMDBNoMessageLoss.

@Test
public void testReconnectMDBNoMessageLoss() throws Exception {
    AddressSettings settings = new AddressSettings();
    settings.setRedeliveryDelay(100);
    settings.setMaxDeliveryAttempts(-1);
    server.getAddressSettingsRepository().clear();
    server.getAddressSettingsRepository().addMatch("#", settings);
    ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
    resourceAdapter = qResourceAdapter;
    resourceAdapter.setConfirmationWindowSize(-1);
    resourceAdapter.setCallTimeout(1000L);
    resourceAdapter.setConsumerWindowSize(1024 * 1024);
    resourceAdapter.setReconnectAttempts(-1);
    resourceAdapter.setRetryInterval(100L);
    // qResourceAdapter.setTransactionManagerLocatorClass(DummyTMLocator.class.getName());
    // qResourceAdapter.setTransactionManagerLocatorMethod("getTM");
    MyBootstrapContext ctx = new MyBootstrapContext();
    qResourceAdapter.setConnectorClassName(NETTY_CONNECTOR_FACTORY);
    qResourceAdapter.start(ctx);
    final int NUMBER_OF_SESSIONS = 10;
    ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
    spec.setTransactionTimeout(1);
    spec.setMaxSession(NUMBER_OF_SESSIONS);
    spec.setSetupAttempts(-1);
    spec.setSetupInterval(100L);
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Queue");
    spec.setDestination(MDBQUEUE);
    // Some the routines would be screwed up if using the default one
    Assert.assertFalse(spec.isHasBeenUpdated());
    TestEndpointFactory endpointFactory = new TestEndpointFactory(true);
    qResourceAdapter.endpointActivation(endpointFactory, spec);
    Assert.assertEquals(1, resourceAdapter.getActivations().values().size());
    ActiveMQActivation activation = resourceAdapter.getActivations().values().toArray(new ActiveMQActivation[1])[0];
    final int NUMBER_OF_MESSAGES = 1000;
    Thread producer = new Thread() {

        @Override
        public void run() {
            try {
                ServerLocator locator = createInVMLocator(0);
                ClientSessionFactory factory = locator.createSessionFactory();
                ClientSession session = factory.createSession(false, false);
                ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED);
                StringBuffer buffer = new StringBuffer();
                for (int b = 0; b < 500; b++) {
                    buffer.append("ab");
                }
                for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
                    ClientMessage message = session.createMessage(true);
                    message.getBodyBuffer().writeString(buffer.toString() + i);
                    message.putIntProperty("i", i);
                    clientProducer.send(message);
                    if (i % 100 == 0) {
                        session.commit();
                    }
                }
                session.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    producer.start();
    final AtomicBoolean metaDataFailed = new AtomicBoolean(false);
    Thread buggerThread = new Thread() {

        @Override
        public void run() {
            while (running.get()) {
                try {
                    Thread.sleep(RandomUtil.randomInterval(100, 200));
                } catch (InterruptedException intex) {
                    intex.printStackTrace();
                    return;
                }
                List<ServerSession> serverSessions = lookupServerSessions("resource-adapter", NUMBER_OF_SESSIONS);
                System.err.println("Contains " + serverSessions.size() + " RA sessions");
                if (serverSessions.size() != NUMBER_OF_SESSIONS) {
                    System.err.println("the server was supposed to have " + NUMBER_OF_MESSAGES + " RA Sessions but it only contained accordingly to the meta-data");
                    metaDataFailed.set(true);
                } else if (serverSessions.size() == NUMBER_OF_SESSIONS) {
                    // it became the same after some reconnect? which would be acceptable
                    metaDataFailed.set(false);
                }
                if (playServerClosingSession && serverSessions.size() > 0) {
                    int randomBother = RandomUtil.randomInterval(0, serverSessions.size() - 1);
                    System.out.println("bugging session " + randomBother);
                    ServerSession serverSession = serverSessions.get(randomBother);
                    if (playServerClosingConsumer && RandomUtil.randomBoolean()) {
                        // will play this randomly, only half of the times
                        for (ServerConsumer consumer : serverSession.getServerConsumers()) {
                            try {
                                // Simulating a rare race that could happen in production
                                // where the consumer is closed while things are still happening
                                consumer.close(true);
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    RemotingConnection connection = serverSession.getRemotingConnection();
                    connection.fail(new ActiveMQException("failed at random " + randomBother));
                }
            }
        }
    };
    buggerThread.start();
    ServerLocator locator = createInVMLocator(0);
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(false, false);
    session.start();
    ClientConsumer consumer = session.createConsumer("outQueue");
    for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
        ClientMessage message = consumer.receive(60000);
        if (message == null) {
            break;
        }
        if (i == NUMBER_OF_MESSAGES * 0.50) {
            // This is to make sure the MDBs will survive a reboot
            // and no duplications or message loss will happen because of this
            System.err.println("Rebooting the MDBs at least once!");
            activation.startReconnectThread("I");
        }
        if (i == NUMBER_OF_MESSAGES * 0.90) {
            System.out.println("Disabled failures at " + i);
            playTXTimeouts = false;
            playServerClosingSession = false;
            playServerClosingConsumer = false;
        }
        System.out.println("Received " + i + " messages");
        doReceiveMessage(message);
        if (i % 200 == 0) {
            System.out.println("received " + i);
            session.commit();
        }
    }
    session.commit();
    while (true) {
        ClientMessage message = consumer.receiveImmediate();
        if (message == null) {
            break;
        }
        System.out.println("Received extra message " + message);
        doReceiveMessage(message);
    }
    session.commit();
    Assert.assertNull(consumer.receiveImmediate());
    StringWriter writer = new StringWriter();
    PrintWriter out = new PrintWriter(writer);
    boolean failed = false;
    for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
        AtomicInteger atomicInteger = mapCounter.get(Integer.valueOf(i));
        if (atomicInteger == null) {
            out.println("didn't receive message with i=" + i);
            failed = true;
        } else if (atomicInteger.get() > 1) {
            out.println("message with i=" + i + " received " + atomicInteger.get() + " times");
            failed = true;
        }
    }
    running.set(false);
    buggerThread.join();
    producer.join();
    qResourceAdapter.stop();
    session.close();
    if (failed) {
        for (int i = 0; i < 10; i++) {
            System.out.println("----------------------------------------------------");
        }
        System.out.println(writer.toString());
    }
    Assert.assertFalse(writer.toString(), failed);
    System.out.println("Received " + NUMBER_OF_MESSAGES + " messages");
    Assert.assertFalse("There was meta-data failures, some sessions didn't reconnect properly", metaDataFailed.get());
}
Also used : ActiveMQActivation(org.apache.activemq.artemis.ra.inflow.ActiveMQActivation) 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) StringWriter(java.io.StringWriter) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ActiveMQActivationSpec(org.apache.activemq.artemis.ra.inflow.ActiveMQActivationSpec) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) PrintWriter(java.io.PrintWriter) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ServerConsumer(org.apache.activemq.artemis.core.server.ServerConsumer) MessageEndpoint(javax.resource.spi.endpoint.MessageEndpoint) ResourceException(javax.resource.ResourceException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) LocalTransactionException(javax.resource.spi.LocalTransactionException) RollbackException(javax.transaction.RollbackException) UnavailableException(javax.resource.spi.UnavailableException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Test(org.junit.Test)

Example 35 with ActiveMQResourceAdapter

use of org.apache.activemq.artemis.ra.ActiveMQResourceAdapter in project activemq-artemis by apache.

the class ResourceAdapterTest method testDefaultConnectionFactoryOverrides.

@Test
public void testDefaultConnectionFactoryOverrides() throws Exception {
    ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter();
    ra.setConnectorClassName(InVMConnectorFactory.class.getName());
    ra.setAutoGroup(!ActiveMQClient.DEFAULT_AUTO_GROUP);
    ra.setBlockOnAcknowledge(!ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE);
    ra.setBlockOnNonDurableSend(!ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND);
    ra.setBlockOnDurableSend(!ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND);
    ra.setCallTimeout(1L);
    ra.setClientFailureCheckPeriod(2L);
    ra.setClientID("myid");
    ra.setConnectionLoadBalancingPolicyClassName("mlbcn");
    ra.setConnectionTTL(3L);
    ra.setConsumerMaxRate(4);
    ra.setConsumerWindowSize(5);
    ra.setDiscoveryInitialWaitTimeout(6L);
    ra.setDiscoveryRefreshTimeout(7L);
    ra.setDupsOKBatchSize(8);
    ra.setMinLargeMessageSize(10);
    ra.setPreAcknowledge(!ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE);
    ra.setProducerMaxRate(11);
    ra.setConfirmationWindowSize(12);
    ra.setReconnectAttempts(13);
    ra.setRetryInterval(14L);
    ra.setRetryIntervalMultiplier(15d);
    ra.setScheduledThreadPoolMaxSize(16);
    ra.setThreadPoolMaxSize(17);
    ra.setTransactionBatchSize(18);
    ra.setUseGlobalPools(!ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS);
    ActiveMQConnectionFactory factory = ra.getDefaultActiveMQConnectionFactory();
    Assert.assertEquals(factory.getCallTimeout(), 1);
    Assert.assertEquals(factory.getClientFailureCheckPeriod(), 2);
    Assert.assertEquals(factory.getClientID(), "myid");
    Assert.assertEquals(factory.getConnectionLoadBalancingPolicyClassName(), "mlbcn");
    Assert.assertEquals(factory.getConnectionTTL(), 3);
    Assert.assertEquals(factory.getConsumerMaxRate(), 4);
    Assert.assertEquals(factory.getConsumerWindowSize(), 5);
    Assert.assertEquals(factory.getDupsOKBatchSize(), 8);
    Assert.assertEquals(factory.getMinLargeMessageSize(), 10);
    Assert.assertEquals(factory.getProducerMaxRate(), 11);
    Assert.assertEquals(factory.getConfirmationWindowSize(), 12);
    Assert.assertEquals(factory.getReconnectAttempts(), 13);
    Assert.assertEquals(factory.getRetryInterval(), 14);
    Assert.assertEquals(factory.getRetryIntervalMultiplier(), 15d, 0.00001);
    Assert.assertEquals(factory.getScheduledThreadPoolMaxSize(), 16);
    Assert.assertEquals(factory.getThreadPoolMaxSize(), 17);
    Assert.assertEquals(factory.getTransactionBatchSize(), 18);
    Assert.assertEquals(factory.isAutoGroup(), !ActiveMQClient.DEFAULT_AUTO_GROUP);
    Assert.assertEquals(factory.isBlockOnAcknowledge(), !ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE);
    Assert.assertEquals(factory.isBlockOnNonDurableSend(), !ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND);
    Assert.assertEquals(factory.isBlockOnDurableSend(), !ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND);
    Assert.assertEquals(factory.isPreAcknowledge(), !ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE);
    Assert.assertEquals(factory.isUseGlobalPools(), !ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) Test(org.junit.Test)

Aggregations

ActiveMQResourceAdapter (org.apache.activemq.artemis.ra.ActiveMQResourceAdapter)81 Test (org.junit.Test)72 ActiveMQActivationSpec (org.apache.activemq.artemis.ra.inflow.ActiveMQActivationSpec)54 CountDownLatch (java.util.concurrent.CountDownLatch)39 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)28 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)24 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)24 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)24 InVMConnectorFactory (org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory)14 ConnectionFactoryProperties (org.apache.activemq.artemis.ra.ConnectionFactoryProperties)10 ActiveMQRAManagedConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory)9 ActiveMQRAConnectionFactoryImpl (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl)8 Binding (org.apache.activemq.artemis.core.postoffice.Binding)7 HashSet (java.util.HashSet)5 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)5 Role (org.apache.activemq.artemis.core.security.Role)5 ActiveMQActivation (org.apache.activemq.artemis.ra.inflow.ActiveMQActivation)5 Before (org.junit.Before)5 ArrayList (java.util.ArrayList)4 Session (javax.jms.Session)4