Search in sources :

Example 16 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class JMSClientTestSupport method createOpenWireConnection.

private Connection createOpenWireConnection(String connectionString, String username, String password, String clientId, boolean start) throws JMSException {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionString);
    Connection connection = trackJMSConnection(factory.createConnection(username, password));
    connection.setExceptionListener(new ExceptionListener() {

        @Override
        public void onException(JMSException exception) {
            exception.printStackTrace();
        }
    });
    if (clientId != null && !clientId.isEmpty()) {
        connection.setClientID(clientId);
    }
    if (start) {
        connection.start();
    }
    return connection;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Connection(javax.jms.Connection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException)

Example 17 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class JMSTopicConsumerTest method testDurableSubscriptionReconnection.

@Test(timeout = 60000)
public void testDurableSubscriptionReconnection() throws Exception {
    Connection connection = createConnection("myClientId");
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(getTopicName());
        int numMessages = 100;
        TopicSubscriber sub = session.createDurableSubscriber(topic, "myPubId");
        Session sendSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = sendSession.createProducer(topic);
        connection.start();
        for (int i = 0; i < numMessages; i++) {
            producer.send(sendSession.createTextMessage("message:" + i));
        }
        for (int i = 0; i < numMessages; i++) {
            TextMessage receive = (TextMessage) sub.receive(5000);
            Assert.assertNotNull(receive);
            Assert.assertEquals(receive.getText(), "message:" + i);
        }
        connection.close();
        connection = createConnection("myClientId");
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException exception) {
                exception.printStackTrace();
            }
        });
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        sub = session.createDurableSubscriber(topic, "myPubId");
        sendSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = sendSession.createProducer(topic);
        connection.start();
        for (int i = 0; i < numMessages; i++) {
            producer.send(sendSession.createTextMessage("message:" + i));
        }
        for (int i = 0; i < numMessages; i++) {
            TextMessage receive = (TextMessage) sub.receive(5000);
            Assert.assertNotNull(receive);
            Assert.assertEquals(receive.getText(), "message:" + i);
        }
    } finally {
        connection.close();
    }
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) Connection(javax.jms.Connection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession) Test(org.junit.Test)

Example 18 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class ConnectionTest method testExceptionListener.

/**
 * Test ExceptionListener stuff
 */
@Test
public void testExceptionListener() throws Exception {
    Connection conn = createConnection();
    ExceptionListener listener1 = new MyExceptionListener();
    conn.setExceptionListener(listener1);
    ExceptionListener listener2 = conn.getExceptionListener();
    ProxyAssertSupport.assertNotNull(listener2);
    ProxyAssertSupport.assertEquals(listener1, listener2);
    conn.close();
    // Ensure setting / getting exception listener can occur before setting clientid.
    final String clientID = "my-test-client-id";
    Connection connection = createConnection();
    ExceptionListener listener = connection.getExceptionListener();
    try {
        connection.setClientID(clientID);
    } catch (javax.jms.IllegalStateException e) {
        ProxyAssertSupport.fail();
    }
    connection.close();
    connection = createConnection();
    connection.setExceptionListener(listener);
    try {
        connection.setClientID(clientID);
    } catch (javax.jms.IllegalStateException e) {
        ProxyAssertSupport.fail();
    }
    connection.close();
}
Also used : Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) ExceptionListener(javax.jms.ExceptionListener) Test(org.junit.Test)

Example 19 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class DisconnectOnCriticalFailureTest method testSendDisconnect.

@Test
@BMRules(rules = { @BMRule(name = "Corrupt Decoding", targetClass = "org.apache.activemq.artemis.core.protocol.core.impl.PacketDecoder", targetMethod = "decode(byte)", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.DisconnectOnCriticalFailureTest.doThrow();") })
public void testSendDisconnect() throws Exception {
    createQueue("queue1");
    final Connection producerConnection = nettyCf.createConnection();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        producerConnection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException e) {
                latch.countDown();
            }
        });
        corruptPacket.set(true);
        producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        assertTrue(latch.await(5, TimeUnit.SECONDS));
    } finally {
        corruptPacket.set(false);
        if (producerConnection != null) {
            producerConnection.close();
        }
    }
}
Also used : Connection(javax.jms.Connection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 20 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class ClientKickoffExample method main.

public static void main(final String[] args) throws Exception {
    QueueConnection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perform a lookup on the Connection Factory
        QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 3.Create a JMS Connection
        connection = cf.createQueueConnection();
        // Step 4. Set an exception listener on the connection to be notified after a problem occurred
        final AtomicReference<JMSException> exception = new AtomicReference<>();
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(final JMSException e) {
                exception.set(e);
            }
        });
        // Step 5. We start the connection
        connection.start();
        // Step 6. Create an ActiveMQServerControlMBean proxy to manage the server
        ObjectName on = ObjectNameBuilder.DEFAULT.getActiveMQServerObjectName();
        JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap<String, String>());
        MBeanServerConnection mbsc = connector.getMBeanServerConnection();
        ActiveMQServerControl serverControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, ActiveMQServerControl.class, false);
        // Step 7. List the remote address connected to the server
        System.out.println("List of remote addresses connected to the server:");
        System.out.println("----------------------------------");
        String[] remoteAddresses = serverControl.listRemoteAddresses();
        for (String remoteAddress : remoteAddresses) {
            System.out.println(remoteAddress);
        }
        System.out.println("----------------------------------");
        // Step 8. Close the connections for the 1st remote address and kickoff the client
        serverControl.closeConnectionsForAddress(remoteAddresses[0]);
        // Sleep a little bit so that the stack trace from the server won't be
        // mingled with the JMSException received on the ExceptionListener
        Thread.sleep(1000);
        // Step 9. Display the exception received by the connection's ExceptionListener
        System.err.println("\nException received from the server:");
        System.err.println("----------------------------------");
        exception.get().printStackTrace();
        System.err.println("----------------------------------");
    } finally {
        // Step 10. Be sure to close the resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) QueueConnectionFactory(javax.jms.QueueConnectionFactory) JMSException(javax.jms.JMSException) AtomicReference(java.util.concurrent.atomic.AtomicReference) InitialContext(javax.naming.InitialContext) ObjectName(javax.management.ObjectName) QueueConnection(javax.jms.QueueConnection) JMXConnector(javax.management.remote.JMXConnector) ExceptionListener(javax.jms.ExceptionListener) MBeanServerConnection(javax.management.MBeanServerConnection)

Aggregations

ExceptionListener (javax.jms.ExceptionListener)28 JMSException (javax.jms.JMSException)25 Connection (javax.jms.Connection)17 Test (org.junit.Test)13 CountDownLatch (java.util.concurrent.CountDownLatch)7 Session (javax.jms.Session)7 MessageConsumer (javax.jms.MessageConsumer)5 ConnectionFactory (javax.jms.ConnectionFactory)4 MessageProducer (javax.jms.MessageProducer)4 TextMessage (javax.jms.TextMessage)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Queue (javax.jms.Queue)3 InitialContext (javax.naming.InitialContext)3 NamingException (javax.naming.NamingException)3 TimeUnit (java.util.concurrent.TimeUnit)2 Destination (javax.jms.Destination)2 IllegalStateException (javax.jms.IllegalStateException)2 Message (javax.jms.Message)2 QueueConnection (javax.jms.QueueConnection)2 Topic (javax.jms.Topic)2