Search in sources :

Example 76 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class JmsConsumerTest method testIndividualACKJms2.

@Test
public void testIndividualACKJms2() throws Exception {
    JMSContext context = cf.createContext(ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
    jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
    JMSProducer producer = context.createProducer();
    JMSConsumer consumer = context.createConsumer(jBossQueue);
    int noOfMessages = 100;
    for (int i = 0; i < noOfMessages; i++) {
        producer.send(jBossQueue, context.createTextMessage("m" + i));
    }
    context.start();
    // Consume even numbers first
    for (int i = 0; i < noOfMessages; i++) {
        Message m = consumer.receive(500);
        Assert.assertNotNull(m);
        if (i % 2 == 0) {
            m.acknowledge();
        }
    }
    context.close();
    context = cf.createContext(ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
    consumer = context.createConsumer(jBossQueue);
    // Consume odd numbers first
    for (int i = 0; i < noOfMessages; i++) {
        if (i % 2 == 0) {
            continue;
        }
        TextMessage m = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(m);
        m.acknowledge();
        Assert.assertEquals("m" + i, m.getText());
    }
    SimpleString queueName = new SimpleString(JmsConsumerTest.Q_NAME);
    context.close();
    Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(queueName).getBindable()).getDeliveringCount());
    Assert.assertEquals(0, getMessageCount((Queue) server.getPostOffice().getBinding(queueName).getBindable()));
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSProducer(javax.jms.JMSProducer) Queue(org.apache.activemq.artemis.core.server.Queue) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 77 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class JmsContextTest method testRollbackTest.

@Test
public void testRollbackTest() {
    JMSContext ctx = addContext(cf.createContext(JMSContext.SESSION_TRANSACTED));
    JMSProducer producer = ctx.createProducer();
    JMSConsumer cons = ctx.createConsumer(queue1);
    producer.send(queue1, context.createTextMessage("hello"));
    ctx.rollback();
    assertNull(cons.receiveNoWait());
    producer.send(queue1, context.createTextMessage("hello"));
    ctx.commit();
    assertNotNull(cons.receiveNoWait());
    ctx.commit();
    ctx.rollback();
    assertNull(cons.receiveNoWait());
    cons.close();
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 78 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class JmsContextTest method testGetAnotherContextFromIt.

@Test
public void testGetAnotherContextFromIt() {
    JMSContext c2 = context.createContext(Session.DUPS_OK_ACKNOWLEDGE);
    Assert.assertNotNull(c2);
    Assert.assertEquals(Session.DUPS_OK_ACKNOWLEDGE, c2.getSessionMode());
    Message m2 = c2.createMessage();
    Assert.assertNotNull(m2);
    // should close its session, but not its (shared) connection
    c2.close();
    try {
        c2.createMessage();
        Assert.fail("session should be closed...");
    } catch (JMSRuntimeException expected) {
    // expected
    }
    Message m1 = context.createMessage();
    Assert.assertNotNull("connection must be open", m1);
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 79 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class JmsContextTest method testSetGetClientIdNewContext.

@Test
public void testSetGetClientIdNewContext() {
    final String id = "123";
    // createContext();
    JMSContext c = context;
    c.setClientID(id);
    JMSContext c2 = addContext(c.createContext(Session.CLIENT_ACKNOWLEDGE));
    Assert.assertEquals(id, c2.getClientID());
}
Also used : JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 80 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class JmsContextTest method recoverAckTest.

@Test
public void recoverAckTest() throws Exception {
    try (JMSContext context = cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE)) {
        int numMessages = 10;
        TextMessage textMessage = null;
        // Create JMSConsumer from JMSContext
        JMSConsumer consumer = context.createConsumer(queue1);
        // Create JMSProducer from JMSContext
        JMSProducer producer = context.createProducer();
        // send messages
        for (int i = 0; i < numMessages; i++) {
            String message = "text message " + i;
            textMessage = context.createTextMessage(message);
            textMessage.setStringProperty("COM_SUN_JMS_TESTNAME", "recoverAckTest" + i);
            producer.send(queue1, textMessage);
        }
        // receive messages but do not acknowledge
        for (int i = 0; i < numMessages; i++) {
            textMessage = (TextMessage) consumer.receive(5000);
            assertNotNull(textMessage);
        }
        context.recover();
        // receive messages a second time followed by acknowledge
        for (int i = 0; i < numMessages; i++) {
            textMessage = (TextMessage) consumer.receive(5000);
            assertNotNull(textMessage);
        }
        // Acknowledge all messages
        context.acknowledge();
    }
    // doing this check with another context / consumer to make sure it was acked.
    try (JMSContext context = cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE)) {
        // Create JMSConsumer from JMSContext
        JMSConsumer consumer = context.createConsumer(queue1);
        assertNull(consumer.receiveNoWait());
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Aggregations

JMSContext (javax.jms.JMSContext)120 Test (org.junit.Test)57 JMSConsumer (javax.jms.JMSConsumer)44 ConnectionFactory (javax.jms.ConnectionFactory)35 Destination (javax.jms.Destination)30 TextMessage (javax.jms.TextMessage)28 JMSProducer (javax.jms.JMSProducer)27 Message (javax.jms.Message)18 JMSRuntimeException (javax.jms.JMSRuntimeException)13 TemporaryQueue (javax.jms.TemporaryQueue)13 InitialContext (javax.naming.InitialContext)12 JMSException (javax.jms.JMSException)10 Queue (javax.jms.Queue)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)7 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 QueueConnectionFactory (javax.jms.QueueConnectionFactory)4 Context (javax.naming.Context)4 NamingException (javax.naming.NamingException)4