Search in sources :

Example 31 with JMSProducer

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

the class JmsContextTest method testReceiveText.

@Test
public void testReceiveText() throws Exception {
    JMSProducer producer = context.createProducer();
    JMSConsumer consumer = context.createConsumer(queue1);
    String randomStr = newXID().toString();
    System.out.println("RandomStr:" + randomStr);
    TextMessage sendMsg = context.createTextMessage(randomStr);
    producer.send(queue1, sendMsg);
    TextMessage receiveMsg = (TextMessage) consumer.receiveNoWait();
    assertEquals(randomStr, receiveMsg.getText());
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 32 with JMSProducer

use of javax.jms.JMSProducer 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)

Example 33 with JMSProducer

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

the class JmsContextTest method testInvalidDestination.

@Test
public void testInvalidDestination() {
    JMSProducer producer = context.createProducer();
    Message msg = context.createMessage();
    try {
        producer.send((Destination) null, msg);
        Assert.fail("null Destination");
    } catch (InvalidDestinationRuntimeException expected) {
    // no-op
    }
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) JMSProducer(javax.jms.JMSProducer) InvalidDestinationRuntimeException(javax.jms.InvalidDestinationRuntimeException) Test(org.junit.Test)

Example 34 with JMSProducer

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

the class JmsContextTest method testDelay.

@Test
public void testDelay() throws Exception {
    JMSProducer producer = context.createProducer();
    JMSConsumer consumer = context.createConsumer(queue1);
    producer.setDeliveryDelay(500);
    long timeStart = System.currentTimeMillis();
    String strRandom = newXID().toString();
    producer.send(queue1, context.createTextMessage(strRandom));
    TextMessage msg = (TextMessage) consumer.receive(2500);
    assertNotNull(msg);
    long actualDelay = System.currentTimeMillis() - timeStart;
    assertTrue("delay is not working, actualDelay=" + actualDelay, actualDelay >= 500 && actualDelay < 2000);
    assertEquals(strRandom, msg.getText());
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 35 with JMSProducer

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

the class JmsContextTest method testDeliveryMode.

@Test
public void testDeliveryMode() throws Exception {
    JMSProducer producer = context.createProducer();
    JMSConsumer consumer = context.createConsumer(queue1);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    String strRandom = newXID().toString();
    producer.send(queue1, context.createTextMessage(strRandom));
    TextMessage msg = (TextMessage) consumer.receiveNoWait();
    assertNotNull(msg);
    assertEquals(DeliveryMode.NON_PERSISTENT, msg.getJMSDeliveryMode());
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Aggregations

JMSProducer (javax.jms.JMSProducer)39 Test (org.junit.Test)32 JMSContext (javax.jms.JMSContext)22 JMSConsumer (javax.jms.JMSConsumer)21 TextMessage (javax.jms.TextMessage)20 Message (javax.jms.Message)11 BytesMessage (javax.jms.BytesMessage)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)8 ConnectionFactory (javax.jms.ConnectionFactory)7 Destination (javax.jms.Destination)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 StreamMessage (javax.jms.StreamMessage)6 JMSException (javax.jms.JMSException)5 JMSRuntimeException (javax.jms.JMSRuntimeException)4 Queue (javax.jms.Queue)4 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)4 Topic (javax.jms.Topic)3 CompletionListener (javax.jms.CompletionListener)2 IllegalStateRuntimeException (javax.jms.IllegalStateRuntimeException)2 InvalidDestinationRuntimeException (javax.jms.InvalidDestinationRuntimeException)2