Search in sources :

Example 41 with TextMessage

use of javax.jms.TextMessage 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 42 with TextMessage

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

the class DefaultTimeoutMDB method onMessage.

@Override
public void onMessage(Message message) {
    try {
        log.tracef("onMessage received message: %s '%s'", message, ((TextMessage) message).getText());
        final Destination replyTo = message.getJMSReplyTo();
        if (replyTo == null) {
            throw new RuntimeException("ReplyTo info in message was not specified" + " and bean does not know where to reply to");
        }
        TxTestUtil.enlistTestXAResource(tm.getTransaction(), checker);
        try (JMSContext context = factory.createContext()) {
            context.createProducer().setJMSCorrelationID(message.getJMSMessageID()).send(replyTo, REPLY_PREFIX + ((TextMessage) message).getText());
        }
        // waiting up to 2.5 sec - expecting transaction timeout to happen
        TxTestUtil.waitForTimeout(tm);
    } catch (Exception e) {
        throw new RuntimeException("onMessage method execution failed", e);
    }
}
Also used : Destination(javax.jms.Destination) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

Example 43 with TextMessage

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

the class ContainerManagedTransactionNotSupportedTestCase method doSetRollbackOnlyInContainerManagedTransactionNotSupportedMDBThrowsIllegalStateException.

private void doSetRollbackOnlyInContainerManagedTransactionNotSupportedMDBThrowsIllegalStateException(Destination destination) throws Exception {
    try (JMSContext context = cf.createContext()) {
        TemporaryQueue replyTo = context.createTemporaryQueue();
        String text = UUID.randomUUID().toString();
        TextMessage message = context.createTextMessage(text);
        message.setJMSReplyTo(replyTo);
        context.createProducer().send(destination, message);
        Message reply = context.createConsumer(replyTo).receive(adjust(2000));
        assertNotNull(reply);
        assertEquals(message.getJMSMessageID(), reply.getJMSCorrelationID());
        assertTrue("messageDrivenContext.setRollbackOnly() did not throw the expected IllegalStateException", reply.getBooleanProperty(EXCEPTION_PROP_NAME));
    }
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) TemporaryQueue(javax.jms.TemporaryQueue) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

Example 44 with TextMessage

use of javax.jms.TextMessage 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 45 with TextMessage

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

the class SendMessagesTestCase method sendMessage.

private static void sendMessage(Session session, MessageProducer sender, Queue replyQueue, String txt) throws JMSException {
    TextMessage msg = session.createTextMessage(txt);
    msg.setJMSReplyTo(replyQueue);
    msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
    sender.send(msg);
}
Also used : TextMessage(javax.jms.TextMessage)

Aggregations

TextMessage (javax.jms.TextMessage)231 Test (org.junit.Test)92 Session (javax.jms.Session)75 MessageProducer (javax.jms.MessageProducer)71 Message (javax.jms.Message)70 JMSException (javax.jms.JMSException)64 Connection (javax.jms.Connection)44 Destination (javax.jms.Destination)44 MessageConsumer (javax.jms.MessageConsumer)44 ObjectMessage (javax.jms.ObjectMessage)25 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 QueueSession (javax.jms.QueueSession)20 StubTextMessage (org.springframework.jms.StubTextMessage)18 ConnectionFactory (javax.jms.ConnectionFactory)13 QueueConnection (javax.jms.QueueConnection)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 MapMessage (javax.jms.MapMessage)11 Topic (javax.jms.Topic)11 JMSContext (javax.jms.JMSContext)9