Exemplo n.º 1
0
    private synchronized QueueConnection newQueueConnection() throws Exception {

      if (connectionFactory == null) {
        connectionFactory = new ActiveMQConnectionFactory(amqUser, amqPassword, activemqURL);
      }

      // Set the redelivery count to -1 (infinite), or else messages will start dropping
      // after the queue has had a certain number of failures (default is 6)
      RedeliveryPolicy policy = connectionFactory.getRedeliveryPolicy();
      policy.setMaximumRedeliveries(-1);

      QueueConnection amqConnection = connectionFactory.createQueueConnection();
      amqConnection.start();
      return amqConnection;
    }
  public static void main(String[] args) throws Exception {
    ActiveMQConnectionFactory connectionFactory =
        new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616");
    QueueConnection queueConnection = connectionFactory.createQueueConnection();

    QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue("test-queue");

    MessageProducer producer = queueSession.createProducer(queue);

    queueConnection.start();

    TextMessage textMessage =
        queueSession.createTextMessage("Current time: " + System.currentTimeMillis());

    producer.send(textMessage);
    log.info("Message sent at {}", System.currentTimeMillis());

    producer.close();
    queueSession.close();
    queueConnection.close();
  }