예제 #1
0
  @Test
  public void testReceiveMessageTimeout() throws JMSException {
    JmsMessageReceiver receiver = new JmsMessageReceiver();
    receiver.setConnectionFactory(connectionFactory);

    receiver.setDestination(destination);

    reset(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    expect(connectionFactory.createConnection()).andReturn(connection).once();
    expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session).once();
    expect(session.getTransacted()).andReturn(false).once();
    expect(session.getAcknowledgeMode()).andReturn(Session.AUTO_ACKNOWLEDGE).once();

    expect(session.createConsumer(destination, null)).andReturn(messageConsumer).once();

    expect(messageConsumer.receive(5000L)).andReturn(null).once();

    connection.start();
    expectLastCall().once();

    replay(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    try {
      receiver.receive();
    } catch (ActionTimeoutException e) {
      Assert.assertTrue(
          e.getMessage().startsWith("Action timed out while receiving JMS message on"));
      return;
    }

    Assert.fail(
        "Missing " + CitrusRuntimeException.class + " because of receiveing message timeout");
  }
예제 #2
0
  @Test
  public void testWithMessageSelectorAndCustomTimeout() throws JMSException {
    JmsMessageReceiver receiver = new JmsMessageReceiver();
    receiver.setConnectionFactory(connectionFactory);

    receiver.setDestination(destination);
    receiver.setReceiveTimeout(10000L);

    Map<String, Object> controlHeaders = new HashMap<String, Object>();
    final Message<String> controlMessage =
        MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>")
            .copyHeaders(controlHeaders)
            .build();

    Map<String, String> headers = new HashMap<String, String>();

    reset(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    expect(connectionFactory.createConnection()).andReturn(connection).once();
    expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session).once();
    expect(session.getTransacted()).andReturn(false).once();
    expect(session.getAcknowledgeMode()).andReturn(Session.AUTO_ACKNOWLEDGE).once();

    expect(session.createConsumer(destination, "Operation = 'sayHello'"))
        .andReturn(messageConsumer)
        .once();

    connection.start();
    expectLastCall().once();

    expect(messageConsumer.receive(10000L))
        .andReturn(
            new TextMessageImpl(
                "<TestRequest><Message>Hello World!</Message></TestRequest>", headers))
        .once();

    replay(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    Message<?> receivedMessage = receiver.receiveSelected("Operation = 'sayHello'");
    Assert.assertEquals(receivedMessage.getPayload(), controlMessage.getPayload());

    verify(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);
  }
  /** Test that close() hierarchically closes all child objects */
  public void testCloseHierarchy() throws Exception {
    Connection conn = cf.createConnection();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = sess.createConsumer(topic1);
    MessageProducer producer = sess.createProducer(topic1);
    sess.createBrowser(queue1);
    Message m = sess.createMessage();

    conn.close();

    // Session

    /* If the session is closed then any method invocation apart from close()
     * will throw an IllegalStateException
     */
    try {
      sess.createMessage();
      fail("Session is not closed");
    } catch (javax.jms.IllegalStateException e) {
    }

    try {
      sess.getAcknowledgeMode();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      sess.getTransacted();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      sess.getMessageListener();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      sess.createProducer(queue1);
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      sess.createConsumer(queue1);
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    // Producer

    /* If the producer is closed then any method invocation apart from close()
     * will throw an IllegalStateException
     */
    try {
      producer.send(m);
      fail("Producer is not closed");
    } catch (javax.jms.IllegalStateException e) {
    }

    try {
      producer.getDisableMessageID();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      producer.getPriority();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      producer.getDestination();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      producer.getTimeToLive();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    // ClientConsumer

    try {
      consumer.getMessageSelector();
      fail("should throw exception");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      consumer.getMessageListener();
      fail("should throw exception");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      consumer.receive();
      fail("should throw exception");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    // Browser

  }