本文整理汇总了Java中javax.jms.Connection.setExceptionListener方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.setExceptionListener方法的具体用法?Java Connection.setExceptionListener怎么用?Java Connection.setExceptionListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.jms.Connection
的用法示例。
在下文中一共展示了Connection.setExceptionListener方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExceptionListenerGetsNotified
import javax.jms.Connection; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testExceptionListenerGetsNotified() throws Exception {
final CountDownLatch signal = new CountDownLatch(1);
Connection connection = cf.createConnection();
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException exception) {
LOG.info("ExceptionListener called with error: {}", exception.getMessage());
signal.countDown();
}
});
assertNotNull(connection.getExceptionListener());
MockJMSConnection mockJMSConnection = (MockJMSConnection) ((JmsPoolConnection) connection).getConnection();
mockJMSConnection.injectConnectionError(new JMSException("Some non-fatal error"));
assertTrue(signal.await(10, TimeUnit.SECONDS));
}
示例2: testFailedConnectThenSucceedsWithListener
import javax.jms.Connection; //导入方法依赖的package包/类
@Test
public void testFailedConnectThenSucceedsWithListener() throws JMSException {
Connection connection = pooledConnFact.createConnection("invalid", "credentials");
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException exception) {
LOG.warn("Connection get error: {}", exception.getMessage());
}
});
try {
connection.start();
fail("Should fail to connect");
} catch (JMSSecurityException ex) {
LOG.info("Caught expected security error");
}
connection = pooledConnFact.createConnection("system", "manager");
connection.start();
LOG.info("Successfully create new connection.");
connection.close();
}
示例3: run
import javax.jms.Connection; //导入方法依赖的package包/类
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://Toshiba:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
connection.setExceptionListener(this);
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("HELLOWORLD.TESTQ");
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
示例4: testConnectionCanBeCreatedAfterFailure
import javax.jms.Connection; //导入方法依赖的package包/类
@Ignore("Fails for unknown reason")
@Test(timeout = 60000)
public void testConnectionCanBeCreatedAfterFailure() throws Exception {
final CountDownLatch failed = new CountDownLatch(1);
Connection connection = cf.createConnection();
LOG.info("Fetched new connection from the pool: {}", connection);
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException exception) {
LOG.info("Pooled Connection failed");
failed.countDown();
}
});
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("test");
MessageProducer producer = session.createProducer(queue);
MockJMSConnection mockConnection = (MockJMSConnection) ((JmsPoolConnection) connection).getConnection();
mockConnection.injectConnectionFailure(new IOException("Lost connection"));
assertTrue(failed.await(15, TimeUnit.SECONDS));
try {
producer.send(session.createMessage());
fail("Should be disconnected");
} catch (JMSException ex) {
LOG.info("Producer failed as expected: {}", ex.getMessage());
}
Connection connection2 = cf.createConnection();
assertNotSame(connection, connection2);
LOG.info("Fetched new connection from the pool: {}", connection2);
session = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection2.close();
cf.stop();
}
示例5: testConnectionFailures
import javax.jms.Connection; //导入方法依赖的package包/类
@Test
public void testConnectionFailures() throws Exception {
final CountDownLatch failed = new CountDownLatch(1);
Connection connection = pooledConnFact.createConnection();
LOG.info("Fetched new connection from the pool: {}", connection);
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException exception) {
LOG.info("Pooled Connection failed");
failed.countDown();
}
});
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(getTestName());
MessageProducer producer = session.createProducer(queue);
brokerService.stop();
assertTrue(failed.await(15, TimeUnit.SECONDS));
createBroker();
try {
producer.send(session.createMessage());
fail("Should be disconnected");
} catch (JMSException ex) {
LOG.info("Producer failed as expected: {}", ex.getMessage());
}
Connection connection2 = pooledConnFact.createConnection();
assertNotSame(connection, connection2);
LOG.info("Fetched new connection from the pool: {}", connection2);
session = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection2.close();
pooledConnFact.stop();
}