Search in sources :

Example 71 with ActiveMQConnectionFactory

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

the class JmsConsumerTest method testPreCommitAcksSetOnConnectionFactory.

@Test
public void testPreCommitAcksSetOnConnectionFactory() throws Exception {
    ((ActiveMQConnectionFactory) cf).setPreAcknowledge(true);
    conn = cf.createConnection();
    Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
    MessageProducer producer = session.createProducer(jBossQueue);
    MessageConsumer consumer = session.createConsumer(jBossQueue);
    int noOfMessages = 100;
    for (int i = 0; i < noOfMessages; i++) {
        producer.send(session.createTextMessage("m" + i));
    }
    conn.start();
    for (int i = 0; i < noOfMessages; i++) {
        Message m = consumer.receive(500);
        Assert.assertNotNull(m);
    }
    // Messages should all have been acked since we set pre ack on the cf
    SimpleString queueName = new SimpleString(JmsConsumerTest.Q_NAME);
    Queue queue = server.locateQueue(queueName);
    Wait.assertEquals(0, queue::getDeliveringCount);
    Wait.assertEquals(0, queue::getMessageCount);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageProducer(javax.jms.MessageProducer) Queue(org.apache.activemq.artemis.core.server.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 72 with ActiveMQConnectionFactory

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

the class JmsConsumerTest method testBrowserOnly.

@Test
public void testBrowserOnly() throws Exception {
    ((ActiveMQConnectionFactory) cf).setConsumerWindowSize(0);
    conn = cf.createConnection();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
    MessageProducer producer = session.createProducer(jBossQueue);
    int noOfMessages = 10;
    for (int i = 0; i < noOfMessages; i++) {
        TextMessage textMessage = session.createTextMessage("m" + i);
        textMessage.setIntProperty("i", i);
        producer.send(textMessage);
    }
    QueueBrowser browser = session.createBrowser(jBossQueue);
    Enumeration enumMessages = browser.getEnumeration();
    for (int i = 0; i < noOfMessages; i++) {
        Assert.assertTrue(enumMessages.hasMoreElements());
        TextMessage msg = (TextMessage) enumMessages.nextElement();
        Assert.assertNotNull(msg);
        Assert.assertEquals(i, msg.getIntProperty("i"));
    }
    Assert.assertFalse(enumMessages.hasMoreElements());
    conn.close();
// Asserting delivering count is zero is bogus since messages might still be being delivered and expired at this
// point
// which can cause delivering count to flip to 1
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) Enumeration(java.util.Enumeration) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session) Test(org.junit.Test)

Example 73 with ActiveMQConnectionFactory

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

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

the class JmsConsumerTest method testBrowserAndConsumerSimultaneous.

@Test
public void testBrowserAndConsumerSimultaneous() throws Exception {
    ((ActiveMQConnectionFactory) cf).setConsumerWindowSize(0);
    conn = cf.createConnection();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
    MessageProducer producer = session.createProducer(jBossQueue);
    QueueBrowser browser = session.createBrowser(jBossQueue);
    Enumeration enumMessages = browser.getEnumeration();
    MessageConsumer consumer = session.createConsumer(jBossQueue);
    int noOfMessages = 10;
    for (int i = 0; i < noOfMessages; i++) {
        TextMessage textMessage = session.createTextMessage("m" + i);
        textMessage.setIntProperty("i", i);
        producer.send(textMessage);
    }
    conn.start();
    for (int i = 0; i < noOfMessages; i++) {
        TextMessage msg = (TextMessage) enumMessages.nextElement();
        Assert.assertNotNull(msg);
        Assert.assertEquals(i, msg.getIntProperty("i"));
        conn.start();
        TextMessage recvMessage = (TextMessage) consumer.receiveNoWait();
        Assert.assertNotNull(recvMessage);
        conn.stop();
        Assert.assertEquals(i, msg.getIntProperty("i"));
    }
    Assert.assertNull(consumer.receiveNoWait());
    Assert.assertFalse(enumMessages.hasMoreElements());
    conn.close();
// Asserting delivering count is zero is bogus since messages might still be being delivered and expired at this
// point
// which can cause delivering count to flip to 1
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) MessageProducer(javax.jms.MessageProducer) QueueBrowser(javax.jms.QueueBrowser) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 75 with ActiveMQConnectionFactory

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

the class JMSServerStartStopTest method testStopStart1.

@Test
public void testStopStart1() throws Exception {
    final int numMessages = 5;
    for (int j = 0; j < numMessages; j++) {
        JMSServerStartStopTest.log.info("Iteration " + j);
        jmsServer.start();
        ActiveMQConnectionFactory jbcf = createConnectionFactory();
        jbcf.setBlockOnDurableSend(true);
        jbcf.setBlockOnNonDurableSend(true);
        Connection conn = jbcf.createConnection();
        try {
            Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = sess.createQueue("myJMSQueue");
            MessageProducer producer = sess.createProducer(queue);
            TextMessage tm = sess.createTextMessage("message" + j);
            producer.send(tm);
        } finally {
            conn.close();
            jbcf.close();
            jmsServer.stop();
        }
    }
    jmsServer.start();
    jbcf = createConnectionFactory();
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    conn = jbcf.createConnection();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = sess.createQueue("myJMSQueue");
    MessageConsumer consumer = sess.createConsumer(queue);
    conn.start();
    for (int i = 0; i < numMessages; i++) {
        TextMessage tm = (TextMessage) consumer.receive(10000);
        Assert.assertNotNull("not null", tm);
        Assert.assertEquals("message" + i, tm.getText());
    }
    conn.close();
    jbcf.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

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