Search in sources :

Example 31 with TextMessage

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

the class LegacyJMSTestCase method doSendAndReceive.

private void doSendAndReceive(String connectionFactoryLookup, String destinationLoookup) throws Exception {
    Destination destination = (Destination) remoteContext.lookup(destinationLoookup);
    assertNotNull(destination);
    ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
    assertNotNull(cf);
    try (JMSContext producerContext = cf.createContext("guest", "guest");
        JMSContext consumerContext = cf.createContext("guest", "guest")) {
        final CountDownLatch latch = new CountDownLatch(10);
        final List<String> result = new ArrayList<String>();
        JMSConsumer consumer = consumerContext.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                TextMessage msg = (TextMessage) message;
                try {
                    result.add(msg.getText());
                    latch.countDown();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        JMSProducer producer = producerContext.createProducer();
        for (int i = 0; i < 10; i++) {
            String text = "Test" + i;
            producer.send(destination, text);
        }
        assertTrue(latch.await(3, SECONDS));
        assertEquals(10, result.size());
        for (int i = 0; i < result.size(); i++) {
            assertEquals("Test" + i, result.get(i));
        }
    }
}
Also used : Destination(javax.jms.Destination) JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) JMSProducer(javax.jms.JMSProducer) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionFactory(javax.jms.ConnectionFactory) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

Example 32 with TextMessage

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

the class DefaultJMSConnectionFactoryTest method assertMessageInQueue.

private void assertMessageInQueue(String text) throws JMSException {
    Connection connection = factory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(queue);
    connection.start();
    Message message = consumer.receive(2000);
    assertNotNull(message);
    assertTrue(message instanceof TextMessage);
    assertEquals(text, ((TextMessage) message).getText());
    connection.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 33 with TextMessage

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

the class JMSBridgeTest method sendAndReceiveMessage.

/**
     * Send a message on the source queue
     * Consumes it on the target queue
     *
     * The test will pass since a JMS Bridge has been created to bridge the source destination to the target destination.
     */
@Test
public void sendAndReceiveMessage() throws Exception {
    Connection connection = null;
    Session session = null;
    Message receivedMessage = null;
    try {
        // SEND A MESSAGE on the source queue
        connection = factory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(sourceQueue);
        String text = MESSAGE_TEXT + " " + UUID.randomUUID().toString();
        Message message = session.createTextMessage(text);
        message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
        producer.send(message);
        connection.start();
        connection.close();
        // RECEIVE THE MESSAGE from the target queue
        connection = factory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(targetQueue);
        connection.start();
        receivedMessage = consumer.receive(5000);
        // ASSERTIONS
        assertNotNull("did not receive expected message", receivedMessage);
        assertTrue(receivedMessage instanceof TextMessage);
        assertEquals(text, ((TextMessage) receivedMessage).getText());
        assertNotNull("did not get header set by the JMS bridge", receivedMessage.getStringProperty(ActiveMQJMSConstants.AMQ_MESSAGING_BRIDGE_MESSAGE_ID_LIST));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // CLEANUP
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 34 with TextMessage

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

the class SendToJMSTopicTest method sendMessage.

@Test
public void sendMessage() 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.AUTO_ACKNOWLEDGE);
        logger.trace("Creating consumer");
        consumer = consumerSession.createConsumer(topic);
        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.AUTO_ACKNOWLEDGE);
        MessageProducer producer = senderSession.createProducer(topic);
        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);
        logger.trace("Received: " + ((TextMessage) receivedMessage).getText());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        if (receivedMessage == null) {
            Assert.fail("received null instead of a TextMessage");
        }
        if (consumerSession != null) {
            consumerSession.close();
        }
        if (consumerConnection != null) {
            consumerConnection.close();
        }
    }
    Assert.assertTrue("received a " + receivedMessage.getClass().getName() + " instead of a TextMessage", receivedMessage instanceof TextMessage);
    Assert.assertEquals(((TextMessage) receivedMessage).getText(), "Hello world!");
}
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 35 with TextMessage

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

the class JmsClientTestCase method doSendAndReceive.

private void doSendAndReceive(String connectionFactoryLookup) throws Exception {
    Connection conn = null;
    try {
        ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
        assertNotNull(cf);
        Destination destination = (Destination) remoteContext.lookup(QUEUE_NAME);
        assertNotNull(destination);
        conn = cf.createConnection("guest", "guest");
        conn.start();
        Session consumerSession = conn.createSession(false, AUTO_ACKNOWLEDGE);
        final CountDownLatch latch = new CountDownLatch(10);
        final List<String> result = new ArrayList<String>();
        // Set the async listener
        MessageConsumer consumer = consumerSession.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                TextMessage msg = (TextMessage) message;
                try {
                    result.add(msg.getText());
                    latch.countDown();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        final Session producerSession = conn.createSession(false, AUTO_ACKNOWLEDGE);
        MessageProducer producer = producerSession.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for (int i = 0; i < 10; i++) {
            String s = "Test" + i;
            TextMessage msg = producerSession.createTextMessage(s);
            //System.out.println("sending " + s);
            producer.send(msg);
        }
        producerSession.close();
        assertTrue(latch.await(3, SECONDS));
        assertEquals(10, result.size());
        for (int i = 0; i < result.size(); i++) {
            assertEquals("Test" + i, result.get(i));
        }
    } finally {
        try {
            conn.close();
        } catch (Exception ignore) {
        }
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) JMSException(javax.jms.JMSException) ConnectionFactory(javax.jms.ConnectionFactory) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

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