Search in sources :

Example 96 with Session

use of javax.jms.Session 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 97 with Session

use of javax.jms.Session 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 98 with Session

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

the class SimplifiedMessageProducer method send.

private void send(ConnectionFactory cf, Destination destination, String text) throws Exception {
    // TODO use JMS 2.0 context when HornetQ supports it
    Connection connection = cf.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(destination);
    Message message = session.createTextMessage(text);
    message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
    producer.send(message);
    connection.close();
}
Also used : Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 99 with Session

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

the class TransactedQueueMessageSender method sendToQueueSuccessfully.

@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void sendToQueueSuccessfully() throws Exception {
    Connection connection = null;
    Session session = null;
    try {
        logger.trace("Creating a Connection");
        connection = factory.createConnection();
        logger.trace("Creating a Session");
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        Message message = session.createTextMessage("Hello world!");
        logger.trace("Sending message");
        producer.send(message);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) JMSException(javax.jms.JMSException) Session(javax.jms.Session) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 100 with Session

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

the class TransactedTopicMessageSender method sendToTopicSuccessfully.

@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void sendToTopicSuccessfully() throws Exception {
    Connection connection = null;
    Session session = null;
    try {
        logger.trace("Creating a Connection");
        connection = factory.createConnection();
        logger.trace("Creating a Session");
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(topic);
        Message message = session.createTextMessage("Hello world!");
        logger.trace("Sending message");
        producer.send(message);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) JMSException(javax.jms.JMSException) Session(javax.jms.Session) TransactionAttribute(javax.ejb.TransactionAttribute)

Aggregations

Session (javax.jms.Session)231 MessageProducer (javax.jms.MessageProducer)106 Test (org.junit.Test)98 Connection (javax.jms.Connection)88 Message (javax.jms.Message)73 TextMessage (javax.jms.TextMessage)69 JMSException (javax.jms.JMSException)58 MessageConsumer (javax.jms.MessageConsumer)53 Topic (javax.jms.Topic)36 ConnectionFactory (javax.jms.ConnectionFactory)31 Destination (javax.jms.Destination)27 Queue (javax.jms.Queue)26 ObjectMessage (javax.jms.ObjectMessage)24 StubTextMessage (org.springframework.jms.StubTextMessage)20 QueueSession (javax.jms.QueueSession)16 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 MapMessage (javax.jms.MapMessage)12 MessageCreator (org.springframework.jms.core.MessageCreator)12 BytesMessage (javax.jms.BytesMessage)11 TopicSession (javax.jms.TopicSession)9