Search in sources :

Example 16 with QueueConnectionFactory

use of javax.jms.QueueConnectionFactory 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 17 with QueueConnectionFactory

use of javax.jms.QueueConnectionFactory 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 18 with QueueConnectionFactory

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

the class MDBEJBRuntimeNameTestsCase method testMDB.

@Test
@InSequence(1)
public void testMDB() throws Exception {
    final QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("java:/jms/RemoteConnectionFactory");
    final QueueConnection connection = factory.createQueueConnection("guest", "guest");
    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) context.lookup(Constants.QUEUE_JNDI_NAME);
        final MessageProducer producer = session.createProducer(destination);
        producer.send(message);
        producer.close();
        // wait for a reply
        final QueueReceiver receiver = session.createReceiver(replyDestination);
        final Message reply = receiver.receive(TimeoutUtil.adjust(1000));
        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) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test) InSequence(org.jboss.arquillian.junit.InSequence)

Example 19 with QueueConnectionFactory

use of javax.jms.QueueConnectionFactory in project tomee by apache.

the class EncMdbBean method lookupJMSConnectionFactory.

@Override
public void lookupJMSConnectionFactory() throws TestFailureException {
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            Object obj = ctx.lookup("java:comp/env/jms");
            Assert.assertNotNull("The JMS ConnectionFactory is null", obj);
            Assert.assertTrue("Not an instance of ConnectionFactory", obj instanceof ConnectionFactory);
            final ConnectionFactory connectionFactory = (ConnectionFactory) obj;
            testJmsConnection(connectionFactory.createConnection());
            obj = ctx.lookup("java:comp/env/TopicCF");
            Assert.assertNotNull("The JMS TopicConnectionFactory is null", obj);
            Assert.assertTrue("Not an instance of TopicConnectionFactory", obj instanceof TopicConnectionFactory);
            final TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
            testJmsConnection(topicConnectionFactory.createConnection());
            obj = ctx.lookup("java:comp/env/QueueCF");
            Assert.assertNotNull("The JMS QueueConnectionFactory is null", obj);
            Assert.assertTrue("Not an instance of QueueConnectionFactory", obj instanceof QueueConnectionFactory);
            final QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
            testJmsConnection(queueConnectionFactory.createConnection());
        } catch (final Exception e) {
            e.printStackTrace();
            Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
        }
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : TopicConnectionFactory(javax.jms.TopicConnectionFactory) QueueConnectionFactory(javax.jms.QueueConnectionFactory) TopicConnectionFactory(javax.jms.TopicConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) QueueConnectionFactory(javax.jms.QueueConnectionFactory) BasicStatelessObject(org.apache.openejb.test.stateless.BasicStatelessObject) BasicStatefulObject(org.apache.openejb.test.stateful.BasicStatefulObject) BasicBmpObject(org.apache.openejb.test.entity.bmp.BasicBmpObject) TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError) InitialContext(javax.naming.InitialContext) JMSException(javax.jms.JMSException) EJBException(javax.ejb.EJBException) TestFailureException(org.apache.openejb.test.TestFailureException)

Example 20 with QueueConnectionFactory

use of javax.jms.QueueConnectionFactory in project tomee by apache.

the class ContextLookupSingletonBean method lookupJMSConnectionFactory.

public void lookupJMSConnectionFactory() throws TestFailureException {
    try {
        try {
            Object obj = ejbContext.lookup("jms");
            Assert.assertNotNull("The JMS ConnectionFactory is null", obj);
            Assert.assertTrue("Not an instance of ConnectionFactory", obj instanceof ConnectionFactory);
            final ConnectionFactory connectionFactory = (ConnectionFactory) obj;
            testJmsConnection(connectionFactory.createConnection());
            obj = ejbContext.lookup("TopicCF");
            Assert.assertNotNull("The JMS TopicConnectionFactory is null", obj);
            Assert.assertTrue("Not an instance of TopicConnectionFactory", obj instanceof TopicConnectionFactory);
            final TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
            testJmsConnection(topicConnectionFactory.createConnection());
            obj = ejbContext.lookup("QueueCF");
            Assert.assertNotNull("The JMS QueueConnectionFactory is null", obj);
            Assert.assertTrue("Not an instance of QueueConnectionFactory", obj instanceof QueueConnectionFactory);
            final QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
            testJmsConnection(queueConnectionFactory.createConnection());
        } catch (final Exception e) {
            e.printStackTrace();
            Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
        }
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : TopicConnectionFactory(javax.jms.TopicConnectionFactory) QueueConnectionFactory(javax.jms.QueueConnectionFactory) TopicConnectionFactory(javax.jms.TopicConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) QueueConnectionFactory(javax.jms.QueueConnectionFactory) BasicBmpObject(org.apache.openejb.test.entity.bmp.BasicBmpObject) TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError) JMSException(javax.jms.JMSException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) TestFailureException(org.apache.openejb.test.TestFailureException)

Aggregations

QueueConnectionFactory (javax.jms.QueueConnectionFactory)27 ConnectionFactory (javax.jms.ConnectionFactory)18 TopicConnectionFactory (javax.jms.TopicConnectionFactory)18 JMSException (javax.jms.JMSException)16 AssertionFailedError (junit.framework.AssertionFailedError)15 TestFailureException (org.apache.openejb.test.TestFailureException)15 EJBException (javax.ejb.EJBException)13 InitialContext (javax.naming.InitialContext)11 BasicStatefulObject (org.apache.openejb.test.stateful.BasicStatefulObject)11 BasicStatelessObject (org.apache.openejb.test.stateless.BasicStatelessObject)11 RemoteException (java.rmi.RemoteException)10 QueueConnection (javax.jms.QueueConnection)10 BasicBmpObject (org.apache.openejb.test.entity.bmp.BasicBmpObject)10 Test (org.junit.Test)7 Queue (javax.jms.Queue)6 QueueSession (javax.jms.QueueSession)6 Message (javax.jms.Message)5 MessageProducer (javax.jms.MessageProducer)5 TextMessage (javax.jms.TextMessage)5 RemoveException (javax.ejb.RemoveException)4