Search in sources :

Example 51 with Connection

use of javax.jms.Connection 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 52 with Connection

use of javax.jms.Connection 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 53 with Connection

use of javax.jms.Connection 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 54 with Connection

use of javax.jms.Connection 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 55 with Connection

use of javax.jms.Connection 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)

Aggregations

Connection (javax.jms.Connection)124 Session (javax.jms.Session)88 Test (org.junit.Test)62 MessageProducer (javax.jms.MessageProducer)46 ConnectionFactory (javax.jms.ConnectionFactory)44 Message (javax.jms.Message)38 MessageConsumer (javax.jms.MessageConsumer)38 JMSException (javax.jms.JMSException)32 TextMessage (javax.jms.TextMessage)32 QueueConnection (javax.jms.QueueConnection)22 TopicConnection (javax.jms.TopicConnection)22 Destination (javax.jms.Destination)15 Queue (javax.jms.Queue)12 QueueConnectionFactory (javax.jms.QueueConnectionFactory)11 TopicConnectionFactory (javax.jms.TopicConnectionFactory)11 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)7 TransactionAttribute (javax.ejb.TransactionAttribute)4