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;
}
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();
}
}
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();
}
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();
}
}
}
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();
}
}
}
Aggregations