Search in sources :

Example 61 with Connection

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

the class JMSQueueManagerTest method checkMessagesInQueue.

private void checkMessagesInQueue(String name, int expected) throws JMSException {
    Connection connection = amqConnectionFactoryService.getConnectionFactory().createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue(name);
    QueueBrowser browser = session.createBrowser(queue);
    int n = 0;
    for (Enumeration e = browser.getEnumeration(); e.hasMoreElements(); ) {
        Message m = (Message) e.nextElement();
        LOGGER.info("Message at {} is {} ", n, m);
        n++;
    }
    browser.close();
    session.close();
    connection.stop();
    assertEquals(expected, n);
}
Also used : Enumeration(java.util.Enumeration) Message(javax.jms.Message) Connection(javax.jms.Connection) Queue(javax.jms.Queue) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session)

Example 62 with Connection

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

the class JmsMdbContainerTest method sendMessage.

private void sendMessage(final ConnectionFactory connectionFactory, final String bean, final String text) throws JMSException, InterruptedException {
    WidgetBean.lock.lock();
    try {
        final Connection connection = connectionFactory.createConnection();
        connection.start();
        // Create a Session
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Queue queue = session.createQueue(bean);
        // Create a MessageProducer from the Session to the Topic or Queue
        final MessageProducer producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        // Create a message
        final TextMessage message = session.createTextMessage(text);
        // Tell the producer to send the message
        producer.send(message);
        WidgetBean.messageRecieved.await();
    } finally {
        WidgetBean.lock.unlock();
    }
}
Also used : Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 63 with Connection

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

the class JmsProxyTest method testProxy.

public void testProxy() throws Exception {
    // create reciever object
    final JmsProxyTest.TestObject testObject = new JmsProxyTest.TestObject("foo");
    final MdbInvoker mdbInvoker = new MdbInvoker(connectionFactory, testObject);
    // Create a Session
    final Connection connection = connectionFactory.createConnection();
    connection.start();
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    // Create the request Queue
    final Destination requestQueue = session.createQueue(REQUEST_QUEUE_NAME);
    final MessageConsumer consumer = session.createConsumer(requestQueue);
    consumer.setMessageListener(mdbInvoker);
    // create in invoker
    final JmsProxyTest.TestInterface testInterface = MdbProxy.newProxyInstance(JmsProxyTest.TestInterface.class, connectionFactory, REQUEST_QUEUE_NAME);
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) Session(javax.jms.Session)

Example 64 with Connection

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

the class JmsTest method testProxy.

public void testProxy() throws Exception {
    // Create a Session
    final Connection connection = connectionFactory.createConnection();
    try {
        connection.start();
        final Destination requestQueue = createListener(connection);
        createSender(connection, requestQueue);
    } finally {
        MdbUtil.close(connection);
    }
}
Also used : Destination(javax.jms.Destination) Connection(javax.jms.Connection)

Example 65 with Connection

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

the class Messages method receiveMessage.

public String receiveMessage() throws JMSException {
    Connection connection = null;
    Session session = null;
    MessageConsumer consumer = null;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        // Create a Session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Create a MessageConsumer from the Session to the Topic or Queue
        consumer = session.createConsumer(chatQueue);
        // Wait for a message
        TextMessage message = (TextMessage) consumer.receive(1000);
        return message.getText();
    } finally {
        if (consumer != null) {
            consumer.close();
        }
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) TextMessage(javax.jms.TextMessage) 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