Search in sources :

Example 31 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ResourceAdapterTest method testDefaultConnectionFactory.

// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testDefaultConnectionFactory() throws Exception {
    ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter();
    ra.setConnectorClassName(InVMConnectorFactory.class.getName());
    ActiveMQConnectionFactory factory = ra.getDefaultActiveMQConnectionFactory();
    Assert.assertEquals(factory.getCallTimeout(), ActiveMQClient.DEFAULT_CALL_TIMEOUT);
    Assert.assertEquals(factory.getClientFailureCheckPeriod(), ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD);
    Assert.assertEquals(factory.getClientID(), null);
    Assert.assertEquals(factory.getConnectionLoadBalancingPolicyClassName(), ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME);
    Assert.assertEquals(factory.getConnectionTTL(), ActiveMQClient.DEFAULT_CONNECTION_TTL);
    Assert.assertEquals(factory.getConsumerMaxRate(), ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE);
    Assert.assertEquals(factory.getConsumerWindowSize(), ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE);
    Assert.assertEquals(factory.getDupsOKBatchSize(), ActiveMQClient.DEFAULT_ACK_BATCH_SIZE);
    Assert.assertEquals(factory.getMinLargeMessageSize(), ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE);
    Assert.assertEquals(factory.getProducerMaxRate(), ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE);
    Assert.assertEquals(factory.getConfirmationWindowSize(), ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE);
    // by default, reconnect attempts is set to -1
    Assert.assertEquals(-1, factory.getReconnectAttempts());
    Assert.assertEquals(factory.getRetryInterval(), ActiveMQClient.DEFAULT_RETRY_INTERVAL);
    Assert.assertEquals(factory.getRetryIntervalMultiplier(), ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, 0.00001);
    Assert.assertEquals(factory.getScheduledThreadPoolMaxSize(), ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE);
    Assert.assertEquals(factory.getThreadPoolMaxSize(), ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE);
    Assert.assertEquals(factory.getTransactionBatchSize(), ActiveMQClient.DEFAULT_ACK_BATCH_SIZE);
    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)

Example 32 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ResourceAdapterTest method testCreateConnectionFactoryOverrideConnector.

@Test
public void testCreateConnectionFactoryOverrideConnector() throws Exception {
    ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter();
    ra.setConnectorClassName(InVMConnectorFactory.class.getName());
    ConnectionFactoryProperties connectionFactoryProperties = new ConnectionFactoryProperties();
    ArrayList<String> value = new ArrayList<>();
    value.add(NettyConnectorFactory.class.getName());
    connectionFactoryProperties.setParsedConnectorClassNames(value);
    ActiveMQConnectionFactory factory = ra.getConnectionFactory(connectionFactoryProperties);
    ActiveMQConnectionFactory defaultFactory = ra.getDefaultActiveMQConnectionFactory();
    Assert.assertNotSame(factory, defaultFactory);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactoryProperties(org.apache.activemq.artemis.ra.ConnectionFactoryProperties) NettyConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory) ArrayList(java.util.ArrayList) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) Test(org.junit.Test)

Example 33 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ResourceAdapterTest method testCreateConnectionFactoryOverrideDiscovery.

@Test
public void testCreateConnectionFactoryOverrideDiscovery() throws Exception {
    ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter();
    ra.setConnectorClassName(InVMConnectorFactory.class.getName());
    ConnectionFactoryProperties connectionFactoryProperties = new ConnectionFactoryProperties();
    connectionFactoryProperties.setDiscoveryAddress("myhost");
    connectionFactoryProperties.setDiscoveryPort(5678);
    connectionFactoryProperties.setDiscoveryLocalBindAddress("newAddress");
    ActiveMQConnectionFactory factory = ra.getConnectionFactory(connectionFactoryProperties);
    ActiveMQConnectionFactory defaultFactory = ra.getDefaultActiveMQConnectionFactory();
    Assert.assertNotSame(factory, defaultFactory);
    DiscoveryGroupConfiguration dc = factory.getServerLocator().getDiscoveryGroupConfiguration();
    UDPBroadcastEndpointFactory udpDg = (UDPBroadcastEndpointFactory) dc.getBroadcastEndpointFactory();
    Assert.assertEquals(udpDg.getLocalBindAddress(), "newAddress");
    Assert.assertEquals(udpDg.getGroupAddress(), "myhost");
    Assert.assertEquals(udpDg.getGroupPort(), 5678);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactoryProperties(org.apache.activemq.artemis.ra.ConnectionFactoryProperties) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) DiscoveryGroupConfiguration(org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration) UDPBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.UDPBroadcastEndpointFactory) Test(org.junit.Test)

Example 34 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ConcurrentDeliveryCancelTest method testConcurrentCancels.

@Test
@BMRules(rules = { @BMRule(name = "enterCancel-holdThere", targetClass = "org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl", targetMethod = "close", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.ConcurrentDeliveryCancelTest.enterCancel();") })
public void testConcurrentCancels() throws Exception {
    System.out.println(server.getConfiguration().getJournalLocation().toString());
    server.getAddressSettingsRepository().clear();
    AddressSettings settings = new AddressSettings();
    settings.setMaxDeliveryAttempts(-1);
    server.getAddressSettingsRepository().addMatch("#", settings);
    ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
    cf.setReconnectAttempts(0);
    cf.setRetryInterval(10);
    System.out.println(".....");
    for (ServerSession srvSess : server.getSessions()) {
        System.out.println(srvSess);
    }
    String queueName = RandomUtil.randomString();
    Queue queue = createQueue(queueName);
    int numberOfMessages = 10000;
    {
        Connection connection = cf.createConnection();
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        MessageProducer producer = session.createProducer(queue);
        for (int i = 0; i < numberOfMessages; i++) {
            TextMessage msg = session.createTextMessage("message " + i);
            msg.setIntProperty("i", i);
            producer.send(msg);
        }
        session.commit();
        connection.close();
    }
    for (int i = 0; i < 100; i++) {
        XAConnectionFactory xacf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
        final XAConnection connection = xacf.createXAConnection();
        final XASession theSession = connection.createXASession();
        ((ActiveMQSession) theSession).getCoreSession().addMetaData("theSession", "true");
        connection.start();
        final MessageConsumer consumer = theSession.createConsumer(queue);
        XidImpl xid = newXID();
        theSession.getXAResource().start(xid, XAResource.TMNOFLAGS);
        // I'm setting a small timeout just because I'm lazy to call end myself
        theSession.getXAResource().setTransactionTimeout(1);
        for (int msg = 0; msg < 11; msg++) {
            Assert.assertNotNull(consumer.receiveNoWait());
        }
        System.out.println(".....");
        final List<ServerSession> serverSessions = new LinkedList<>();
        // We will force now the failure simultaneously from several places
        for (ServerSession srvSess : server.getSessions()) {
            if (srvSess.getMetaData("theSession") != null) {
                System.out.println(srvSess);
                serverSessions.add(srvSess);
            }
        }
        // from Transactional reaper
        resetLatches(2);
        List<Thread> threads = new LinkedList<>();
        threads.add(new Thread("ConsumerCloser") {

            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName() + " closing consumer");
                    consumer.close();
                    System.out.println(Thread.currentThread().getName() + " closed consumer");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threads.add(new Thread("SessionCloser") {

            @Override
            public void run() {
                for (ServerSession sess : serverSessions) {
                    System.out.println("Thread " + Thread.currentThread().getName() + " starting");
                    try {
                        // A session.close could sneak in through failover or some other scenarios.
                        // a call to RemotingConnection.fail wasn't replicating the issue.
                        // I needed to call Session.close() directly to replicate what was happening in production
                        sess.close(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread " + Thread.currentThread().getName() + " done");
                }
            }
        });
        for (Thread t : threads) {
            t.start();
        }
        Assert.assertTrue(latchEnter.await(10, TimeUnit.MINUTES));
        latchFlag.countDown();
        for (Thread t : threads) {
            t.join(5000);
            Assert.assertFalse(t.isAlive());
        }
        connection.close();
    }
    Connection connection = cf.createConnection();
    try {
        connection.setClientID("myID");
        // I am too lazy to call end on all the transactions
        Thread.sleep(5000);
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(queue);
        HashMap<Integer, AtomicInteger> mapCount = new HashMap<>();
        while (true) {
            TextMessage message = (TextMessage) consumer.receiveNoWait();
            if (message == null) {
                break;
            }
            Integer value = message.getIntProperty("i");
            AtomicInteger count = mapCount.get(value);
            if (count == null) {
                count = new AtomicInteger(0);
                mapCount.put(message.getIntProperty("i"), count);
            }
            count.incrementAndGet();
        }
        boolean failed = false;
        for (int i = 0; i < numberOfMessages; i++) {
            AtomicInteger count = mapCount.get(i);
            if (count == null) {
                System.out.println("Message " + i + " not received");
                failed = true;
            } else if (count.get() > 1) {
                System.out.println("Message " + i + " received " + count.get() + " times");
                failed = true;
            }
        }
        Assert.assertFalse("test failed, look at the system.out of the test for more information", failed);
    } finally {
        connection.close();
    }
}
Also used : HashMap(java.util.HashMap) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) XASession(javax.jms.XASession) Queue(javax.jms.Queue) XAConnection(javax.jms.XAConnection) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) MessageConsumer(javax.jms.MessageConsumer) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XAConnectionFactory(javax.jms.XAConnectionFactory) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 35 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class DisconnectOnCriticalFailureTest method testClientDisconnectLarge.

@Test(timeout = 60000)
@BMRules(rules = { @BMRule(name = "Corrupt Decoding", targetClass = "org.apache.activemq.artemis.core.protocol.ClientPacketDecoder", targetMethod = "decode(org.apache.activemq.artemis.api.core.ActiveMQBuffer)", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.DisconnectOnCriticalFailureTest.doThrow($1);") })
public void testClientDisconnectLarge() throws Exception {
    Queue q1 = createQueue("queue1");
    final Connection connection = nettyCf.createConnection();
    final CountDownLatch latch = new CountDownLatch(1);
    ServerLocator locator = ((ActiveMQConnectionFactory) nettyCf).getServerLocator();
    int minSize = locator.getMinLargeMessageSize();
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < minSize; i++) {
        builder.append("a");
    }
    try {
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException e) {
                latch.countDown();
            }
        });
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(q1);
        TextMessage m = session.createTextMessage(builder.toString());
        producer.send(m);
        connection.start();
        corruptPacket.set(true);
        MessageConsumer consumer = session.createConsumer(q1);
        Message lm = consumer.receive(2000);
        // first receive won't crash because the packet
        // is SESS_RECEIVE_LARGE_MSG
        assertNotNull(lm);
        // second receive will force server to send a
        // "forced delivery" message, and will cause
        // the exception to be thrown.
        lm = consumer.receive(5000);
        assertNull(lm);
        assertTrue(latch.await(5, TimeUnit.SECONDS));
    } finally {
        corruptPacket.set(false);
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ExceptionListener(javax.jms.ExceptionListener) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Aggregations

ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)221 Test (org.junit.Test)141 Connection (javax.jms.Connection)84 Session (javax.jms.Session)84 MessageProducer (javax.jms.MessageProducer)63 MessageConsumer (javax.jms.MessageConsumer)60 TextMessage (javax.jms.TextMessage)49 Queue (javax.jms.Queue)48 ConnectionFactory (javax.jms.ConnectionFactory)35 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)27 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)26 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)24 ActiveMQResourceAdapter (org.apache.activemq.artemis.ra.ActiveMQResourceAdapter)24 URI (java.net.URI)22 JMSException (javax.jms.JMSException)20 Message (javax.jms.Message)19 DiscoveryGroupConfiguration (org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration)19 InitialContext (javax.naming.InitialContext)16 Context (javax.naming.Context)15 Hashtable (java.util.Hashtable)14