Ejemplo n.º 1
0
  @Override
  public int run() throws Exception {
    // Default values of command line arguments
    String host = DEFAULT_HOST;
    int port = DEFAULT_PORT;
    String user = DEFAULT_USER;
    String pass = DEFAULT_PASS;
    String destination = DEFAULT_DESTINATION;
    String file = ""; // No default -- if not given, don't read/write file
    int sleep = 0;
    boolean showpercent = false;
    int batchSize = 0;
    int length = 500; // Length of message generated internally
    String properties = "";
    String format = "short";
    String durable = null;

    int n = 1; // n is the number of messages to process, or a specific
    //   message number, depending on content
    String url = ""; // No default -- if not given, don't use it

    String[] nonSwitchArgs = cl.getArgs();
    if (nonSwitchArgs.length > 0) n = Integer.parseInt(nonSwitchArgs[0]);

    String _destination = cl.getOptionValue("destination");
    if (_destination != null) destination = _destination;

    String _host = cl.getOptionValue("host");
    if (_host != null) host = _host;

    String _port = cl.getOptionValue("port");
    if (_port != null) port = Integer.parseInt(_port);

    String _file = cl.getOptionValue("file");
    if (_file != null) file = _file;

    String _user = cl.getOptionValue("user");
    if (_user != null) user = _user;

    String _pass = cl.getOptionValue("password");
    if (_pass != null) pass = _pass;

    String _url = cl.getOptionValue("url");
    if (_url != null) url = _url;

    String _sleep = cl.getOptionValue("sleep");
    if (_sleep != null) sleep = Integer.parseInt(_sleep);

    if (cl.hasOption("percent")) showpercent = true;

    String _batchSize = cl.getOptionValue("batch");
    if (_batchSize != null) batchSize = Integer.parseInt(_batchSize);

    String _L = cl.getOptionValue("length");
    if (_L != null) length = Integer.parseInt(_L);

    String _properties = cl.getOptionValue("properties");
    if (_properties != null) properties = _properties;

    String _durable = cl.getOptionValue("durable");
    if (_durable != null) durable = _durable;

    boolean batch = false;
    if (batchSize != 0) batch = true;

    String _format = cl.getOptionValue("format");
    if (_format != null) format = _format;

    ActiveMQConnectionFactory factory = getFactory(host, port, url);

    Connection connection = factory.createConnection(user, pass);

    if (durable != null) connection.setClientID(durable);

    connection.start();

    Session session = connection.createSession(batch, Session.AUTO_ACKNOWLEDGE);

    Topic topic = session.createTopic(destination);

    MessageConsumer consumer = null;
    if (durable != null) consumer = session.createDurableSubscriber(topic, "amqutil");
    else consumer = session.createConsumer(topic);

    int oldpercent = 0;
    for (int i = 0; i < n; i++) {
      javax.jms.Message message = consumer.receive();

      if (batch) if ((i + 1) % batchSize == 0) session.commit();

      if (sleep != 0) Thread.sleep(sleep);

      JMSUtil.outputMessage(format, message, file);

      if (showpercent) {
        int percent = i * 100 / n;
        if (percent != oldpercent) System.out.println("" + percent + "%");
        oldpercent = percent;
      }
    }

    if (batch) session.commit();

    connection.close();

    return 0;
  }
  public void testSelectorsAndNonSelectors() throws Exception {
    clearSelectorCacheFiles();
    // borkerA is local and brokerB is remote
    bridgeAndConfigureBrokers("BrokerA", "BrokerB");
    startAllBrokers();
    waitForBridgeFormation();

    final BrokerService brokerA = brokers.get("BrokerA").broker;
    final BrokerService brokerB = brokers.get("BrokerB").broker;

    // Create the remote virtual topic consumer with selector
    ActiveMQDestination consumerBQueue =
        createDestination("Consumer.B.VirtualTopic.tempTopic", false);

    MessageConsumer selectingConsumer = createConsumer("BrokerB", consumerBQueue, "foo = 'bar'");
    MessageConsumer nonSelectingConsumer = createConsumer("BrokerB", consumerBQueue);

    // let advisories propogate
    Wait.waitFor(
        new Wait.Condition() {
          Destination dest =
              brokerA.getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"));

          @Override
          public boolean isSatisified() throws Exception {
            return dest.getConsumers().size() == 2;
          }
        },
        500);

    Destination destination = getDestination(brokerB, consumerBQueue);
    assertEquals(2, destination.getConsumers().size());

    // publisher publishes to this
    ActiveMQTopic virtualTopic = new ActiveMQTopic("VirtualTopic.tempTopic");
    sendMessages("BrokerA", virtualTopic, 10, asMap("foo", "bar"));
    sendMessages("BrokerA", virtualTopic, 10);

    MessageIdList selectingConsumerMessages = getConsumerMessages("BrokerB", selectingConsumer);

    MessageIdList nonSelectingConsumerMessages =
        getConsumerMessages("BrokerB", nonSelectingConsumer);

    // we only expect half of the messages that get sent with the selector, because they get load
    // balanced
    selectingConsumerMessages.waitForMessagesToArrive(5, 1000L);
    assertEquals(5, selectingConsumerMessages.getMessageCount());

    nonSelectingConsumerMessages.waitForMessagesToArrive(15, 1000L);
    assertEquals(15, nonSelectingConsumerMessages.getMessageCount());

    // assert broker A stats
    waitForMessagesToBeConsumed(brokerA, "Consumer.B.VirtualTopic.tempTopic", false, 20, 20, 5000);
    assertEquals(
        20,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        20,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        0,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());

    // assert broker B stats
    waitForMessagesToBeConsumed(brokerB, "Consumer.B.VirtualTopic.tempTopic", false, 20, 20, 5000);
    assertEquals(
        20,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        20,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        0,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());

    // now let's close the consumer without the selector
    nonSelectingConsumer.close();

    // let advisories propogate
    Wait.waitFor(
        new Wait.Condition() {
          Destination dest =
              brokerA.getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"));

          @Override
          public boolean isSatisified() throws Exception {
            return dest.getConsumers().size() == 1;
          }
        },
        500);

    // and let's send messages with a selector that doesnt' match
    selectingConsumerMessages.flushMessages();

    sendMessages("BrokerA", virtualTopic, 10, asMap("ceposta", "redhat"));

    selectingConsumerMessages = getConsumerMessages("BrokerB", selectingConsumer);
    selectingConsumerMessages.waitForMessagesToArrive(1, 1000L);
    assertEquals(0, selectingConsumerMessages.getMessageCount());

    // assert broker A stats
    waitForMessagesToBeConsumed(brokerA, "Consumer.B.VirtualTopic.tempTopic", false, 20, 20, 5000);
    assertEquals(
        20,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        20,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        0,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());

    // assert broker B stats
    waitForMessagesToBeConsumed(brokerB, "Consumer.B.VirtualTopic.tempTopic", false, 20, 20, 5000);
    assertEquals(
        20,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        20,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        0,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());

    // now lets disconect the selecting consumer for a sec and send messages with a selector that
    // DOES match
    selectingConsumer.close();

    // let advisories propogate
    Wait.waitFor(
        new Wait.Condition() {
          Destination dest =
              brokerA.getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"));

          @Override
          public boolean isSatisified() throws Exception {
            return dest.getConsumers().size() == 0;
          }
        },
        500);

    selectingConsumerMessages.flushMessages();

    sendMessages("BrokerA", virtualTopic, 10, asMap("foo", "bar"));

    // assert broker A stats
    waitForMessagesToBeConsumed(brokerA, "Consumer.B.VirtualTopic.tempTopic", false, 20, 20, 5000);
    assertEquals(
        30,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        20,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        10,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());

    // assert broker B stats
    waitForMessagesToBeConsumed(brokerB, "Consumer.B.VirtualTopic.tempTopic", false, 20, 20, 5000);
    assertEquals(
        20,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        20,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        0,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());

    selectingConsumer = createConsumer("BrokerB", consumerBQueue, "foo = 'bar'");
    selectingConsumerMessages = getConsumerMessages("BrokerB", selectingConsumer);
    selectingConsumerMessages.waitForMessagesToArrive(10);
    assertEquals(10, selectingConsumerMessages.getMessageCount());

    // let advisories propogate
    Wait.waitFor(
        new Wait.Condition() {
          Destination dest =
              brokerA.getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"));

          @Override
          public boolean isSatisified() throws Exception {
            return dest.getConsumers().size() == 1;
          }
        },
        500);

    // assert broker A stats
    waitForMessagesToBeConsumed(brokerA, "Consumer.B.VirtualTopic.tempTopic", false, 30, 30, 5000);
    assertEquals(
        30,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        30,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        0,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());

    // assert broker B stats
    waitForMessagesToBeConsumed(brokerB, "Consumer.B.VirtualTopic.tempTopic", false, 30, 30, 5000);
    assertEquals(
        30,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        30,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        0,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());
  }
  public void testSelectorAwareForwarding() throws Exception {
    clearSelectorCacheFiles();
    // borkerA is local and brokerB is remote
    bridgeAndConfigureBrokers("BrokerA", "BrokerB");
    startAllBrokers();
    waitForBridgeFormation();

    final BrokerService brokerB = brokers.get("BrokerB").broker;
    final BrokerService brokerA = brokers.get("BrokerA").broker;

    // Create the remote virtual topic consumer with selector
    MessageConsumer remoteConsumer =
        createConsumer(
            "BrokerB",
            createDestination("Consumer.B.VirtualTopic.tempTopic", false),
            "foo = 'bar'");

    // let advisories propogate
    Wait.waitFor(
        new Wait.Condition() {
          Destination dest =
              brokerA.getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"));

          @Override
          public boolean isSatisified() throws Exception {
            return dest.getConsumers().size() == 1;
          }
        },
        500);

    ActiveMQQueue queueB = new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic");
    Destination destination = getDestination(brokers.get("BrokerB").broker, queueB);
    assertEquals(1, destination.getConsumers().size());

    ActiveMQTopic virtualTopic = new ActiveMQTopic("VirtualTopic.tempTopic");
    assertNull(getDestination(brokers.get("BrokerA").broker, virtualTopic));
    assertNull(getDestination(brokers.get("BrokerB").broker, virtualTopic));

    // send two types of messages, one unwanted and the other wanted
    sendMessages("BrokerA", virtualTopic, 1, asMap("foo", "bar"));
    sendMessages("BrokerA", virtualTopic, 1, asMap("ceposta", "redhat"));

    MessageIdList msgsB = getConsumerMessages("BrokerB", remoteConsumer);
    // wait for the wanted one to arrive at the remote consumer
    msgsB.waitForMessagesToArrive(1);

    // ensure we don't get any more messages
    msgsB.waitForMessagesToArrive(1, 1000);

    // remote consumer should only get one of the messages
    assertEquals(1, msgsB.getMessageCount());

    // and the enqueue count for the remote queue should only be 1
    assertEquals(
        1,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());

    // now let's remove the consumer on broker B and recreate it with new selector
    remoteConsumer.close();

    // now let's shut down broker A and clear its persistent selector cache
    brokerA.stop();
    brokerA.waitUntilStopped();
    deleteSelectorCacheFile("BrokerA");

    assertEquals(0, destination.getConsumers().size());

    remoteConsumer =
        createConsumer(
            "BrokerB",
            createDestination("Consumer.B.VirtualTopic.tempTopic", false),
            "ceposta = 'redhat'");

    assertEquals(1, destination.getConsumers().size());

    // now let's start broker A back up
    brokerA.start(true);
    brokerA.waitUntilStarted();

    System.out.println(brokerA.getNetworkConnectors());

    // give a sec to let advisories propogate
    // let advisories propogate
    Wait.waitFor(
        new Wait.Condition() {
          Destination dest =
              brokerA.getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"));

          @Override
          public boolean isSatisified() throws Exception {
            return dest.getConsumers().size() == 1;
          }
        },
        500);

    // send two types of messages, one unwanted and the other wanted
    sendMessages("BrokerA", virtualTopic, 1, asMap("foo", "bar"));
    sendMessages("BrokerB", virtualTopic, 1, asMap("foo", "bar"));
    sendMessages("BrokerA", virtualTopic, 1, asMap("ceposta", "redhat"));
    sendMessages("BrokerB", virtualTopic, 1, asMap("ceposta", "redhat"));

    // lets get messages on consumer B
    msgsB = getConsumerMessages("BrokerB", remoteConsumer);
    msgsB.waitForMessagesToArrive(2);

    // ensure we don't get any more messages
    msgsB.waitForMessagesToArrive(1, 1000);

    // remote consumer should only get 10 of the messages
    assertEquals(2, msgsB.getMessageCount());

    // queue should be drained
    assertEquals(
        0,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());
    // and the enqueue count for the remote queue should only be 1
    assertEquals(
        3,
        brokerB
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
  }
  public void testSelectorConsumptionWithNoMatchAtHeadOfQueue() throws Exception {
    clearSelectorCacheFiles();
    startAllBrokers();

    BrokerService brokerA = brokers.get("BrokerA").broker;

    // Create the remote virtual topic consumer with selector
    ActiveMQDestination consumerQueue =
        createDestination("Consumer.B.VirtualTopic.tempTopic", false);

    // create it so that the queue is there and messages don't get lost
    MessageConsumer selectingConsumer = establishConsumer("BrokerA", consumerQueue);

    // send messages with NO selection criteria first, and then with a property to be selected
    // this should put messages at the head of the queue that don't match selection
    ActiveMQTopic virtualTopic = new ActiveMQTopic("VirtualTopic.tempTopic");
    sendMessages("BrokerA", virtualTopic, 1);

    // close the consumer w/out consuming any messages; they'll be marked redelivered
    selectingConsumer.close();

    selectingConsumer = createConsumer("BrokerA", consumerQueue, "foo = 'bar'");

    sendMessages("BrokerA", virtualTopic, 1, asMap("foo", "bar"));

    MessageIdList selectingConsumerMessages = getConsumerMessages("BrokerA", selectingConsumer);
    selectingConsumerMessages.waitForMessagesToArrive(1, 1000L);

    assertEquals(1, selectingConsumerMessages.getMessageCount());
    selectingConsumerMessages.waitForMessagesToArrive(10, 1000L);
    assertEquals(1, selectingConsumerMessages.getMessageCount());

    // assert broker A stats
    waitForMessagesToBeConsumed(brokerA, "Consumer.B.VirtualTopic.tempTopic", false, 2, 1, 5000);
    assertEquals(
        1,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getConsumers()
            .size());
    assertEquals(
        2,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getEnqueues()
            .getCount());
    assertEquals(
        1,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getDequeues()
            .getCount());
    assertEquals(
        1,
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount());
  }
  public void testMessageLeaks() throws Exception {
    clearSelectorCacheFiles();
    startAllBrokers();

    final BrokerService brokerA = brokers.get("BrokerA").broker;

    // Create the remote virtual topic consumer with selector
    ActiveMQDestination consumerQueue =
        createDestination("Consumer.B.VirtualTopic.tempTopic", false);
    // create it so that the queue is there and messages don't get lost
    MessageConsumer consumer1 = createConsumer("BrokerA", consumerQueue, "SYMBOL = 'AAPL'");
    MessageConsumer consumer2 = createConsumer("BrokerA", consumerQueue, "SYMBOL = 'AAPL'");

    ActiveMQTopic virtualTopic = new ActiveMQTopic("VirtualTopic.tempTopic");
    ProducerThreadTester producerTester = createProducerTester("BrokerA", virtualTopic);
    producerTester.setRunIndefinitely(true);
    producerTester.setSleep(5);
    producerTester.addMessageProperty("AAPL");
    producerTester.addMessageProperty("VIX");
    producerTester.start();

    int currentCount = producerTester.getSentCount();
    LOG.info(
        ">>>> currently sent: total="
            + currentCount
            + ", AAPL="
            + producerTester.getCountForProperty("AAPL")
            + ", VIX="
            + producerTester.getCountForProperty("VIX"));

    // let some messages get sent
    Thread.sleep(2000);

    MessageIdList consumer1Messages = getConsumerMessages("BrokerA", consumer1);
    consumer1Messages.waitForMessagesToArrive(50, 1000);

    // switch one of the consumers to SYMBOL = 'VIX'
    consumer1.close();
    consumer1 = createConsumer("BrokerA", consumerQueue, "SYMBOL = 'VIX'");

    // wait till new consumer is on board
    Wait.waitFor(
        new Wait.Condition() {
          @Override
          public boolean isSatisified() throws Exception {
            return brokerA
                    .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
                    .getConsumers()
                    .size()
                == 2;
          }
        });

    currentCount = producerTester.getSentCount();
    LOG.info(
        ">>>> currently sent: total="
            + currentCount
            + ", AAPL="
            + producerTester.getCountForProperty("AAPL")
            + ", VIX="
            + producerTester.getCountForProperty("VIX"));

    // let some messages get sent
    Thread.sleep(2000);

    // switch the other consumer to SYMBOL = 'VIX'
    consumer2.close();
    consumer2 = createConsumer("BrokerA", consumerQueue, "SYMBOL = 'VIX'");

    // wait till new consumer is on board
    Wait.waitFor(
        new Wait.Condition() {
          @Override
          public boolean isSatisified() throws Exception {
            return brokerA
                    .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
                    .getConsumers()
                    .size()
                == 2;
          }
        });

    currentCount = producerTester.getSentCount();
    LOG.info(
        ">>>> currently sent: total="
            + currentCount
            + ", AAPL="
            + producerTester.getCountForProperty("AAPL")
            + ", VIX="
            + producerTester.getCountForProperty("VIX"));

    // let some messages get sent
    Thread.sleep(2000);

    currentCount = producerTester.getSentCount();
    LOG.info(
        ">>>> currently sent: total="
            + currentCount
            + ", AAPL="
            + producerTester.getCountForProperty("AAPL")
            + ", VIX="
            + producerTester.getCountForProperty("VIX"));

    // make sure if there are messages that are orphaned in the queue that this number doesn't
    // grow...
    final long currentDepth =
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount();

    LOG.info(">>>>> Orphaned messages? " + currentDepth);

    // wait 5s to see if we can get a growth in the depth of the queue
    Wait.waitFor(
        new Wait.Condition() {
          @Override
          public boolean isSatisified() throws Exception {
            return brokerA
                    .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
                    .getDestinationStatistics()
                    .getMessages()
                    .getCount()
                > currentDepth;
          }
        },
        5000);

    // stop producers
    producerTester.setRunning(false);
    producerTester.join();

    // pause to let consumers catch up
    Thread.sleep(1000);

    assertTrue(
        brokerA
                .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
                .getDestinationStatistics()
                .getMessages()
                .getCount()
            <= currentDepth);
  }
  @Test
  public void genericEmergencyProcedureTest()
      throws HornetQException, InterruptedException, IOException, ClassNotFoundException {

    asynchProcedureStartWorker =
        new MessageConsumerWorker(
            "asyncProcedureStartCoreServer",
            new MessageConsumerWorkerHandler<AsyncProcedureStartMessage>() {

              @Override
              public void handleMessage(AsyncProcedureStartMessage message) {
                System.out.println(
                    ">>>>>>>>>>>Creating a new Procedure = " + message.getProcedureName());
                try {
                  ProceduresMGMTService.getInstance()
                      .newRequestedProcedure(
                          message.getEmergencyId(),
                          message.getProcedureName(),
                          message.getParameters());
                } catch (IOException ex) {
                  Logger.getLogger(GenericEmergencyProcedureTest.class.getName())
                      .log(Level.SEVERE, null, ex);
                }
              }
            });

    asynchProcedureStartWorker.start();

    MessageProducer producer = MessageFactory.createMessageProducer();
    Call initialCall = new Call(1, 2, new Date());

    producer.sendMessage(initialCall);
    producer.stop();

    Call call = (Call) consumer.receiveMessage();
    assertNotNull(call);

    GenericEmergencyProcedureImpl.getInstance().newPhoneCall(call);

    Thread.sleep(5000);

    doOperatorTask();

    // QUERY TO SEE THAT WE HAVE AN EMERGENCY ATTACHED TO THE CALL

    Thread.sleep(2000);

    doControlTask();

    Thread.sleep(6000);
    // I should have one task here, that has been created by the specific procedure started
    doGarageTask();

    Thread.sleep(3000);
    // I can asume that all the procedures are ended, we need to delegate this to the external
    // component
    AllProceduresEndedEvent allProceduresEndedEvent =
        new AllProceduresEndedEvent(null, new ArrayList<String>());
    GenericEmergencyProcedureImpl.getInstance()
        .allProceduresEnededNotification(allProceduresEndedEvent);
    // We should see the report in the console
    Thread.sleep(10000);
  }
Ejemplo n.º 7
0
  @Test
  public void testFetchLast() {
    try {

      String neId =
          "1005255"; // KLNNMS02(cpuusage=YES cpu1min=YES memutil=YES) KLNNMS05(cpuusage=YES
                     // bususage=YES)
      // String neId = "1006119";    // KLNNMS02(cpuusage=YES cpu1min=YES memutil=YES
      // KLNNMS05(cpuusage=NO bususage=NO)

      Set<String> rras = new HashSet<String>();
      // klnnms02
      rras.add("cpu5sec");
      rras.add("cpu1min");
      rras.add("memutil");

      // klnnms05
      rras.add("cpuusage");
      rras.add("bususage");

      FetchLastCommandMessage message =
          CommandMessageFactory.createRRDLastCommandMessage(neId, "AVERAGE", 0, 0, null, rras);

      MessageProducer producer = null;
      MessageConsumer consumer = null;

      // time to send the JMS request
      try {
        TextMessage reqMsg, replyMsg;

        producer = session.createProducer(new HornetQQueue(SERVICE_QUEUE));

        // this will uniquelly identify the request
        String UIID = UUID.randomUUID().toString();

        reqMsg = session.createTextMessage();
        reqMsg.setStringProperty("ServiceRRD_msg_type", "fetchLast");
        reqMsg.setStringProperty("ServiceRRD_correlation_id", UIID);

        String body = JsonUtil.getInstance().toJSON(message);

        reqMsg.setText(body);

        logger.info("SEND:\n" + body);

        producer.send(reqMsg);

        consumer =
            session.createConsumer(
                new HornetQQueue(SERVICE_REPLY_QUEUE),
                "ServiceRRD_correlation_id = '" + UIID + "'");

        replyMsg = (TextMessage) consumer.receive(30000);

        if (replyMsg == null) {
          logger.info("ServiceRRD timeout on receive()");
        } else {
          logger.info("REPLY:\n" + replyMsg.getText());
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
          if (producer != null) producer.close();
          if (consumer != null) consumer.close();
        } catch (JMSException e) {
        }
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Ejemplo n.º 8
0
  // @Test
  public void testFetchGraphSimple() {
    try {

      String neId = "21100799";
      String group = "bw"; // interesting for the BW

      String titleX = "Bandwidth";
      String titleY = "bps";
      int timespan = 0; // Daily

      /*
      String neId = "1005255";
      String group = "cpu";    // interesting for the CPU

      String titleX = "CPU Utilization";
      String titleY = "Utilization";
      int timespan = 0;   // Daily
                */
      FetchGraphSimpleCommandMessage message =
          CommandMessageFactory.createRRDGraphSimpleCommandMessage(
              neId, group, timespan, titleX, titleY);

      MessageProducer producer = null;
      MessageConsumer consumer = null;

      try {
        // time to send the JMS request
        TextMessage reqMsg;
        Message replyMsg;

        producer = session.createProducer(new HornetQQueue(SERVICE_QUEUE));

        // this will uniquelly identify the request
        String UIID = UUID.randomUUID().toString();

        reqMsg = session.createTextMessage();
        reqMsg.setStringProperty("ServiceRRD_msg_type", "fetchGraphSimple");
        reqMsg.setStringProperty("ServiceRRD_correlation_id", UIID);

        String body = JsonUtil.getInstance().toJSON(message);

        reqMsg.setText(body);

        logger.info("SEND:\n" + body);

        producer.send(reqMsg);

        consumer =
            session.createConsumer(
                new HornetQQueue(SERVICE_REPLY_QUEUE),
                "ServiceRRD_correlation_id = '" + UIID + "'");

        replyMsg = consumer.receive(30000);

        if (replyMsg == null) {
          logger.info("ServiceRRD timeout on receive()");
        } else {
          if (replyMsg instanceof BytesMessage) {

            BytesMessage graphStream = (BytesMessage) replyMsg;

            byte[] graph = new byte[(int) graphStream.getBodyLength()];
            graphStream.readBytes(graph);

            FileOutputStream image =
                new FileOutputStream(
                    "/Users/cvasilak/Temp/svc-rrd-images/"
                        + neId
                        + "_"
                        + group
                        + "_"
                        + timespan
                        + ".png");

            image.write(graph);
            image.close();

            logger.info("image retrieved and saved!");

          } else if (replyMsg instanceof TextMessage) { // the server responded with an error
            logger.info(((TextMessage) replyMsg).getText());
          }
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
          if (producer != null) producer.close();
          if (consumer != null) consumer.close();
        } catch (JMSException e) {
        }
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }