Search in sources :

Example 86 with MessageConsumer

use of javax.jms.MessageConsumer in project wildfly by wildfly.

the class SendToJMSQueueTest method sendMessageWithClientAcknowledge.

@Test
public void sendMessageWithClientAcknowledge() throws Exception {
    Connection senderConnection = null;
    Connection consumerConnection = null;
    Session senderSession = null;
    Session consumerSession = null;
    MessageConsumer consumer = null;
    try {
        // CREATE CONSUMER
        logger.trace("******* Creating connection for consumer");
        consumerConnection = factory.createConnection();
        logger.trace("Creating session for consumer");
        consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        logger.trace("Creating consumer");
        consumer = consumerSession.createConsumer(queue2);
        logger.trace("Start session");
        consumerConnection.start();
        // SEND A MESSAGE
        logger.trace("***** Start - sending message to topic");
        senderConnection = factory.createConnection();
        logger.trace("Creating session..");
        senderSession = senderConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        MessageProducer producer = senderSession.createProducer(queue2);
        TextMessage message = senderSession.createTextMessage("Hahaha!");
        logger.trace("Sending..");
        producer.send(message);
        logger.trace("Message sent");
        senderConnection.start();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        logger.trace("Closing connections and sessions");
        if (senderSession != null) {
            senderSession.close();
        }
        if (senderConnection != null) {
            senderConnection.close();
        }
    }
    Message receivedMessage = null;
    Message receivedMessage2 = null;
    try {
        logger.trace("Receiving");
        receivedMessage = consumer.receive(5000);
        logger.trace("Received a message");
        receivedMessage.acknowledge();
        consumerSession.recover();
        receivedMessage2 = consumer.receive(5000);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (consumerSession != null) {
            consumerSession.close();
        }
        if (consumerConnection != null) {
            consumerConnection.close();
        }
    }
    if (receivedMessage == null) {
        Assert.fail("received null instead of a TextMessage");
    }
    Assert.assertTrue("received a " + receivedMessage.getClass().getName() + " instead of a TextMessage", receivedMessage instanceof TextMessage);
    Assert.assertEquals("Hahaha!", ((TextMessage) receivedMessage).getText());
    if (receivedMessage2 != null) {
        Assert.fail("redelivered=" + String.valueOf(receivedMessage2.getJMSRedelivered()) + ", text=" + ((TextMessage) receivedMessage).getText());
    }
    Assert.assertNull("Message should not have been re-delivered", receivedMessage2);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 87 with MessageConsumer

use of javax.jms.MessageConsumer in project wildfly by wildfly.

the class SendToJMSQueueTest method sendMessageWithMissingClientAcknowledge.

@Test
public void sendMessageWithMissingClientAcknowledge() throws Exception {
    Connection senderConnection = null;
    Connection consumerConnection = null;
    Session senderSession = null;
    Session consumerSession = null;
    MessageConsumer consumer = null;
    try {
        // CREATE SUBSCRIBER
        logger.trace("******* Creating connection for consumer");
        consumerConnection = factory.createConnection();
        logger.trace("Creating session for consumer");
        consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        logger.trace("Creating consumer");
        consumer = consumerSession.createConsumer(queue3);
        logger.trace("Start session");
        consumerConnection.start();
        // SEND A MESSAGE
        logger.trace("***** Start - sending message to topic");
        senderConnection = factory.createConnection();
        logger.trace("Creating session..");
        senderSession = senderConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        MessageProducer producer = senderSession.createProducer(queue3);
        TextMessage message = senderSession.createTextMessage("Hello world!");
        logger.trace("Sending..");
        producer.send(message);
        logger.trace("Message sent");
        senderConnection.start();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        logger.trace("Closing connections and sessions");
        if (senderSession != null) {
            senderSession.close();
        }
        if (senderConnection != null) {
            senderConnection.close();
        }
    }
    Message receivedMessage = null;
    try {
        logger.trace("Receiving");
        receivedMessage = consumer.receive(5000);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
        }
        consumerSession.recover();
        receivedMessage = consumer.receive(5000);
        receivedMessage.acknowledge();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        if (consumerSession != null) {
            consumerSession.close();
        }
        if (consumerConnection != null) {
            consumerConnection.close();
        }
    }
    if (receivedMessage == null) {
        Assert.fail("Message should have been re-delivered, but subsequent attempt to receive it returned null");
    }
    Assert.assertTrue("received a " + receivedMessage.getClass().getName() + " instead of a TextMessage", receivedMessage instanceof TextMessage);
    Assert.assertEquals(((TextMessage) receivedMessage).getText(), "Hello world!");
    Assert.assertTrue("Redelivered header should have been set to true", receivedMessage.getJMSRedelivered());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 88 with MessageConsumer

use of javax.jms.MessageConsumer in project wildfly by wildfly.

the class SendToJMSQueueTest method sendAndReceiveMessage.

@Test
public void sendAndReceiveMessage() throws Exception {
    Connection connection = null;
    Session session = null;
    Message receivedMessage = null;
    try {
        // SEND A MESSAGE
        connection = factory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        Message message = session.createTextMessage(MESSAGE_TEXT);
        producer.send(message);
        connection.start();
        session.close();
        connection.close();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
        }
        // RECEIVE THE MESSAGE BACK
        connection = factory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();
        receivedMessage = consumer.receive(5000);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // CLEANUP
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
    // ASSERTIONS
    Assert.assertTrue(receivedMessage instanceof TextMessage);
    Assert.assertTrue(((TextMessage) receivedMessage).getText().equals(MESSAGE_TEXT));
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 89 with MessageConsumer

use of javax.jms.MessageConsumer in project wildfly by wildfly.

the class ResourceInjectionSubstitutionTestCase method testResourceInjectionSubstitutionMdb.

/**
     * Test resource injection with MDB
     */
@Test
public void testResourceInjectionSubstitutionMdb() throws Exception {
    // ConnectionFactory and Reply message are injected in SimpleMDB
    ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
    Connection con = factory.createConnection();
    try {
        Destination dest = (Destination) ctx.lookup("java:jboss/queue/testQueue");
        Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(dest);
        Queue replyQueue = session.createTemporaryQueue();
        MessageConsumer consumer = session.createConsumer(replyQueue);
        con.start();
        TextMessage msg = session.createTextMessage();
        msg.setJMSReplyTo(replyQueue);
        msg.setText("This is message one");
        producer.send(msg);
        MapMessage replyMsg = (MapMessage) consumer.receive(5000);
        Assert.assertNotNull(replyMsg);
        Assert.assertEquals("It's Friday!!!", replyMsg.getString("replyMsg"));
    } finally {
        con.close();
    }
}
Also used : Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) MapMessage(javax.jms.MapMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 90 with MessageConsumer

use of javax.jms.MessageConsumer in project ats-framework by Axway.

the class ManagedSession method close.

@Override
public synchronized void close() throws JMSException {
    for (final QueueBrowser b : browsers) try {
        b.close();
    } catch (final Exception e) {
    }
    browsers.clear();
    for (final MessageConsumer b : consumers) try {
        b.close();
    } catch (final Exception e) {
    }
    for (final MessageProducer b : producers) try {
        b.close();
    } catch (final Exception e) {
    }
    for (final ManagedSession b : sessions) b.shutdown();
    session.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MessageProducer(javax.jms.MessageProducer) QueueBrowser(javax.jms.QueueBrowser) JMSException(javax.jms.JMSException)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)107 Session (javax.jms.Session)64 Message (javax.jms.Message)50 MessageProducer (javax.jms.MessageProducer)49 Connection (javax.jms.Connection)47 TextMessage (javax.jms.TextMessage)45 Test (org.junit.Test)45 Destination (javax.jms.Destination)24 JMSException (javax.jms.JMSException)23 ConnectionFactory (javax.jms.ConnectionFactory)15 TemporaryQueue (javax.jms.TemporaryQueue)9 Queue (javax.jms.Queue)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 ObjectMessage (javax.jms.ObjectMessage)7 MessageListener (javax.jms.MessageListener)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)3