Java Code Examples for org.apache.activemq.ActiveMQConnection#start()

The following examples show how to use org.apache.activemq.ActiveMQConnection#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JmsProxyImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Runs until (re)connected. Should only be called by one thread. Use
 * startReconnectThread to run in another thread (only one thread will be
 * started however many calls are made).
 * <p>
 * Listeners are notified of connection once the connection is reestablished
 * and all topic subscriptions are back.
 */
private synchronized void connect() {
  while (!connected && !shutdownRequested) {
    try {
      connection = (ActiveMQConnection) jmsConnectionFactory.createConnection();
      connection.start();
      connection.addTransportListener((ActiveMQTransportListener) this::startReconnectThread);
      refreshSubscriptions();
      connected = true;
    } catch (Exception e) {
      log.error("Exception caught while trying to refresh the JMS connection; sleeping 5s before retrying.", e);
      try {
        wait(SLEEP_BETWEEN_CONNECTION_ATTEMPTS);
      } catch (InterruptedException interEx) {
        log.error("InterruptedException caught while waiting to reconnect.", interEx);
      }
    }
  }
  if (connected) {
    notifyConnectionListenerOnConnection();
  }
}
 
Example 2
Source File: AbortSlowAckConsumer0Test.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testZeroPrefetchConsumerIsAborted() throws Exception {
   strategy.setMaxTimeSinceLastAck(2000); // Make it shorter

   ActiveMQConnection conn = (ActiveMQConnection) createConnectionFactory().createConnection();
   conn.setExceptionListener(this);
   connections.add(conn);

   Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
   final MessageConsumer consumer = sess.createConsumer(destination);
   assertNotNull(consumer);
   conn.start();
   startProducers(destination, 20);

   Message message = consumer.receive(5000);
   assertNotNull(message);

   TimeUnit.SECONDS.sleep(15);

   try {
      consumer.receive(5000);
      fail("Slow consumer not aborted.");
   } catch (Exception ex) {
   }
}
 
Example 3
Source File: BrokerRedeliveryTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testNoScheduledRedeliveryOfExpired() throws Exception {
   startBroker(true);
   ActiveMQConnection consumerConnection = (ActiveMQConnection) createConnection();
   consumerConnection.start();
   Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
   MessageConsumer consumer = consumerSession.createConsumer(destination);
   sendMessage(1500);
   Message message = consumer.receive(1000);
   assertNotNull("got message", message);

   // ensure there is another consumer to redispatch to
   MessageConsumer redeliverConsumer = consumerSession.createConsumer(destination);

   // allow consumed to expire so it gets redelivered
   TimeUnit.SECONDS.sleep(2);
   consumer.close();

   // should go to dlq as it has expired
   // validate DLQ
   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"));
}
 
Example 4
Source File: DurableSubscriptionSelectorTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void openConsumer() throws Exception {
   consumerConnection = (ActiveMQConnection) createConnection();
   consumerConnection.setClientID("cliID");
   consumerConnection.start();
   Session session = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   TopicSubscriber subscriber = session.createDurableSubscriber(topic, "subName", "filter=true", false);

   subscriber.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(Message message) {
         received++;
      }
   });
}
 
Example 5
Source File: FailoverComplexClusterTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected void createClients(int numOfClients) throws Exception {
   ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(clientUrl);
   for (int i = 0; i < numOfClients; i++) {
      ActiveMQConnection c = (ActiveMQConnection) factory.createConnection();
      c.start();
      Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue queue = s.createQueue(getClass().getName());
      MessageConsumer consumer = s.createConsumer(queue);
      connections.add(c);
   }
}
 
Example 6
Source File: FailoverConsumerOutstandingCommitTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private Message receiveMessage(ActiveMQConnectionFactory cf, Queue destination) throws Exception {
   final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
   connection.start();
   final Session consumerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
   final MessageConsumer consumer = consumerSession.createConsumer(destination);
   Message msg = consumer.receive(5000);
   consumerSession.commit();
   connection.close();
   return msg;
}
 
Example 7
Source File: DurableSubscriberNonPersistentMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public Consumer(String brokerURL, String interest, String clientId) throws JMSException {
   factory = new ActiveMQConnectionFactory(brokerURL);
   connection = (ActiveMQConnection) factory.createConnection();
   connection.setClientID(clientId);
   connection.start();
   connection.getPrefetchPolicy().setAll(15);
   session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Destination destination = session.createTopic(interest);
   messageConsumer = session.createDurableSubscriber((Topic) destination, "MyDurableTopic");
}
 
Example 8
Source File: MessageCompressionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private ActiveMQMapMessage receiveTestMapMessage(ActiveMQConnectionFactory factory) throws JMSException {
   ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
   connection.start();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageConsumer consumer = session.createConsumer(queue);
   ActiveMQMapMessage rc = (ActiveMQMapMessage) consumer.receive();
   connection.close();
   return rc;
}
 
Example 9
Source File: TwoBrokerFailoverClusterTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected void createClients(int numOfClients) throws Exception {
   ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(clientUrl);
   for (int i = 0; i < numOfClients; i++) {
      ActiveMQConnection c = (ActiveMQConnection) factory.createConnection();
      c.start();
      Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue queue = s.createQueue(getClass().getName());
      MessageConsumer consumer = s.createConsumer(queue);
      connections.add(c);
   }
}
 
Example 10
Source File: MessageCompressionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private ActiveMQObjectMessage receiveTestObjectMessage(ActiveMQConnectionFactory factory) throws JMSException {
   ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
   connection.start();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageConsumer consumer = session.createConsumer(queue);
   ActiveMQObjectMessage rc = (ActiveMQObjectMessage) consumer.receive();
   connection.close();
   return rc;
}
 
Example 11
Source File: BrokerRedeliveryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 12
Source File: MessageCompressionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private ActiveMQStreamMessage receiveTestStreamMessage(ActiveMQConnectionFactory factory) throws JMSException {
   ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
   connection.start();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageConsumer consumer = session.createConsumer(queue);
   ActiveMQStreamMessage rc = (ActiveMQStreamMessage) consumer.receive();
   connection.close();
   return rc;
}
 
Example 13
Source File: BrokerRedeliveryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
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(3);

         Message brokerRedeliveryMessage = consumer.receive(500);
         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", 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();
   }
 
Example 14
Source File: DurablePersistentFalseRestartTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void testValidateNoPersistenceForDurableAfterRestart() throws Exception {

      ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("failover:(" + broker.getTransportConnectors().get(0).getPublishableConnectString() + ")");
      ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
      connection.setClientID("clientId");
      connection.start();

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

      Topic destination = session.createTopic(queueName);
      MessageConsumer consumer = session.createDurableSubscriber(destination, "subscriberName");

      populateDestination(10, destination, connection);

      restartBroker();

      // make failover aware of the restarted auto assigned port
      connection.getTransport().narrow(FailoverTransport.class).add(true, broker.getTransportConnectors().get(0).getPublishableConnectString());

      TextMessage msg = (TextMessage) consumer.receive(4000);
      assertNull("did not get a message when persistent=false, message: " + msg, msg);

      connection.close();
   }
 
Example 15
Source File: NonBlockingConsumerRedeliveryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testNonBlockingMessageDeleiveryIsDelayed() throws Exception {
   final LinkedHashSet<Message> received = new LinkedHashSet<>();

   ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
   connection.getRedeliveryPolicy().setInitialRedeliveryDelay(TimeUnit.SECONDS.toMillis(6));
   Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
   Destination destination = session.createQueue(destinationName);
   MessageConsumer consumer = session.createConsumer(destination);

   consumer.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(Message message) {
         received.add(message);
      }
   });

   sendMessages();
   connection.start();

   assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
      @Override
      public boolean isSatisified() throws Exception {
         LOG.info("Consumer has received " + received.size() + " messages.");
         return received.size() == MSG_COUNT;
      }
   }));

   received.clear();
   session.rollback();

   assertFalse("Delayed redelivery test not expecting any messages yet.", Wait.waitFor(new Wait.Condition() {
      @Override
      public boolean isSatisified() throws Exception {
         return received.size() > 0;
      }
   }, TimeUnit.SECONDS.toMillis(4)));

   session.commit();
   session.close();
}
 
Example 16
Source File: FailoverConsumerOutstandingCommitTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testRollbackFailoverConsumerTx() throws Exception {
   server = createBroker();
   server.start();

   ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
   cf.setConsumerFailoverRedeliveryWaitPeriod(10000);
   final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
   connection.start();

   final Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   final Queue destination = producerSession.createQueue(QUEUE_NAME);

   final Session consumerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
   final MessageConsumer testConsumer = consumerSession.createConsumer(destination);
   assertNull("no message yet", testConsumer.receiveNoWait());

   produceMessage(producerSession, destination, 1);
   producerSession.close();

   // consume then rollback after restart
   Message msg = testConsumer.receive(5000);
   assertNotNull(msg);

   // restart with outstanding delivered message
   server.stop();
   server = createBroker();
   server.start();

   consumerSession.rollback();

   // receive again
   msg = testConsumer.receive(10000);
   assertNotNull("got message again after rollback", msg);

   consumerSession.commit();

   // close before sweep
   consumerSession.close();
   msg = receiveMessage(cf, destination);
   assertNull("should be nothing left after commit", msg);
   connection.close();
}
 
Example 17
Source File: TwoBrokerTempQueueAdvisoryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void testSendToRemovedTemp() throws Exception {

      ActiveMQQueue requestReplyDest = new ActiveMQQueue("RequestReply");

      NetworkConnector nc = bridgeBrokers("BrokerA", "BrokerB");
      if (useDuplex) {
         nc.setDuplex(true);
      } else {
         bridgeBrokers("BrokerB", "BrokerA");
      }

      // destination advisory can loose the race with message dispatch, so we need to allow replies on network broker
      // to work in the absence of an advisory, the destination will be cleaned up in the normal
      // way
      if (!useDuplex) {
         brokers.get("BrokerB").broker.setAllowTempAutoCreationOnSend(true);
      }

      TransportConnector forClient = brokers.get("BrokerA").broker.addConnector("tcp://localhost:0");
      startAllBrokers();
      waitForBridgeFormation();
      waitForMinTopicRegionConsumerCount("BrokerB", 1);
      waitForMinTopicRegionConsumerCount("BrokerA", 1);

      ConnectionFactory factory = new ActiveMQConnectionFactory(forClient.getConnectUri());
      ActiveMQConnection conn = (ActiveMQConnection) factory.createConnection();
      conn.setWatchTopicAdvisories(false);
      conn.start();
      Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      ConnectionFactory replyFactory = getConnectionFactory("BrokerB");
      for (int i = 0; i < 500; i++) {
         TemporaryQueue tempDest = session.createTemporaryQueue();
         MessageProducer producer = session.createProducer(requestReplyDest);
         javax.jms.Message message = session.createTextMessage("req-" + i);
         message.setJMSReplyTo(tempDest);

         ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(tempDest);
         producer.send(message);

         ActiveMQConnection replyConnection = (ActiveMQConnection) replyFactory.createConnection();
         replyConnection.setWatchTopicAdvisories(false);
         replyConnection.start();
         Session replySession = replyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         ActiveMQMessageConsumer replyConsumer = (ActiveMQMessageConsumer) replySession.createConsumer(requestReplyDest);
         javax.jms.Message msg = replyConsumer.receive(10000);
         assertNotNull("request message not null: " + i, msg);
         MessageProducer replyProducer = replySession.createProducer(msg.getJMSReplyTo());
         replyProducer.send(session.createTextMessage("reply-" + i));
         replyConnection.close();

         javax.jms.Message reply = consumer.receive(10000);
         assertNotNull("reply message : " + i + ", to: " + tempDest + ", by consumer:" + consumer.getConsumerId(), reply);
         consumer.close();
         tempDest.delete();
      }
   }
 
Example 18
Source File: TwoSecureBrokerRequestReplyTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void testRequestReply() throws Exception {
   ActiveMQQueue requestReplyDest = new ActiveMQQueue("RequestReply");

   startAllBrokers();
   waitForBridgeFormation();
   waitForMinTopicRegionConsumerCount("sender", 1);
   waitForMinTopicRegionConsumerCount("receiver", 1);

   ConnectionFactory factory = getConnectionFactory("sender");
   ActiveMQConnection conn = (ActiveMQConnection) factory.createConnection("system", "manager");
   conn.setWatchTopicAdvisories(false);
   conn.start();
   Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   ConnectionFactory replyFactory = getConnectionFactory("receiver");
   for (int i = 0; i < 2000; i++) {
      TemporaryQueue tempDest = session.createTemporaryQueue();
      MessageProducer producer = session.createProducer(requestReplyDest);
      javax.jms.Message message = session.createTextMessage("req-" + i);
      message.setJMSReplyTo(tempDest);

      ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(tempDest);
      producer.send(message);

      ActiveMQConnection replyConnection = (ActiveMQConnection) replyFactory.createConnection("system", "manager");
      replyConnection.setWatchTopicAdvisories(false);
      replyConnection.start();
      Session replySession = replyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      ActiveMQMessageConsumer replyConsumer = (ActiveMQMessageConsumer) replySession.createConsumer(requestReplyDest);
      javax.jms.Message msg = replyConsumer.receive(10000);
      assertNotNull("request message not null: " + i, msg);
      MessageProducer replyProducer = replySession.createProducer(msg.getJMSReplyTo());
      replyProducer.send(session.createTextMessage("reply-" + i));
      replyConnection.close();

      javax.jms.Message reply = consumer.receive(10000);
      assertNotNull("reply message : " + i + ", to: " + tempDest + ", by consumer:" + consumer.getConsumerId(), reply);
      consumer.close();
      tempDest.delete();
      LOG.info("message #" + i + " processed");
   }

}
 
Example 19
Source File: RequestReplyNoAdvisoryNetworkTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private ActiveMQConnection createConnection(ActiveMQConnectionFactory factory) throws Exception {
   ActiveMQConnection c = (ActiveMQConnection) factory.createConnection();
   c.start();
   return c;
}
 
Example 20
Source File: MQTTOpenwireTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void doTestSendJMSReceiveMQTT(String jmsDestination, String mqttTopic) throws Exception {
   final MQTTClientProvider provider = getMQTTClientProvider();
   initializeConnection(provider);

   ActiveMQConnection activeMQConnection = (ActiveMQConnection) cf.createConnection();
   try {
      activeMQConnection.setUseRetroactiveConsumer(true);
      activeMQConnection.start();
      Session s = activeMQConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      javax.jms.Topic jmsTopic = s.createTopic(jmsDestination);
      MessageProducer producer = s.createProducer(jmsTopic);

      final String RETAINED = "RETAINED";
      provider.subscribe(mqttTopic, AT_MOST_ONCE);

      // send retained message from JMS
      TextMessage sendMessage = s.createTextMessage(RETAINED);
      // mark the message to be retained
      sendMessage.setBooleanProperty("ActiveMQ.Retain", true);
      // MQTT QoS can be set using MQTTProtocolConverter.QOS_PROPERTY_NAME property
      sendMessage.setIntProperty("ActiveMQ.MQTT.QoS", 0);
      producer.send(sendMessage);

      byte[] message = provider.receive(2000);
      assertNotNull("Should get retained message " + jmsDestination + "->" + mqttTopic, message);
      assertEquals(RETAINED, new String(message));

      for (int i = 0; i < 1; i++) {
         String payload = "This is Test Message: " + i;
         sendMessage = s.createTextMessage(payload);
         producer.send(sendMessage);
         message = provider.receive(1000);
         assertNotNull("Should get a message " + jmsDestination + "->" + mqttTopic, message);

         assertEquals(payload, new String(message));
      }
   } finally {
      activeMQConnection.close();
      provider.disconnect();
   }
}