示例#1
3
  public void process(String[] arg) {
    if (arg.length != 13 && arg.length != 14) {
      System.out.println("Invalid number of arguments! " + arg.length);
      printUsage();
      System.exit(-1);
    }

    String sourceHost;
    String sourceUser;
    String sourcePass;
    int sourcePort;
    String queue;

    String targetHost;
    int targetPort;
    String targetUser;
    String targetPassword;
    String producingAddress;

    int commit;

    int waitTimeout;

    String filter = null;

    try {
      sourceHost = arg[1];
      sourcePort = Integer.parseInt(arg[2]);
      sourceUser = arg[3];
      sourcePass = arg[4];
      queue = arg[5];

      targetHost = arg[6];
      targetPort = Integer.parseInt(arg[7]);
      targetUser = arg[8];
      targetPassword = arg[9];
      producingAddress = arg[10];

      waitTimeout = Integer.parseInt(arg[11]);
      commit = Integer.parseInt(arg[12]);

      if (arg.length == 14) {
        filter = arg[13];
      }
    } catch (Exception e) {
      e.printStackTrace();
      printUsage();
      System.exit(-1);
      return; // the compiler doesn't understand exit as leaving the VM
    }

    Map<String, Object> sourceParameters = new HashMap<String, Object>();
    sourceParameters.put(TransportConstants.HOST_PROP_NAME, sourceHost);
    sourceParameters.put(TransportConstants.PORT_PROP_NAME, sourcePort);

    Map<String, Object> targetParameters = new HashMap<String, Object>();
    sourceParameters.put(TransportConstants.HOST_PROP_NAME, targetHost);
    sourceParameters.put(TransportConstants.PORT_PROP_NAME, targetPort);

    try {
      TransportConfiguration configurationSource =
          new TransportConfiguration(NettyConnectorFactory.class.getName(), sourceParameters);

      ServerLocator locatorSource = ActiveMQClient.createServerLocator(false, configurationSource);

      ClientSessionFactory factorySource = locatorSource.createSessionFactory();

      ClientSession sessionSource =
          factorySource.createSession(sourceUser, sourcePass, false, false, false, false, 0);

      ClientConsumer consumer;

      if (filter == null) {
        consumer = sessionSource.createConsumer(queue);
      } else {
        consumer = sessionSource.createConsumer(queue, filter);
      }

      TransportConfiguration configurationTarget =
          new TransportConfiguration(NettyConnectorFactory.class.getName(), targetParameters);

      ServerLocator locatorTarget =
          ActiveMQClient.createServerLocatorWithoutHA(configurationTarget);

      ClientSessionFactory factoryTarget = locatorTarget.createSessionFactory();

      ClientSession sessionTarget =
          factoryTarget.createSession(targetUser, targetPassword, false, false, false, false, 0);

      ClientProducer producer = sessionTarget.createProducer(producingAddress);

      sessionSource.start();

      int countMessage = 0;

      while (true) {
        ClientMessage message = consumer.receive(waitTimeout);
        if (message == null) {
          break;
        }

        message.acknowledge();

        if (!message.containsProperty("_HQ_TOOL_original_address")) {
          message.putStringProperty("_HQ_TOOL_original_address", message.getAddress().toString());
        }

        LinkedList<String> listToRemove = new LinkedList<String>();

        for (SimpleString name : message.getPropertyNames()) {
          if (name.toString().startsWith("_HQ_ROUTE_TO")) {
            listToRemove.add(name.toString());
          }
        }

        for (String str : listToRemove) {
          message.removeProperty(str);
        }

        producer.send(message);

        if (countMessage++ % commit == 0) {
          System.out.println("Sent " + countMessage + " messages");
          sessionTarget.commit();
          sessionSource.commit();
        }
      }

      sessionTarget.commit();
      sessionSource.commit();
      consumer.close();
      producer.close();

      sessionSource.close();
      sessionTarget.close();

      locatorSource.close();
      locatorTarget.close();

    } catch (Exception e) {
      e.printStackTrace();
      printUsage();
      System.exit(-1);
    }
  }
 @Test
 public void testNoConnection() throws Exception {
   ServerLocator locator =
       ActiveMQClient.createServerLocatorWithHA(
           new TransportConfiguration(NettyConnectorFactory.class.getName()));
   try {
     createSessionFactory(locator);
     fail();
   } catch (ActiveMQNotConnectedException nce) {
     // ok
   } catch (ActiveMQException e) {
     fail("Invalid Exception type:" + e.getType());
   }
 }