Example usage for javax.jms Connection setExceptionListener

List of usage examples for javax.jms Connection setExceptionListener

Introduction

In this page you can find the example usage for javax.jms Connection setExceptionListener.

Prototype


void setExceptionListener(ExceptionListener listener) throws JMSException;

Source Link

Document

Sets an exception listener for this connection.

Usage

From source file:org.wso2.andes.systest.TestingBaseCase.java

/**
 * Perform the Main test of a topic Consumer with the given AckMode.
 *
 * Test creates a new connection and sets up the connection to prevent
 * failover/*from w w  w. ja  v a 2 s.  co m*/
 *
 * A new consumer is connected and started so that it will prefetch msgs.
 *
 * An asynchrounous publisher is started to fill the broker with messages.
 *
 * We then wait to be notified of the disconnection via the ExceptionListener
 *
 * 0-10 does not have the same notification paths but sync() apparently should
 * give us the exception, currently it doesn't, so the test is excluded from 0-10
 *
 * We should ensure that this test has the same path for all protocol versions.
 *
 * Clients should not have to modify their code based on the protocol in use.
 *
 * @param ackMode @see javax.jms.Session
 *
 * @throws Exception
 */
protected void topicConsumer(int ackMode, boolean durable) throws Exception {
    Connection connection = getConnection();

    connection.setExceptionListener(this);

    Session session = connection.createSession(ackMode == Session.SESSION_TRANSACTED, ackMode);

    _destination = session.createTopic(getName());

    MessageConsumer consumer;

    if (durable) {
        consumer = session.createDurableSubscriber(_destination, getTestQueueName());
    } else {
        consumer = session.createConsumer(_destination);
    }

    connection.start();

    // Start the consumer pre-fetching
    // Don't care about response as we will fill the broker up with messages
    // after this point and ensure that the client is disconnected at the
    // right point.
    consumer.receiveNoWait();
    startPublisher(_destination);

    boolean disconnected = _disconnectionLatch.await(DISCONNECTION_WAIT, TimeUnit.SECONDS);

    assertTrue("Client was not disconnected", disconnected);
    assertTrue("Client was not disconnected.", _connectionException != null);

    Exception linked = _connectionException.getLinkedException();

    _publisher.join(JOIN_WAIT);

    assertFalse("Publisher still running", _publisher.isAlive());

    //Validate publishing occurred ok
    if (_publisherError != null) {
        throw _publisherError;
    }

    // NOTE these exceptions will need to be modeled so that they are not
    // 0-8 specific. e.g. JMSSessionClosedException

    assertNotNull("No error received onException listener.", _connectionException);

    assertNotNull("No linked exception set on:" + _connectionException.getMessage(), linked);

    assertTrue("Incorrect linked exception received.", linked instanceof AMQException);

    AMQException amqException = (AMQException) linked;

    assertEquals("Channel was not closed with correct code.", AMQConstant.RESOURCE_ERROR,
            amqException.getErrorCode());
}

From source file:org.wso2.carbon.esb.scenario.test.common.jms.ActiveMQJMSClient.java

/**
 * Function to retrieve message from specified message queue
 *
 * @param queueName Name of the queue/* ww  w.  jav a 2  s .c o m*/
 * @param timeout Timeout value (in milliseconds)
 * @return Retrieved message from the queue
 * @throws JMSException if error occurred
 */
public Message consumeMessageFromQueue(String queueName, long timeout) throws JMSException {

    Connection connection = null;
    Session session = null;
    MessageConsumer consumer = null;

    try {
        // Create a ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

        // Create a Connection
        connection = connectionFactory.createConnection();
        connection.start();
        connection.setExceptionListener(this);

        // Create a Session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create the destination (Topic or Queue)
        Destination destination = session.createQueue(queueName);

        // Create a MessageConsumer from the Session to the Topic or Queue
        consumer = session.createConsumer(destination);

        // Wait for a message
        return consumer.receive(timeout);

    } finally {
        if (consumer != null) {
            consumer.close();
        }
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.wso2.carbon.esb.scenario.test.common.jms.ActiveMQJMSClient.java

/**
 * Function to produce message to ActiveMQ Queue
 *
 * @param queueName name of the target queue
 * @param messageStr message to place/*from  w w w. j a v a 2 s  .c o m*/
 * @throws JMSException
 */
public void produceMessageToQueue(String queueName, String messageStr) throws JMSException {

    Connection connection = null;
    Session session = null;

    try {
        // Create a ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

        // Create a Connection
        connection = connectionFactory.createConnection();
        connection.start();
        connection.setExceptionListener(this);

        // Create a Session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create the destination (Topic or Queue)
        Destination destination = session.createQueue(queueName);

        // Create a MessageProducer from the Session to the Topic or Queue
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        // Create a messages
        TextMessage message = session.createTextMessage(messageStr);

        // Tell the producer to send the message
        producer.send(message);

    } finally {
        // Clean up
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.wso2.carbon.transport.jms.factory.JMSClientConnectionFactory.java

@Override
public Connection createConnection() throws JMSException {
    Connection connection = super.createConnection();
    connection.setExceptionListener(new JMSErrorListener());
    return connection;
}