Search in sources :

Example 21 with JMSProducer

use of javax.jms.JMSProducer in project qpid-broker-j by apache.

the class DeliveryDelayTest method testDeliveryDelay.

@Test
public void testDeliveryDelay() throws Exception {
    try (JMSContext context = getConnectionBuilder().buildConnectionFactory().createContext()) {
        Destination queue = createQueue(context, BrokerAdmin.TEST_QUEUE_NAME, true);
        final AtomicLong messageReceiptTime = new AtomicLong();
        final CountDownLatch receivedLatch = new CountDownLatch(1);
        context.createConsumer(queue).setMessageListener(message -> {
            messageReceiptTime.set(System.currentTimeMillis());
            receivedLatch.countDown();
        });
        JMSProducer producer = context.createProducer().setDeliveryDelay(DELIVERY_DELAY);
        final long messageSentTime = System.currentTimeMillis();
        producer.send(queue, "delayed message");
        final boolean messageArrived = receivedLatch.await(DELIVERY_DELAY * 3, TimeUnit.MILLISECONDS);
        assertTrue("Delayed message did not arrive within expected period", messageArrived);
        final long actualDelay = messageReceiptTime.get() - messageSentTime;
        assertTrue(String.format("Message was not delayed by sufficient time (%d). Actual delay (%d)", DELIVERY_DELAY, actualDelay), actualDelay >= DELIVERY_DELAY);
    }
}
Also used : Destination(javax.jms.Destination) AtomicLong(java.util.concurrent.atomic.AtomicLong) JMSProducer(javax.jms.JMSProducer) CountDownLatch(java.util.concurrent.CountDownLatch) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 22 with JMSProducer

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

the class OutgoingConnectionJTATest method testSimpleSendNoXAJMSContext.

@Test
public void testSimpleSendNoXAJMSContext() throws Exception {
    Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
    try (ClientSessionFactory sf = locator.createSessionFactory();
        ClientSession session = sf.createSession();
        ClientConsumer consVerify = session.createConsumer(MDBQUEUE);
        JMSContext jmsctx = qraConnectionFactory.createContext()) {
        session.start();
        // These next 4 lines could be written in a single line however it makes difficult for debugging
        JMSProducer producer = jmsctx.createProducer();
        producer.setProperty("strvalue", "hello");
        TextMessage msgsend = jmsctx.createTextMessage("hello");
        producer.send(q, msgsend);
        ClientMessage msg = consVerify.receive(1000);
        assertNotNull(msg);
        assertEquals("hello", msg.getStringProperty("strvalue"));
    }
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) JMSProducer(javax.jms.JMSProducer) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Queue(javax.jms.Queue) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 23 with JMSProducer

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

the class JMSAutoCloseableExample method main.

public static void main(final String[] args) throws Exception {
    // Step 2. Perfom a lookup on the queue
    Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
    // Step 4.Create a JMS Context using the try-with-resources statement
    try (// Even though ConnectionFactory is not closeable it would be nice to close an ActiveMQConnectionFactory
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
        JMSContext jmsContext = cf.createContext()) {
        // Step 5. create a jms producer
        JMSProducer jmsProducer = jmsContext.createProducer();
        // Step 6. Try sending a message, we don't have the appropriate privileges to do this so this will throw an exception
        jmsProducer.send(queue, "A Message from JMS2!");
        System.out.println("Received:" + jmsContext.createConsumer(queue).receiveBody(String.class));
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) JMSProducer(javax.jms.JMSProducer) Queue(javax.jms.Queue) JMSContext(javax.jms.JMSContext)

Example 24 with JMSProducer

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

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

the class InvalidDestinationTest method invalidDestinationRuntimeExceptionTests.

@Test
public void invalidDestinationRuntimeExceptionTests() throws Exception {
    JMSProducer producer = context.createProducer();
    Destination invalidDestination = null;
    Topic invalidTopic = null;
    String message = "hello world";
    byte[] bytesMsgSend = message.getBytes();
    Map<String, Object> mapMsgSend = new HashMap<>();
    mapMsgSend.put("s", "foo");
    mapMsgSend.put("b", true);
    mapMsgSend.put("i", 1);
    TextMessage expTextMessage = context.createTextMessage(message);
    try {
        producer.send(invalidDestination, expTextMessage);
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        producer.send(invalidDestination, message);
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    ObjectMessage om = context.createObjectMessage();
    StringBuffer sb = new StringBuffer(message);
    om.setObject(sb);
    try {
        producer.send(invalidDestination, om);
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        producer.send(invalidDestination, bytesMsgSend);
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        producer.send(invalidDestination, mapMsgSend);
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createConsumer(invalidDestination);
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createConsumer(invalidDestination, "lastMessage = TRUE");
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createConsumer(invalidDestination, "lastMessage = TRUE", false);
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createDurableConsumer(invalidTopic, "InvalidDestinationRuntimeException");
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createDurableConsumer(invalidTopic, "InvalidDestinationRuntimeException", "lastMessage = TRUE", false);
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createSharedDurableConsumer(invalidTopic, "InvalidDestinationRuntimeException");
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createSharedDurableConsumer(invalidTopic, "InvalidDestinationRuntimeException", "lastMessage = TRUE");
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.unsubscribe("InvalidSubscriptionName");
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createSharedConsumer(invalidTopic, "InvalidDestinationRuntimeException");
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
    try {
        context.createSharedConsumer(invalidTopic, "InvalidDestinationRuntimeException", "lastMessage = TRUE");
    } catch (InvalidDestinationRuntimeException e) {
    // pass
    } catch (Exception e) {
        fail("Expected InvalidDestinationRuntimeException, received " + e);
    }
}
Also used : Destination(javax.jms.Destination) HashMap(java.util.HashMap) JMSProducer(javax.jms.JMSProducer) InvalidDestinationRuntimeException(javax.jms.InvalidDestinationRuntimeException) InvalidDestinationException(javax.jms.InvalidDestinationException) InvalidDestinationRuntimeException(javax.jms.InvalidDestinationRuntimeException) ObjectMessage(javax.jms.ObjectMessage) Topic(javax.jms.Topic) 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