@Test
  public void testReconnectMultipleTimesWithSameClientID() throws Exception {
    try {
      sameIdConnection = (ActiveMQConnection) this.factory.createConnection();
      useConnection(sameIdConnection);

      // now lets create another which should fail
      for (int i = 1; i < 11; i++) {
        Connection connection2 = this.factory.createConnection();
        try {
          useConnection(connection2);
          fail("Should have thrown InvalidClientIDException on attempt" + i);
        } catch (InvalidClientIDException e) {
          System.err.println("Caught expected: " + e);
        } finally {
          connection2.close();
        }
      }

      // now lets try closing the original connection and creating a new
      // connection with the same ID
      sameIdConnection.close();
      sameIdConnection = (ActiveMQConnection) factory.createConnection();
      useConnection(connection);
    } finally {
      if (sameIdConnection != null) {
        sameIdConnection.close();
      }
    }
  }
 @After
 public void tearDown() throws Exception {
   if (sameIdConnection != null) {
     sameIdConnection.close();
     sameIdConnection = null;
   }
   super.tearDown();
 }
 @After
 public void stopBroker() throws Exception {
   if (connection != null) {
     connection.close();
   }
   if (broker != null) {
     broker.stop();
   }
 }
Пример #4
0
  public void disconnect() throws JMSException {

    if (connection != null) {

      messageProducer.close();
      connection.stop();
      connection.close();
      System.out.println("disconnect");
    }
  }
Пример #5
0
 private void sendMessage(int timeToLive) throws Exception {
   ActiveMQConnection producerConnection = (ActiveMQConnection) createConnection();
   producerConnection.start();
   Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageProducer producer = producerSession.createProducer(destination);
   if (timeToLive > 0) {
     producer.setTimeToLive(timeToLive);
   }
   Message message = producerSession.createMessage();
   message.setStringProperty("data", data);
   producer.send(message);
   producerConnection.close();
 }
Пример #6
0
  public void doTestScheduledRedelivery(int maxBrokerRedeliveriesToValidate, boolean validateDLQ)
      throws Exception {

    startBroker(true);
    sendMessage(0);

    ActiveMQConnection consumerConnection = (ActiveMQConnection) createConnection();
    RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
    redeliveryPolicy.setInitialRedeliveryDelay(0);
    redeliveryPolicy.setMaximumRedeliveries(0);
    consumerConnection.setRedeliveryPolicy(redeliveryPolicy);
    consumerConnection.start();
    Session consumerSession = consumerConnection.createSession(true, Session.SESSION_TRANSACTED);
    MessageConsumer consumer = consumerSession.createConsumer(destination);
    Message message = consumer.receive(1000);
    assertNotNull("got message", message);
    LOG.info("got: " + message);
    consumerSession.rollback();

    for (int i = 0; i < maxBrokerRedeliveriesToValidate; i++) {
      Message shouldBeNull = consumer.receive(500);
      assertNull(
          "did not get message after redelivery count exceeded: " + shouldBeNull, shouldBeNull);

      TimeUnit.SECONDS.sleep(4);

      Message brokerRedeliveryMessage = consumer.receive(1500);
      LOG.info("got: " + brokerRedeliveryMessage);
      assertNotNull("got message via broker redelivery after delay", brokerRedeliveryMessage);
      assertEquals(
          "message matches",
          message.getStringProperty("data"),
          brokerRedeliveryMessage.getStringProperty("data"));
      assertEquals(
          "has expiryDelay specified - iteration:" + i,
          i == 0 ? initialRedeliveryDelayMillis : redeliveryDelayMillis,
          brokerRedeliveryMessage.getLongProperty(RedeliveryPlugin.REDELIVERY_DELAY));

      consumerSession.rollback();
    }

    if (validateDLQ) {
      MessageConsumer dlqConsumer =
          consumerSession.createConsumer(
              new ActiveMQQueue(SharedDeadLetterStrategy.DEFAULT_DEAD_LETTER_QUEUE_NAME));
      Message dlqMessage = dlqConsumer.receive(2000);
      assertNotNull("Got message from dql", dlqMessage);
      assertEquals(
          "message matches",
          message.getStringProperty("data"),
          dlqMessage.getStringProperty("data"));
      consumerSession.commit();
    } else {
      // consume/commit ok
      message = consumer.receive(3000);
      assertNotNull("got message", message);
      assertEquals(
          "redeliveries accounted for",
          maxBrokerRedeliveriesToValidate + 2,
          message.getLongProperty("JMSXDeliveryCount"));
      consumerSession.commit();
    }

    consumerConnection.close();
  }
  @SuppressWarnings("unchecked")
  @Test
  public void testPrefetchZeroConsumerThroughRestart() throws Exception {
    broker = createBroker(true);

    final CountDownLatch pullDone = new CountDownLatch(1);
    broker.setPlugins(
        new BrokerPlugin[] {
          new BrokerPluginSupport() {
            @Override
            public Response messagePull(ConnectionContext context, final MessagePull pull)
                throws Exception {
              context.setDontSendReponse(true);
              pullDone.countDown();
              Executors.newSingleThreadExecutor()
                  .execute(
                      new Runnable() {
                        @Override
                        public void run() {
                          LOG.info("Stopping broker on pull: " + pull);
                          try {
                            broker.stop();
                          } catch (Exception e) {
                            e.printStackTrace();
                          }
                        }
                      });
              return null;
            }
          }
        });
    broker.start();

    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
    cf.setWatchTopicAdvisories(false);

    final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
    connection.start();

    final Session consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final Queue destination =
        consumerSession.createQueue(QUEUE_NAME + "?consumer.prefetchSize=" + prefetch);

    final MessageConsumer consumer = consumerSession.createConsumer(destination);
    produceMessage(consumerSession, destination, 1);

    final CountDownLatch receiveDone = new CountDownLatch(1);
    final Vector<Message> received = new Vector<>();
    Executors.newSingleThreadExecutor()
        .execute(
            new Runnable() {
              @Override
              public void run() {
                try {
                  LOG.info("receive one...");
                  Message msg = consumer.receive(30000);
                  if (msg != null) {
                    received.add(msg);
                  }
                  receiveDone.countDown();
                  LOG.info("done receive");
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            });

    // will be stopped by the plugin
    assertTrue("pull completed on broker", pullDone.await(30, TimeUnit.SECONDS));
    broker.waitUntilStopped();
    broker = createBroker(false, url);
    broker.start();

    assertTrue("receive completed through failover", receiveDone.await(30, TimeUnit.SECONDS));

    assertTrue("we got our message:", !received.isEmpty());

    connection.close();
  }