Search in sources :

Example 71 with QueueConnection

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

the class JMSMessageDrivenBeanTestCase method testSendMessage.

@Test
public void testSendMessage() throws JMSException, NamingException {
    final InitialContext ctx = new InitialContext();
    final QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("java:/JmsXA");
    final QueueConnection connection = factory.createQueueConnection();
    connection.start();
    try {
        final QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        final Queue replyDestination = session.createTemporaryQueue();
        final QueueReceiver receiver = session.createReceiver(replyDestination);
        final Message message = session.createTextMessage("Test");
        message.setJMSReplyTo(replyDestination);
        final Destination destination = (Destination) ctx.lookup("queue/myAwesomeQueue");
        final MessageProducer producer = session.createProducer(destination);
        producer.send(message);
        producer.close();
        final Message reply = receiver.receive(TimeoutUtil.adjust(5000));
        assertNotNull(reply);
        final String result = ((TextMessage) reply).getText();
        assertEquals("replying Test", result);
    } finally {
        connection.close();
    }
}
Also used : Destination(javax.jms.Destination) QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueConnectionFactory(javax.jms.QueueConnectionFactory) QueueReceiver(javax.jms.QueueReceiver) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 72 with QueueConnection

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

the class PooledEJBLifecycleTestCase method triggerRequestResponseCycleOnQueue.

// ------------------ HELPER METHODS -------------------
private void triggerRequestResponseCycleOnQueue() throws Exception {
    final InitialContext ctx = new InitialContext();
    final QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("java:/JmsXA");
    final QueueConnection connection = factory.createQueueConnection();
    try {
        connection.start();
        final QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        final Queue replyDestination = session.createTemporaryQueue();
        final String requestMessage = "test";
        final Message message = session.createTextMessage(requestMessage);
        message.setJMSReplyTo(replyDestination);
        final Destination destination = (Destination) ctx.lookup(Constants.QUEUE_JNDI_NAME);
        final MessageProducer producer = session.createProducer(destination);
        // create receiver
        final QueueReceiver receiver = session.createReceiver(replyDestination);
        producer.send(message);
        producer.close();
        // wait for reply
        final Message reply = receiver.receive(TimeoutUtil.adjust(5000));
        assertNotNull("Did not receive a reply on the reply queue. Perhaps the original (request) message didn't make it to the MDB?", reply);
        final String result = ((TextMessage) reply).getText();
        assertEquals("Unexpected reply messsage", Constants.REPLY_MESSAGE_PREFIX + requestMessage, result);
    } finally {
        if (connection != null) {
            // just closing the connection will close the session and other related resources (@see javax.jms.Connection)
            safeClose(connection);
        }
    }
}
Also used : Destination(javax.jms.Destination) QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueConnectionFactory(javax.jms.QueueConnectionFactory) QueueReceiver(javax.jms.QueueReceiver) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage)

Example 73 with QueueConnection

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

the class MDBRoleTestCase method testIsMDBinRole.

@Test
public void testIsMDBinRole() throws NamingException, JMSException {
    final InitialContext ctx = new InitialContext();
    final QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("java:/JmsXA");
    final QueueConnection connection = factory.createQueueConnection();
    connection.start();
    final QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    final Queue replyDestination = session.createTemporaryQueue();
    final QueueReceiver receiver = session.createReceiver(replyDestination);
    final Message message = session.createTextMessage("Let's test it!");
    message.setJMSReplyTo(replyDestination);
    final Destination destination = (Destination) ctx.lookup("queue/myAwesomeQueue");
    final MessageProducer producer = session.createProducer(destination);
    producer.send(message);
    producer.close();
    final Message reply = receiver.receive(TimeoutUtil.adjust(5000));
    assertNotNull(reply);
    final String result = ((TextMessage) reply).getText();
    assertEquals(SimpleSLSB.SUCCESS, result);
    connection.close();
}
Also used : Destination(javax.jms.Destination) QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueConnectionFactory(javax.jms.QueueConnectionFactory) QueueReceiver(javax.jms.QueueReceiver) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 74 with QueueConnection

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

the class QueueTestMDB method sendReply.

private void sendReply(Queue destination, String messageID) throws JMSException {
    QueueConnection conn = qFactory.createQueueConnection();
    try {
        QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        QueueSender sender = session.createSender(destination);
        TextMessage message = session.createTextMessage("SUCCESS");
        message.setJMSCorrelationID(messageID);
        sender.send(message, DeliveryMode.NON_PERSISTENT, 4, 500);
    } finally {
        conn.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueSender(javax.jms.QueueSender) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage)

Example 75 with QueueConnection

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

the class MDBLifecycleCallback method sendReply.

private void sendReply(Queue destination, String messageID, Exception e) throws JMSException {
    QueueConnection conn = qFactory.createQueueConnection();
    try {
        QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        QueueSender sender = session.createSender(destination);
        ObjectMessage message = session.createObjectMessage(e == null ? "SUCCESS" : e);
        message.setJMSCorrelationID(messageID);
        sender.send(message, DeliveryMode.NON_PERSISTENT, 4, 500);
    } finally {
        conn.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueSender(javax.jms.QueueSender) ObjectMessage(javax.jms.ObjectMessage) QueueSession(javax.jms.QueueSession)

Aggregations

QueueConnection (javax.jms.QueueConnection)77 QueueSession (javax.jms.QueueSession)53 Test (org.junit.Test)41 TextMessage (javax.jms.TextMessage)36 Queue (javax.jms.Queue)33 Message (javax.jms.Message)26 MessageProducer (javax.jms.MessageProducer)20 QueueConnectionFactory (javax.jms.QueueConnectionFactory)20 Session (javax.jms.Session)17 JMSException (javax.jms.JMSException)16 InitialContext (javax.naming.InitialContext)15 QueueSender (javax.jms.QueueSender)14 XAQueueConnection (javax.jms.XAQueueConnection)14 ActiveMQRAConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory)14 ActiveMQRAConnectionFactoryImpl (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl)14 ActiveMQRAManagedConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory)14 QueueReceiver (javax.jms.QueueReceiver)13 MessageConsumer (javax.jms.MessageConsumer)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)9 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)6