Java源码示例:javax.jms.IllegalStateException

示例1
public void deleteTemporaryQueue(final ActiveMQDestination tempQueue) throws JMSException {
   if (!tempQueue.isTemporary()) {
      throw new InvalidDestinationException("Not a temporary queue " + tempQueue);
   }
   try {
      QueueQuery response = session.queueQuery(tempQueue.getSimpleAddress());

      if (!response.isExists()) {
         throw new InvalidDestinationException("Cannot delete temporary queue " + tempQueue.getName() +
                                                  " does not exist");
      }

      if (response.getConsumerCount() > 0) {
         throw new IllegalStateException("Cannot delete temporary queue " + tempQueue.getName() +
                                            " since it has subscribers");
      }

      SimpleString address = tempQueue.getSimpleAddress();

      session.deleteQueue(address);

      connection.removeTemporaryQueue(address);
   } catch (ActiveMQException e) {
      throw JMSExceptionHelper.convertFromActiveMQException(e);
   }
}
 
示例2
@Test
public void testRuntimeExceptionFromSendMessage() 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(), context.createMessage());
        fail("Should have thrown an exception");
    } catch (IllegalStateRuntimeException isre) {}
}
 
示例3
@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) {}
}
 
示例4
@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) {}
}
 
示例5
@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) {}
}
 
示例6
@Override
public void setClientID(String clientID) throws JMSException {
    checkClosedOrFailed();

    if (explicitClientID) {
        throw new IllegalStateException("The clientID has already been set");
    }
    if (clientID == null || clientID.isEmpty()) {
        throw new InvalidClientIDException("Cannot have a null or empty clientID");
    }
    if (connected.get()) {
        throw new IllegalStateException("Cannot set the client id once connected.");
    }

    setClientID(clientID, true);

    // We weren't connected if we got this far, we should now connect to ensure the
    // configured clientID is valid.
    initialize();
}
 
示例7
void deleteTemporaryDestination(MockJMSTemporaryDestination destination) throws JMSException {
    checkClosedOrFailed();

    try {
        for (MockJMSSession session : sessions.values()) {
            if (session.isDestinationInUse(destination)) {
                throw new IllegalStateException("A consumer is consuming from the temporary destination");
            }
        }

        signalDeleteTemporaryDestination(destination);
        stats.temporaryDestinationDestroyed(destination);
        tempDestinations.remove(destination);
    } catch (Exception e) {
        throw JMSExceptionSupport.create(e);
    }
}
 
示例8
@Test
public void testGetQueue() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    QueueReceiver receiver = session.createReceiver(queue);

    assertNotNull(receiver.getQueue());
    assertSame(queue, receiver.getQueue());

    receiver.close();

    try {
        receiver.getQueue();
        fail("Cannot read topic on closed receiver");
    } catch (IllegalStateException ise) {}
}
 
示例9
@Test
public void testGetTopicSubscriber() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    JmsPoolQueueReceiver receiver = (JmsPoolQueueReceiver) session.createReceiver(queue);

    assertNotNull(receiver.getQueueReceiver());
    assertTrue(receiver.getQueueReceiver() instanceof MockJMSQueueReceiver);

    receiver.close();

    try {
        receiver.getQueueReceiver();
        fail("Cannot read state on closed receiver");
    } catch (IllegalStateException ise) {}
}
 
示例10
@Test
public void testGetTopic() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
    TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTemporaryTopic();
    TopicSubscriber subscriber = session.createSubscriber(topic);

    assertNotNull(subscriber.getTopic());
    assertSame(topic, subscriber.getTopic());

    subscriber.close();

    try {
        subscriber.getTopic();
        fail("Cannot read topic on closed subscriber");
    } catch (IllegalStateException ise) {}
}
 
示例11
@Test
public void testGetTopicSubscriber() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
    TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTemporaryTopic();
    JmsPoolTopicSubscriber subscriber = (JmsPoolTopicSubscriber) session.createDurableSubscriber(topic, "name", "color = red", true);

    assertNotNull(subscriber.getTopicSubscriber());
    assertTrue(subscriber.getTopicSubscriber() instanceof MockJMSTopicSubscriber);

    subscriber.close();

    try {
        subscriber.getTopicSubscriber();
        fail("Cannot read state on closed subscriber");
    } catch (IllegalStateException ise) {}
}
 
示例12
@Test(timeout = 60000)
public void testClose() throws Exception {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    assertEquals(0, connection.getNumtIdleSessions());
    session.close();
    assertEquals(1, connection.getNumtIdleSessions());

    try {
        session.close();
    } catch (JMSException ex) {
        fail("Shouldn't fail on second close call.");
    }

    try {
        session.createTemporaryQueue();
        fail("Session should be closed.");
    } catch (IllegalStateException ise) {}
}
 
示例13
@Test
public void testGetQueue() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    QueueSender sender = session.createSender(queue);

    assertNotNull(sender.getQueue());
    assertSame(queue, sender.getQueue());

    sender.close();

    try {
        sender.getQueue();
        fail("Cannot read topic on closed sender");
    } catch (IllegalStateException ise) {}
}
 
示例14
@Test
public void testGetTopicSubscriber() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    JmsPoolQueueSender sender = (JmsPoolQueueSender) session.createSender(queue);

    assertNotNull(sender.getQueueSender());
    assertTrue(sender.getQueueSender() instanceof MockJMSQueueSender);

    sender.close();

    try {
        sender.getQueueSender();
        fail("Cannot read state on closed sender");
    } catch (IllegalStateException ise) {}
}
 
示例15
@Test
public void testReceive() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNull(consumer.receive());

    consumer.close();

    try {
        consumer.receive();
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
示例16
@Test
public void testReceiveNoWait() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNull(consumer.receiveNoWait());

    consumer.close();

    try {
        consumer.receiveNoWait();
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
示例17
@Test
public void testReceiveTimed() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNull(consumer.receive(1));

    consumer.close();

    try {
        consumer.receive(1);
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
示例18
@Test
public void testGetMessageSelector() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNotNull(consumer.getMessageSelector());
    assertEquals("Color = Red", consumer.getMessageSelector());

    consumer.close();

    try {
        consumer.getMessageSelector();
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
示例19
@Test(timeout = 60000)
public void testSetClientIDTwiceWithSameID() throws Exception {
    Connection connection = cf.createConnection();

    // test: call setClientID("newID") twice
    // this should be tolerated and not result in an exception
    connection.setClientID("newID");

    try {
        connection.setClientID("newID");
        connection.start();
        connection.close();
    } catch (IllegalStateException 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.");
}
 
示例20
/**
 * Create a XA queue session
 *
 * @return The XA queue session
 * @throws JMSException Thrown if an error occurs
 */
@Override
public XAQueueSession createXAQueueSession() throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("createXAQueueSession()");
   }

   checkClosed();

   if (type == ActiveMQRAConnectionFactory.CONNECTION || type == ActiveMQRAConnectionFactory.TOPIC_CONNECTION ||
      type == ActiveMQRAConnectionFactory.XA_TOPIC_CONNECTION) {
      throw new IllegalStateException("Can not get a topic session from a queue connection");
   }

   return allocateConnection(type);
}
 
示例21
@Test(timeout = 60000)
public void testSetClientIDAfterConnectionStart() throws Exception {
    Connection connection = cf.createConnection();

    // test: try to call setClientID() after start()
    // should result in an exception
    try {
        connection.start();
        connection.setClientID("newID3");
        fail("Calling setClientID() after start() mut raise a JMSException.");
    } catch (IllegalStateException ise) {
        LOG.debug("Correctly received " + ise);
    } finally {
        connection.close();
        cf.stop();
    }

    LOG.debug("Test finished.");
}
 
示例22
@Test
public void testGetQueue() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    QueueBrowser browser = session.createBrowser(queue);

    assertNotNull(browser.getQueue());

    browser.close();
    browser.close();

    try {
        browser.getQueue();
        fail("Should not be able to use a closed browser");
    } catch (IllegalStateException ise) {
    }
}
 
示例23
@Test
public void testGetQueueBrowser() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    JmsPoolQueueBrowser browser = (JmsPoolQueueBrowser) session.createBrowser(queue);

    assertNotNull(browser.getQueueBrowser());

    browser.close();

    try {
        browser.getQueueBrowser();
        fail("Should not be able to use a closed browser");
    } catch (IllegalStateException ise) {
    }
}
 
示例24
@Test
public void testGetMessageSelector() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    QueueBrowser browser = session.createBrowser(queue, "color = red");

    assertNotNull(browser.getMessageSelector());
    assertEquals("color = red", browser.getMessageSelector());

    browser.close();

    try {
        browser.getMessageSelector();
        fail("Should not be able to use a closed browser");
    } catch (IllegalStateException ise) {
    }
}
 
示例25
/**
 * Rollback
 *
 * @throws JMSException Failed to close session.
 */
@Override
public void rollback() throws JMSException {
   if (cri.getType() == ActiveMQRAConnectionFactory.XA_CONNECTION || cri.getType() == ActiveMQRAConnectionFactory.XA_QUEUE_CONNECTION ||
      cri.getType() == ActiveMQRAConnectionFactory.XA_TOPIC_CONNECTION) {
      throw new TransactionInProgressException("XA connection");
   }

   lock();
   try {
      Session session = getSessionInternal();

      if (cri.isTransacted() == false) {
         throw new IllegalStateException("Session is not transacted");
      }

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("Rollback session " + this);
      }

      session.rollback();
   } finally {
      unlock();
   }
}
 
示例26
/**
 * Unsubscribe
 *
 * @param name The name
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void unsubscribe(final String name) throws JMSException {
   if (cri.getType() == ActiveMQRAConnectionFactory.QUEUE_CONNECTION || cri.getType() == ActiveMQRAConnectionFactory.XA_QUEUE_CONNECTION) {
      throw new IllegalStateException("Cannot unsubscribe for javax.jms.QueueSession");
   }

   lock();
   try {
      Session session = getSessionInternal();

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("unsubscribe " + session + " name=" + name);
      }

      session.unsubscribe(name);
   } finally {
      unlock();
   }
}
 
示例27
/**
 * Create a XA topic session
 *
 * @return The XA topic session
 * @throws JMSException Thrown if an error occurs
 */
@Override
public XATopicSession createXATopicSession() throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("createXATopicSession()");
   }

   checkClosed();

   if (type == ActiveMQRAConnectionFactory.CONNECTION || type == ActiveMQRAConnectionFactory.QUEUE_CONNECTION ||
      type == ActiveMQRAConnectionFactory.XA_QUEUE_CONNECTION) {
      throw new IllegalStateException("Can not get a topic session from a queue connection");
   }

   return allocateConnection(type);
}
 
示例28
/**
 * Get the XA resource and ensure that it is open
 *
 * @return The XA Resource
 * @throws JMSException          Thrown if an error occurs
 * @throws IllegalStateException The session is closed
 */
XAResource getXAResourceInternal() throws JMSException {
   if (mc == null) {
      throw new IllegalStateException("The session is closed");
   }

   try {
      XAResource xares = mc.getXAResource();

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("getXAResourceInternal " + xares + " for " + this);
      }

      return xares;
   } catch (ResourceException e) {
      JMSException jmse = new JMSException("Unable to get XA Resource");
      jmse.initCause(e);
      throw jmse;
   }
}
 
示例29
@Test(timeout = 60000)
public void testSetClientIDAfterConnectionStart() throws Exception {
    LOG.debug("running testRepeatedSetClientIDCalls()");

    ConnectionFactory cf = createPooledConnectionFactory();
    Connection conn = cf.createConnection();

    // test: try to call setClientID() after start()
    // should result in an exception
    try {
        conn.start();
        conn.setClientID("newID3");
        fail("Calling setClientID() after start() must raise a JMSException.");
    } catch (IllegalStateException ise) {
        LOG.debug("Correctly received " + ise);
    } finally {
        conn.close();
        ((JmsPoolConnectionFactory) cf).stop();
    }

    LOG.debug("Test finished.");
}
 
示例30
@Test(timeout = 60000)
public void testSetClientIDTwiceWithSameID() throws Exception {
    // test: call setClientID("newID") twice
    // this should be tolerated and not result in an exception
    Connection connection = cf.createConnection();
    connection.setClientID("newID");

    try {
        connection.setClientID("newID");
        connection.start();
        connection.close();
    } catch (IllegalStateException 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.");
}