Search in sources :

Example 6 with JMSContext

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

the class JmsConsumerTest method testShareDuraleWithJMSContext.

@Test
public void testShareDuraleWithJMSContext() throws Exception {
    ((ActiveMQConnectionFactory) cf).setConsumerWindowSize(0);
    JMSContext conn = cf.createContext(JMSContext.AUTO_ACKNOWLEDGE);
    JMSConsumer consumer = conn.createSharedDurableConsumer(topic, "c1");
    JMSProducer producer = conn.createProducer();
    for (int i = 0; i < 100; i++) {
        producer.setProperty("count", i).send(topic, "test" + i);
    }
    JMSContext conn2 = conn.createContext(JMSContext.AUTO_ACKNOWLEDGE);
    JMSConsumer consumer2 = conn2.createSharedDurableConsumer(topic, "c1");
    for (int i = 0; i < 50; i++) {
        String txt = consumer.receiveBody(String.class, 5000);
        System.out.println("TXT:" + txt);
        Assert.assertNotNull(txt);
        txt = consumer.receiveBody(String.class, 5000);
        System.out.println("TXT:" + txt);
        Assert.assertNotNull(txt);
    }
    Assert.assertNull(consumer.receiveNoWait());
    Assert.assertNull(consumer2.receiveNoWait());
    boolean exceptionHappened = false;
    try {
        conn.unsubscribe("c1");
    } catch (Exception e) {
        e.printStackTrace();
        exceptionHappened = true;
    }
    Assert.assertTrue(exceptionHappened);
    consumer.close();
    consumer2.close();
    conn2.close();
    conn.unsubscribe("c1");
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSContext(javax.jms.JMSContext) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 7 with JMSContext

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

the class JmsContextTest method testDupsOK.

@Test
public void testDupsOK() {
    JMSContext ctx = addContext(cf.createContext(JMSContext.DUPS_OK_ACKNOWLEDGE));
    assertEquals(JMSContext.DUPS_OK_ACKNOWLEDGE, ctx.getSessionMode());
    ctx.close();
    ctx = addContext(cf.createContext(JMSContext.SESSION_TRANSACTED));
    assertEquals(JMSContext.SESSION_TRANSACTED, ctx.getSessionMode());
    ctx.close();
    ctx = addContext(cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE));
    assertEquals(JMSContext.CLIENT_ACKNOWLEDGE, ctx.getSessionMode());
    ctx.close();
    ctx = addContext(cf.createContext(JMSContext.AUTO_ACKNOWLEDGE));
    assertEquals(JMSContext.AUTO_ACKNOWLEDGE, ctx.getSessionMode());
}
Also used : JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 8 with JMSContext

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

the class JmsContextTest method testGetClientId.

@Test
public void testGetClientId() {
    JMSContext context2 = addContext(context.createContext(Session.AUTO_ACKNOWLEDGE));
    final String id = "ID: " + random.nextInt();
    context.setClientID(id);
    Assert.assertEquals("id's must match because the connection is shared", id, context2.getClientID());
}
Also used : JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 9 with JMSContext

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

the class JmsContextTest method testContextStopAndCloseFromMessageListeners.

@Test
public void testContextStopAndCloseFromMessageListeners() throws Exception {
    final JMSContext context1 = context.createContext(Session.AUTO_ACKNOWLEDGE);
    JMSConsumer consumer1 = context1.createConsumer(queue1);
    final CountDownLatch latch1 = new CountDownLatch(1);
    InvalidMessageListener listener1 = new InvalidMessageListener(context1, latch1, 1);
    consumer1.setMessageListener(listener1);
    JMSProducer producer = context1.createProducer();
    Message msg = context1.createTextMessage("first message");
    producer.send(queue1, msg);
    latch1.await();
    Throwable error1 = listener1.getError();
    assertNotNull(error1);
    assertTrue(error1 instanceof IllegalStateRuntimeException);
    context1.close();
    final JMSContext context2 = context.createContext(Session.AUTO_ACKNOWLEDGE);
    JMSConsumer consumer2 = context2.createConsumer(queue1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    InvalidMessageListener listener2 = new InvalidMessageListener(context2, latch2, 2);
    consumer2.setMessageListener(listener2);
    JMSProducer producer2 = context2.createProducer();
    Message msg2 = context2.createTextMessage("second message");
    producer2.send(queue1, msg2);
    latch2.await();
    Throwable error2 = listener2.getError();
    assertNotNull(error2);
    assertTrue(error2 instanceof IllegalStateRuntimeException);
    context2.close();
}
Also used : JMSConsumer(javax.jms.JMSConsumer) IllegalStateRuntimeException(javax.jms.IllegalStateRuntimeException) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) JMSProducer(javax.jms.JMSProducer) CountDownLatch(java.util.concurrent.CountDownLatch) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 10 with JMSContext

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

the class JmsContextTest method testCloseSecondContextConnectionRemainsOpen.

@Test
public void testCloseSecondContextConnectionRemainsOpen() throws JMSException {
    JMSContext localContext = context.createContext(JMSContext.CLIENT_ACKNOWLEDGE);
    Assert.assertEquals("client_ack", JMSContext.CLIENT_ACKNOWLEDGE, localContext.getSessionMode());
    JMSProducer producer = localContext.createProducer();
    JMSConsumer consumer = localContext.createConsumer(queue1);
    final int pass = 1;
    for (int idx = 0; idx < 2; idx++) {
        Message m = localContext.createMessage();
        int intProperty = random.nextInt();
        m.setIntProperty("random", intProperty);
        Assert.assertNotNull(m);
        producer.send(queue1, m);
        m = null;
        Message msg = consumer.receive(100);
        Assert.assertNotNull("must have a msg", msg);
        Assert.assertEquals(intProperty, msg.getIntProperty("random"));
        /* In the second pass we close the connection before ack'ing */
        if (idx == pass) {
            localContext.close();
        }
        /**
         * From {@code JMSContext.close()}'s javadoc:<br/>
         * Invoking the {@code acknowledge} method of a received message from a closed connection's
         * session must throw an {@code IllegalStateRuntimeException}. Closing a closed connection
         * must NOT throw an exception.
         */
        try {
            msg.acknowledge();
            Assert.assertEquals("connection should be open on pass 0. It is " + pass, 0, idx);
        } catch (javax.jms.IllegalStateException expected) {
            // HORNETQ-1209 "JMS 2.0" XXX JMSContext javadoc says we must expect a
            // IllegalStateRuntimeException here. But Message.ack...() says it must throws the
            // non-runtime variant.
            Assert.assertEquals("we only close the connection on pass " + pass, pass, idx);
        }
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) JMSProducer(javax.jms.JMSProducer) JMSContext(javax.jms.JMSContext) 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