下面列出了怎么用javax.jms.IllegalStateRuntimeException的API类实例代码及写法,或者点击链接到github查看源代码。
private void enqueue(long n) {
for (int i = 0; i < n; i++) {
executor.execute(() -> {
try {
Message message = consumer.receive();
if (message != null) { // null means closed.
requests.decrementAndGet();
downstream.get().onNext(message);
}
} catch (IllegalStateRuntimeException e) {
log.clientClosed();
}
});
}
}
@Override
public JMSProducer createProducer() {
if (connectionRefCount.get() == 0) {
throw new IllegalStateRuntimeException("The Connection is closed");
}
try {
if (sharedProducer == null) {
synchronized (this) {
if (sharedProducer == null) {
sharedProducer = (JmsPoolMessageProducer) getSession().createProducer(null);
}
}
}
return new JmsPoolJMSProducer(getSession(), sharedProducer);
} catch (JMSException jmse) {
throw JMSExceptionSupport.createRuntimeException(jmse);
}
}
@Test
public void testRuntimeExceptionOnAcknowledgeFailure() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).acknowledge(ACK_TYPE.ACCEPTED);
try {
context.acknowledge();
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test
public void testRuntimeExceptionFromSendByteBody() throws JMSException {
JMSProducer producer = context.createProducer();
MockJMSConnection connection = (MockJMSConnection) context.getConnection();
connection.addConnectionListener(new MockJMSDefaultConnectionListener() {
@Override
public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
throw new IllegalStateException("Send Failed");
}
});
try {
producer.send(context.createTemporaryQueue(), new byte[0]);
fail("Should have thrown an exception");
} catch (IllegalStateRuntimeException isre) {}
}
@Test
public void testRuntimeExceptionFromSendMapBody() throws JMSException {
JMSProducer producer = context.createProducer();
MockJMSConnection connection = (MockJMSConnection) context.getConnection();
connection.addConnectionListener(new MockJMSDefaultConnectionListener() {
@Override
public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
throw new IllegalStateException("Send Failed");
}
});
try {
producer.send(context.createTemporaryQueue(), Collections.<String, Object>emptyMap());
fail("Should have thrown an exception");
} catch (IllegalStateRuntimeException isre) {}
}
@Test
public void testRuntimeExceptionFromSendSerializableBody() throws JMSException {
JMSProducer producer = context.createProducer();
MockJMSConnection connection = (MockJMSConnection) context.getConnection();
connection.addConnectionListener(new MockJMSDefaultConnectionListener() {
@Override
public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
throw new IllegalStateException("Send Failed");
}
});
try {
producer.send(context.createTemporaryQueue(), UUID.randomUUID());
fail("Should have thrown an exception");
} catch (IllegalStateRuntimeException isre) {}
}
@Test
public void testRuntimeExceptionFromSendStringBody() throws JMSException {
JMSProducer producer = context.createProducer();
MockJMSConnection connection = (MockJMSConnection) context.getConnection();
connection.addConnectionListener(new MockJMSDefaultConnectionListener() {
@Override
public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
throw new IllegalStateException("Send Failed");
}
});
try {
producer.send(context.createTemporaryQueue(), "test");
fail("Should have thrown an exception");
} catch (IllegalStateRuntimeException isre) {}
}
@Override
public JMSProducer createProducer() {
if (connectionRefCount.get() == 0) {
throw new IllegalStateRuntimeException("The Connection is closed");
}
try {
if (sharedProducer == null) {
synchronized (this) {
if (sharedProducer == null) {
sharedProducer = (MockJMSMessageProducer) getSession().createProducer(null);
}
}
}
return new MockJMSProducer(getSession(), sharedProducer);
} catch (JMSException jmse) {
throw JMSExceptionSupport.createRuntimeException(jmse);
}
}
@Test
public void testRuntimeExceptionOnCreateSharedConsumerSelectorNoLocal() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(session.createTemporaryTopic()).thenReturn(new JmsTemporaryTopic());
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.AUTO_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).
createSharedConsumer(any(Topic.class), anyString(), anyString());
try {
context.createSharedConsumer(context.createTemporaryTopic(), "name", "a = b");
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
Mockito.verify(session, Mockito.times(1)).createSharedConsumer(any(Topic.class), anyString(), anyString());
}
@Test
public void testRuntimeExceptionOnCreateStreamMessage() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createStreamMessage();
try {
context.createStreamMessage();
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test
public void testRuntimeExceptionOnCreateMessage() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createMessage();
try {
context.createMessage();
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test
public void testRuntimeExceptionOnCreateTemporaryTopicFailure() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createTemporaryTopic();
try {
context.createTemporaryTopic();
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test
public void testRuntimeExceptionOnCreateSharedConsumer() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(session.createTemporaryTopic()).thenReturn(new JmsTemporaryTopic());
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.AUTO_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createSharedConsumer(any(Topic.class), anyString());
try {
context.createSharedConsumer(context.createTemporaryTopic(), "name");
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
Mockito.verify(session, Mockito.times(1)).createSharedConsumer(any(Topic.class), anyString());
}
@Test
public void testRuntimeExceptionOnCreateSharedDurableConsumerSelectorNoLocal() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(session.createTemporaryTopic()).thenReturn(new JmsTemporaryTopic());
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.AUTO_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).
createSharedDurableConsumer(any(Topic.class), anyString(), anyString());
try {
context.createSharedDurableConsumer(context.createTemporaryTopic(), "name", "a = b");
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
Mockito.verify(session, Mockito.times(1)).createSharedDurableConsumer(any(Topic.class), anyString(), anyString());
}
@Test
public void testRuntimeExceptionOnCreateQueueFailure() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createQueue(anyString());
try {
context.createQueue("test");
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test
public void testRuntimeExceptionOnCreateBytesMessage() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createBytesMessage();
try {
context.createBytesMessage();
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test
public void testRuntimeExceptionOnCreateTopicFailure() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createTopic(anyString());
try {
context.createTopic("test");
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test(timeout = 60000)
public void testSetClientIDTwiceWithSameID() throws Exception {
JMSContext context = cf.createContext();
// test: call setClientID("newID") twice
// this should be tolerated and not result in an exception
context.setClientID("newID");
try {
context.setClientID("newID");
context.start();
context.close();
} catch (IllegalStateRuntimeException ise) {
LOG.error("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
fail("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
} finally {
cf.stop();
}
LOG.debug("Test finished.");
}
@Test(timeout = 60000)
public void testSetClientIDTwiceWithDifferentID() throws Exception {
JMSContext context = cf.createContext();
// test: call setClientID() twice with different IDs
// this should result in an IllegalStateException
context.setClientID("newID1");
try {
context.setClientID("newID2");
fail("calling Connection.setClientID() twice with different clientID must raise an IllegalStateException");
} catch (IllegalStateRuntimeException ise) {
LOG.debug("Correctly received " + ise);
} finally {
context.close();
cf.stop();
}
LOG.debug("Test finished.");
}
@Test(timeout = 60000)
public void testSetClientIDAfterConnectionStart() throws Exception {
JMSContext context = cf.createContext();
// test: try to call setClientID() after start()
// should result in an exception
try {
context.start();
context.setClientID("newID3");
fail("Calling setClientID() after start() mut raise a JMSException.");
} catch (IllegalStateRuntimeException ise) {
LOG.debug("Correctly received " + ise);
} finally {
context.close();
cf.stop();
}
LOG.debug("Test finished.");
}
@Test
public void testRuntimeExceptionOnCreateTextMessage() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createTextMessage();
try {
context.createTextMessage();
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test
public void testRuntimeExceptionFromSendMessage() throws JMSException {
JmsSession session = Mockito.mock(JmsSession.class);
JmsMessageProducer messageProducer = Mockito.mock(JmsMessageProducer.class);
Message message = Mockito.mock(Message.class);
Mockito.when(session.createTemporaryQueue()).thenReturn(new JmsTemporaryQueue());
Mockito.when(session.createMessage()).thenReturn(message);
Mockito.doThrow(IllegalStateException.class).when(message).setJMSCorrelationID(anyString());
JmsProducer producer = new JmsProducer(session, messageProducer);
producer.setJMSCorrelationID("id");
try {
producer.send(session.createTemporaryQueue(), session.createMessage());
fail("Should have thrown an exception");
} catch (IllegalStateRuntimeException isre) {}
}
@Test
public void testRuntimeExceptionFromSendByteBody() throws JMSException {
JmsSession session = Mockito.mock(JmsSession.class);
JmsMessageProducer messageProducer = Mockito.mock(JmsMessageProducer.class);
Mockito.when(session.createTemporaryQueue()).thenReturn(new JmsTemporaryQueue());
Mockito.when(session.createMessage()).thenReturn(Mockito.mock(Message.class));
Mockito.doThrow(IllegalStateException.class).when(session).createBytesMessage();
JmsProducer producer = new JmsProducer(session, messageProducer);
try {
producer.send(session.createTemporaryQueue(), new byte[0]);
fail("Should have thrown an exception");
} catch (IllegalStateRuntimeException isre) {}
}
@Test
public void testRuntimeExceptionFromSendMapBody() throws JMSException {
JmsSession session = Mockito.mock(JmsSession.class);
JmsMessageProducer messageProducer = Mockito.mock(JmsMessageProducer.class);
Mockito.when(session.createTemporaryQueue()).thenReturn(new JmsTemporaryQueue());
Mockito.when(session.createMessage()).thenReturn(Mockito.mock(Message.class));
Mockito.doThrow(IllegalStateException.class).when(session).createMapMessage();
JmsProducer producer = new JmsProducer(session, messageProducer);
try {
producer.send(session.createTemporaryQueue(), Collections.<String, Object>emptyMap());
fail("Should have thrown an exception");
} catch (IllegalStateRuntimeException isre) {}
}
@Test
public void testRuntimeExceptionFromSendStringBody() throws JMSException {
JmsSession session = Mockito.mock(JmsSession.class);
JmsMessageProducer messageProducer = Mockito.mock(JmsMessageProducer.class);
Mockito.when(session.createTemporaryQueue()).thenReturn(new JmsTemporaryQueue());
Mockito.when(session.createMessage()).thenReturn(Mockito.mock(Message.class));
Mockito.doThrow(IllegalStateException.class).when(session).createTextMessage(anyString());
JmsProducer producer = new JmsProducer(session, messageProducer);
try {
producer.send(session.createTemporaryQueue(), "test");
fail("Should have thrown an exception");
} catch (IllegalStateRuntimeException isre) {}
}
@Test
public void testRuntimeExceptionOnCreateSharedDurableConsumer() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(session.createTemporaryTopic()).thenReturn(new JmsTemporaryTopic());
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.AUTO_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createSharedDurableConsumer(any(Topic.class), anyString());
try {
context.createSharedDurableConsumer(context.createTemporaryTopic(), "name");
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
Mockito.verify(session, Mockito.times(1)).createSharedDurableConsumer(any(Topic.class), anyString());
}
@Test
public void testRuntimeExceptionOnCreateTextMessageWithBody() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.CLIENT_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createTextMessage(anyString());
try {
context.createTextMessage("test");
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
}
@Test
public void testRuntimeExceptionOnGetMessageListener() throws JMSException {
JmsSession session = Mockito.mock(JmsSession.class);
JmsMessageConsumer messageConsumer = Mockito.mock(JmsMessageConsumer.class);
JmsConsumer consumer = new JmsConsumer(session, messageConsumer);
Mockito.doThrow(IllegalStateException.class).when(messageConsumer).getMessageListener();
try {
consumer.getMessageListener();
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
consumer.close();
}
}
@Test
public void testRuntimeExceptionOnSetMessageListener() throws JMSException {
JmsSession session = Mockito.mock(JmsSession.class);
JmsMessageConsumer messageConsumer = Mockito.mock(JmsMessageConsumer.class);
JmsConsumer consumer = new JmsConsumer(session, messageConsumer);
Mockito.doThrow(IllegalStateException.class).when(messageConsumer).setMessageListener(null);
try {
consumer.setMessageListener(null);
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
consumer.close();
}
}
@Test
public void testRuntimeExceptionOnCreateQueueBrowser() throws JMSException {
JmsConnection connection = Mockito.mock(JmsConnection.class);
JmsSession session = Mockito.mock(JmsSession.class);
Mockito.when(session.createTemporaryQueue()).thenReturn(new JmsTemporaryQueue());
Mockito.when(connection.createSession(Mockito.anyInt())).thenReturn(session);
JmsContext context = new JmsContext(connection, JMSContext.AUTO_ACKNOWLEDGE);
Mockito.doThrow(IllegalStateException.class).when(session).createBrowser(any(Queue.class));
try {
context.createBrowser(context.createTemporaryQueue());
fail("Should throw ISRE");
} catch (IllegalStateRuntimeException isre) {
} finally {
context.close();
}
Mockito.verify(session, Mockito.times(1)).createBrowser(any(Queue.class));
}