Search in sources :

Example 31 with MessageConsumer

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

the class EnvEntryTestCase method testEnvEntriesMDB.

@Test
public void testEnvEntriesMDB() throws Exception {
    ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
    Connection con = factory.createConnection();
    try {
        Destination dest = (Destination) ctx.lookup("java:jboss/queue/testEnvEntry");
        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(16, replyMsg.getInt("maxExceptions"));
        Assert.assertEquals(12, replyMsg.getInt("numExceptions"));
        Assert.assertEquals(7, replyMsg.getInt("minExceptions"));
    } 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 32 with MessageConsumer

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

the class SendMessagesTestCase method testShutdown.

@Test
public void testShutdown(@ArquillianResource @OperateOnDeployment("singleton") ManagementClient client) throws Exception {
    Connection connection = null;
    try {
        deployer.deploy("mdb");
        ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
        Queue queue = (Queue) ctx.lookup(QUEUE_SEND);
        connection = cf.createConnection("guest", "guest");
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue replyQueue = session.createTemporaryQueue();
        MessageProducer sender = session.createProducer(queue);
        MessageConsumer receiver = session.createConsumer(replyQueue);
        connection.start();
        // we do not assume message order since the 1st message will be redelivered
        // after redeployment (as the MDB is interrupted on 1st delivery)
        Set<String> expected = new TreeSet<String>();
        sendMessage(session, sender, replyQueue, "await");
        expected.add("Reply: await");
        // synchronize receiving message
        int awaitInt = awaitSingleton("await before undeploy");
        log.debug("testsuite: first awaitSingleton() returned: " + awaitInt);
        Future<?> undeployed = executor.submit(undeployTask());
        for (int i = 1; i <= 50; i++) {
            String msg = this.getClass().getSimpleName() + " 50loop: " + i;
            // should be bounced by BlockContainerShutdownInterceptor
            sendMessage(session, sender, replyQueue, msg);
            expected.add("Reply: " + msg);
        }
        log.debug("Sent 50 messages during MDB is undeploying");
        // synchronize with transaction timeout
        awaitInt = awaitSingleton("await after undeploy");
        log.debug("testsuite: second awaitSingleton() returned: " + awaitInt);
        undeployed.get(UNDEPLOYED_WAIT_S, SECONDS);
        // deploying via management client, arquillian deployer does not work for some reason
        final ModelNode deployAddr = new ModelNode();
        deployAddr.get(ClientConstants.OP_ADDR).add("deployment", MESSAGE_DRIVEN_BEAN + ".jar");
        deployAddr.get(ClientConstants.OP).set("deploy");
        applyUpdate(deployAddr, managementClient.getControllerClient());
        for (int i = 1; i <= 10; i++) {
            String msg = this.getClass().getSimpleName() + "10loop: " + i;
            sendMessage(session, sender, replyQueue, msg);
            expected.add("Reply: " + msg);
        }
        log.debug("Sent 10 more messages");
        Set<String> received = new TreeSet<String>();
        for (int i = 1; i <= (1 + 50 + 10 + 1); i++) {
            Message msg = receiver.receive(SECONDS.toMillis(RECEIVE_WAIT_S));
            assertNotNull("did not receive message with ordered number " + i + " in " + SECONDS.toMillis(RECEIVE_WAIT_S) + " seconds", msg);
            String text = ((TextMessage) msg).getText();
            received.add(text);
            log.trace(i + ": " + text);
        }
        assertNull(receiver.receiveNoWait());
        assertEquals(expected, received);
    } finally {
        if (connection != null) {
            connection.close();
        }
        deployer.undeploy("mdb");
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) ConnectionFactory(javax.jms.ConnectionFactory) TreeSet(java.util.TreeSet) MessageProducer(javax.jms.MessageProducer) ModelNode(org.jboss.dmr.ModelNode) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 33 with MessageConsumer

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

the class MDBTestCase method doDeliveryActive.

private void doDeliveryActive(Destination destination, String mdbName) throws Exception {
    // ReplyingMDB has been deployed with deliveryActive set to false
    assertMDBDeliveryIsActive(mdbName, false);
    Connection connection = null;
    try {
        connection = cf.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        // send a message to the MDB
        MessageProducer producer = session.createProducer(destination);
        Message message = session.createMessage();
        message.setJMSReplyTo(replyQueue);
        producer.send(message);
        // the MDB did not reply to the message because its delivery is not active
        MessageConsumer consumer = session.createConsumer(replyQueue);
        Message reply = consumer.receive(TIMEOUT);
        assertNull(reply);
        executeMDBOperation(mdbName, "start-delivery");
        assertMDBDeliveryIsActive(mdbName, true);
        // WFLY-4470 check duplicate message when start delivery twice. Last assertNull(reply) should still be valid
        executeMDBOperation(mdbName, "start-delivery");
        // the message was delivered to the MDB which replied
        reply = consumer.receive(TIMEOUT);
        assertNotNull(reply);
        assertEquals(message.getJMSMessageID(), reply.getJMSCorrelationID());
        executeMDBOperation(mdbName, "stop-delivery");
        assertMDBDeliveryIsActive(mdbName, false);
        // send again a message to the MDB
        message = session.createMessage();
        message.setJMSReplyTo(replyQueue);
        producer.send(message);
        // the MDB did not reply to the message because its delivery is not active
        reply = consumer.receive(TIMEOUT);
        assertNull(reply);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 34 with MessageConsumer

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

the class HelloBean method sendMessage.

public String sendMessage() throws Exception {
    String destinationName = "java:jboss/exported/queue/TestQueue";
    Context ic = null;
    ConnectionFactory cf = null;
    Connection connection = null;
    try {
        ic = getInitialContext();
        cf = (ConnectionFactory) ic.lookup("java:/ConnectionFactory");
        Queue queue = (Queue) ic.lookup(destinationName);
        connection = cf.createConnection("guest", "guest");
        // we need to start connection for consumer
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(queue);
        TextMessage message = session.createTextMessage("hello goodbye");
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        message.setJMSReplyTo(replyQueue);
        sender.send(message);
        MessageConsumer consumer = session.createConsumer(replyQueue);
        TextMessage replyMsg = (TextMessage) consumer.receive(5000);
        log.trace("Message received:" + replyMsg);
        return replyMsg.getText();
    } finally {
        if (ic != null) {
            try {
                ic.close();
            } catch (Exception ignore) {
            }
        }
        closeConnection(connection);
    }
}
Also used : InitialContext(javax.naming.InitialContext) SessionContext(javax.ejb.SessionContext) Context(javax.naming.Context) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 35 with MessageConsumer

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

the class RunAsTestCaseEJBMDB method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    ConnectionFactory cf = null;
    Connection connection = null;
    Session session = null;
    try {
        cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
        Queue queue = (Queue) initialContext.lookup(QUEUE_NAME);
        connection = cf.createConnection("guest", "guest");
        //for consumer we need to start connection
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(queue);
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        TextMessage message = session.createTextMessage("hello goodbye");
        message.setJMSReplyTo(replyQueue);
        sender.send(message);
        log.trace("testSendMessage(): Message sent!");
        MessageConsumer consumer = session.createConsumer(replyQueue);
        Message replyMsg = consumer.receive(5000);
        Assert.assertNotNull(replyMsg);
        Assert.assertTrue(replyMsg instanceof TextMessage);
        String actual = ((TextMessage) replyMsg).getText();
        Assert.assertEquals("Howdy Fred! GoodBye user1", actual);
    } finally {
        if (session != null) {
            session.close();
        }
        closeConnection(connection);
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

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