@Override
  protected void setUp() throws Exception {
    executor = Executors.newFixedThreadPool(20);

    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
    connection = (ActiveMQConnection) factory.createConnection();
    connection.start();
  }
Ejemplo n.º 2
0
  public static void main(String[] args) throws JMSException {

    String user = env("ACTIVEMQ_USER", "");
    String password = env("ACTIVEMQ_PASSWORD", "");
    String host = env("ACTIVEMQ_HOST", "localhost");
    int port = Integer.parseInt(env("ACTIVEMQ_PORT", "61616"));
    String destination = arg(args, 0, "foo.bar");

    int messages = 10;
    int size = 256;

    String DATA = "abcdefghijklmnopqrstuvwxyz";
    String body = "";
    for (int i = 0; i < size; i++) {
      body += DATA.charAt(i % DATA.length());
    }

    ActiveMQConnectionFactory factory =
        new ActiveMQConnectionFactory(
            "tcp://" + host + ":" + port + "?jms.useAsyncSend=false&jms.alwaysSyncSend=true");

    Connection connection = factory.createConnection(user, password);
    logger.info("Connection created");
    connection.start();
    logger.info("Connection started");
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination dest = new ActiveMQQueue(destination);
    MessageProducer producer = session.createProducer(dest);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

    for (int i = 1; i <= messages; i++) {
      TextMessage msg = session.createTextMessage(body);
      msg.setIntProperty("id", i);
      producer.send(msg);
      logger.info("Message send" + i);
    }

    producer.send(session.createTextMessage("THE.END"));
    connection.close();
    logger.info("Connection closed");
  }
Ejemplo n.º 3
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 doTestDLQAfterBlock(ActiveMQDestination destination) throws Exception {
    ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) createConnectionFactory();
    RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
    // Immediately sent to the DLQ on rollback, no redelivery
    redeliveryPolicy.setMaximumRedeliveries(0);
    factory.setRedeliveryPolicy(redeliveryPolicy);

    // Separate connection for consumer so it will not be blocked by filler thread
    // sending when it blocks
    connection = (ActiveMQConnection) factory.createConnection();
    connections.add(connection);
    connection.setClientID("someId");
    connection.start();

    final Session consumerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
    MessageConsumer consumer =
        destination.isQueue()
            ? consumerSession.createConsumer(destination)
            : consumerSession.createDurableSubscriber((Topic) destination, "Durable");

    connection = (ActiveMQConnection) factory.createConnection();
    connections.add(connection);
    connection.start();

    final Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    final MessageProducer producer = session.createProducer(destination);

    final AtomicBoolean done = new AtomicBoolean(true);
    final AtomicBoolean keepGoing = new AtomicBoolean(true);
    final CountDownLatch fillerStarted = new CountDownLatch(1);

    final AtomicLong sent = new AtomicLong(0);
    Thread thread =
        new Thread("Filler") {
          int i;

          @Override
          public void run() {
            while (keepGoing.get()) {
              done.set(false);
              fillerStarted.countDown();
              try {
                producer.send(session.createTextMessage(oneKb + ++i));
                if (i % 10 == 0) {
                  session.commit();
                  sent.getAndAdd(10);
                  LOG.info("committed/sent: " + sent.get());
                }
                LOG.info("sent: " + i);
              } catch (JMSException e) {
              }
            }
          }
        };
    thread.start();

    assertTrue("filler started..", fillerStarted.await(20, TimeUnit.SECONDS));
    waitForBlocked(done);

    // consume and rollback some so message gets to DLQ
    connection = (ActiveMQConnection) factory.createConnection();
    connections.add(connection);
    connection.start();
    TextMessage msg;
    int received = 0;
    for (; received < sent.get(); ++received) {
      msg = (TextMessage) consumer.receive(4000);
      if (msg == null) {
        LOG.info("received null on count: " + received);
        break;
      }
      LOG.info("received: " + received + ", msg: " + msg.getJMSMessageID());
      if (received % 5 == 0) {
        if (received % 3 == 0) {
          // force the use of the DLQ which will use some more store
          LOG.info("rollback on : " + received);
          consumerSession.rollback();
        } else {
          LOG.info("commit on : " + received);
          consumerSession.commit();
        }
      }
    }
    LOG.info("Done:: sent: " + sent.get() + ", received: " + received);
    keepGoing.set(false);
    assertTrue("some were sent:", sent.get() > 0);
    assertEquals("received what was committed", sent.get(), received);
  }