Search in sources :

Example 1 with ActiveMQSession

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

the class ActiveMQRASession method getNodeId.

/**
 * Returns the ID of the Node that this session is associated with.
 *
 * @return Node ID
 */
public String getNodeId() throws JMSException {
    ActiveMQSession session = (ActiveMQSession) getSessionInternal();
    ClientSessionFactoryInternal factory = (ClientSessionFactoryInternal) session.getCoreSession().getSessionFactory();
    return factory.getLiveNodeId();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession)

Example 2 with ActiveMQSession

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

the class FailureDeadlockTest method testDeadlock.

// https://jira.jboss.org/jira/browse/JBMESSAGING-1702
// Test that two failures concurrently executing and calling the same exception listener
// don't deadlock
@Test
public void testDeadlock() throws Exception {
    for (int i = 0; i < 100; i++) {
        final Connection conn1 = cf1.createConnection();
        Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
        RemotingConnection rc1 = ((ClientSessionInternal) ((ActiveMQSession) sess1).getCoreSession()).getConnection();
        final Connection conn2 = cf2.createConnection();
        Session sess2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        RemotingConnection rc2 = ((ClientSessionInternal) ((ActiveMQSession) sess2).getCoreSession()).getConnection();
        ExceptionListener listener1 = new ExceptionListener() {

            @Override
            public void onException(final JMSException exception) {
                try {
                    conn2.close();
                } catch (Exception e) {
                    FailureDeadlockTest.log.error("Failed to close connection2", e);
                }
            }
        };
        conn1.setExceptionListener(listener1);
        conn2.setExceptionListener(listener1);
        Failer f1 = new Failer(rc1);
        Failer f2 = new Failer(rc2);
        f1.start();
        f2.start();
        f1.join();
        f2.join();
        conn1.close();
        conn2.close();
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) Test(org.junit.Test)

Example 3 with ActiveMQSession

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

the class FailureDeadlockTest method testUsingDeadConnection.

// https://jira.jboss.org/jira/browse/JBMESSAGING-1703
// Make sure that failing a connection removes it from the connection manager and can't be returned in a subsequent
// call
@Test
public void testUsingDeadConnection() throws Exception {
    for (int i = 0; i < 100; i++) {
        final Connection conn1 = cf1.createConnection();
        Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
        RemotingConnection rc1 = ((ClientSessionInternal) ((ActiveMQSession) sess1).getCoreSession()).getConnection();
        rc1.fail(new ActiveMQNotConnectedException("blah"));
        try {
            conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Assert.fail("should throw exception");
        } catch (JMSException e) {
        // pass
        }
        conn1.close();
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) JMSException(javax.jms.JMSException) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) Test(org.junit.Test)

Example 4 with ActiveMQSession

use of org.apache.activemq.artemis.jms.client.ActiveMQSession 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 5 with ActiveMQSession

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

the class JmsProducerTest method multipleSendsUsingSetters.

@Test
public void multipleSendsUsingSetters() throws Exception {
    server.createQueue(SimpleString.toSimpleString("q1"), RoutingType.ANYCAST, SimpleString.toSimpleString("q1"), null, true, false);
    Queue q1 = context.createQueue("q1");
    context.createProducer().setProperty("prop1", 1).setProperty("prop2", 2).send(q1, "Text1");
    context.createProducer().setProperty("prop1", 3).setProperty("prop2", 4).send(q1, "Text2");
    for (int i = 0; i < 100; i++) {
        context.createProducer().send(q1, "Text" + i);
    }
    ActiveMQSession sessionUsed = (ActiveMQSession) (((ActiveMQJMSContext) context).getUsedSession());
    ClientSessionImpl coreSession = (ClientSessionImpl) sessionUsed.getCoreSession();
    // JMSConsumer is supposed to cache the producer, each call to createProducer is supposed to always return the same producer
    assertEquals(1, coreSession.cloneProducers().size());
    JMSConsumer consumer = context.createConsumer(q1);
    TextMessage text = (TextMessage) consumer.receive(5000);
    assertNotNull(text);
    assertEquals("Text1", text.getText());
    assertEquals(1, text.getIntProperty("prop1"));
    assertEquals(2, text.getIntProperty("prop2"));
    text = (TextMessage) consumer.receive(5000);
    assertNotNull(text);
    assertEquals("Text2", text.getText());
    assertEquals(3, text.getIntProperty("prop1"));
    assertEquals(4, text.getIntProperty("prop2"));
    for (int i = 0; i < 100; i++) {
        assertEquals("Text" + i, consumer.receiveBody(String.class, 1000));
    }
    consumer.close();
    context.close();
}
Also used : ActiveMQJMSContext(org.apache.activemq.artemis.jms.client.ActiveMQJMSContext) JMSConsumer(javax.jms.JMSConsumer) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionImpl) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Aggregations

ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)21 Session (javax.jms.Session)18 Connection (javax.jms.Connection)17 Test (org.junit.Test)16 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)14 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)12 Queue (javax.jms.Queue)11 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)11 MessageConsumer (javax.jms.MessageConsumer)10 MessageProducer (javax.jms.MessageProducer)10 TextMessage (javax.jms.TextMessage)9 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)9 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)7 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)7 JMSException (javax.jms.JMSException)5 BytesMessage (javax.jms.BytesMessage)4 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)4 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)3 ActiveMQConnection (org.apache.activemq.artemis.jms.client.ActiveMQConnection)3 CountDownLatch (java.util.concurrent.CountDownLatch)2