Search in sources :

Example 56 with Connection

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

Example 57 with Connection

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

Example 58 with Connection

use of javax.jms.Connection in project tomee by apache.

the class AMQXASupportTest method xaCode.

@Test
public void xaCode() throws Exception {
    assertNotNull(xacf);
    final Connection connection = xacf.createXAConnection();
    assertThat(connection, instanceOf(XAConnection.class));
    testConnection(connection);
}
Also used : XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 59 with Connection

use of javax.jms.Connection in project tomee by apache.

the class AMQXASupportTest method standardCode.

@Test
public void standardCode() throws Exception {
    assertNotNull(cf);
    final Connection connection = cf.createConnection();
    testConnection(connection);
}
Also used : XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) Test(org.junit.Test)

Example 60 with Connection

use of javax.jms.Connection in project sling by apache.

the class JMSQueueManagerTest method emptyQueue.

private void emptyQueue(String name) throws JMSException {
    dumpQueue(name);
    Connection connection = amqConnectionFactoryService.getConnectionFactory().createConnection();
    connection.start();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue queue = session.createQueue(name);
    MessageConsumer consumer = session.createConsumer(queue);
    for (; ; ) {
        Message m = consumer.receive(100);
        if (m == null) {
            LOGGER.info("No more messages in queue {} ", name);
            break;
        }
        LOGGER.info("Got message  {}", m);
        m.acknowledge();
        session.commit();
    }
    boolean shouldFail = false;
    QueueBrowser browser = session.createBrowser(queue);
    for (Enumeration messages = browser.getEnumeration(); messages.hasMoreElements(); ) {
        Message m = (Message) messages.nextElement();
        LOGGER.info("Queued message {} ", m);
        shouldFail = true;
    }
    browser.close();
    if (shouldFail) {
        fail("Queue was not emptied as expected");
    }
    consumer.close();
    session.close();
    connection.stop();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) Message(javax.jms.Message) Connection(javax.jms.Connection) Queue(javax.jms.Queue) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session)

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