Java Code Examples for org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory#close()

The following examples show how to use org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory#close() . 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: CliTestBase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
void closeConnection(ActiveMQConnectionFactory cf, Connection connection) throws Exception {
   try {
      connection.close();
      cf.close();
   } finally {
      stopServer();
   }
}
 
Example 2
Source File: InitialConnectionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitialInfinite() throws Exception {
   AtomicInteger errors = new AtomicInteger(0);

   ActiveMQServer server = createServer(false, true);

   Thread t = new Thread() {
      @Override
      public void run() {
         try {
            Thread.sleep(500);
            server.start();
         } catch (Throwable e) {
            e.printStackTrace();
            errors.incrementAndGet();
         }
      }
   };
   t.start();
   ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("(tcp://localhost:61618,tcp://localhost:61616,tcp://localhost:61610)?ha=true&retryInterval=100&retryIntervalMultiplier=1.0&reconnectAttempts=-1&initialConnectAttempts=-1&useTopologyForLoadBalancing=true");
   connectionFactory.createConnection().close();
   connectionFactory.close();


   t.join();

   Assert.assertEquals(0, errors.get());
}
 
Example 3
Source File: ActiveMQConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticConnectorLiveConstructor() throws Exception {
   ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, liveTC);
   assertFactoryParams(cf, new TransportConfiguration[]{liveTC}, null, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
   Connection conn = cf.createConnection();

   conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   testSettersThrowException(cf);

   cf.close();

   conn.close();
}
 
Example 4
Source File: SessionMetadataAddExceptionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5000)
public void testDuplicateClientIdSet() throws Exception {
   ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) cf;
   Connection con = cf.createConnection();
   Connection con2 = cf.createConnection();
   try {
      con.setClientID("valid");
      con2.setClientID("valid");
      fail("Should have failed for duplicate clientId");
   } catch (InvalidClientIDException e) {
      assertEquals(1, duplicateCount.get());
   } finally {
      activeMQConnectionFactory.close();
   }
}
 
Example 5
Source File: SessionMetadataAddExceptionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5000, expected = JMSException.class)
public void testDuplicateClientIdSetActiveMQException() throws Exception {
   ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) cf;
   Connection con = cf.createConnection();
   Connection con2 = cf.createConnection();
   try {
      con.setClientID("valid2");
      con2.setClientID("valid2");
      fail("Should have failed");
   } finally {
      activeMQConnectionFactory.close();
   }
}
 
Example 6
Source File: ConnectionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void testThroughNewConnectionFactory(ActiveMQConnectionFactory factory) throws Exception {
   Connection conn = factory.createConnection();
   conn.close();

   try (JMSContext ctx = factory.createContext()) {
      ctx.createProducer().send(ctx.createQueue("queue"), "Test");
   }

   try (JMSContext ctx = factory.createContext()) {
      Assert.assertNotNull(ctx.createConsumer(ctx.createQueue("queue")).receiveNoWait());
      Assert.assertNull(ctx.createConsumer(ctx.createQueue("queue")).receiveNoWait());
   }

   factory.close();
}
 
Example 7
Source File: HornetQProtocolManagerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/** This test will use an ArtemisConnectionFactory with clientProtocolManager=*/
@Test
public void testLegacy2() throws Exception {
   // WORKAROUND: the 2.0.0 broker introduced addressing change and the 2.2.0 broker added compatibility for old
   // client libraries relying on the legacy prefixes. The new client being used in this test needs prefix explicitly.
   Queue queue = new ActiveMQQueue("jms.queue.testQueue");
   ActiveMQConnectionFactory connectionFactory = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616?protocolManagerFactoryStr=" + HornetQClientProtocolManagerFactory.class.getName(), "legacy");
   Connection connection = connectionFactory.createConnection();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageProducer producer = session.createProducer(queue);

   TextMessage message = session.createTextMessage("Test");
   for (int i = 0; i < 5; i++) {
      message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), "duplicate");
      producer.send(message);
   }

   connection.start();
   MessageConsumer consumer = session.createConsumer(queue);
   TextMessage messageRec = (TextMessage) consumer.receive(5000);
   Assert.assertNotNull(messageRec);

   Assert.assertEquals("Test", messageRec.getText());
   Assert.assertNull(consumer.receiveNoWait());
   connection.close();
   connectionFactory.close();

}
 
Example 8
Source File: MessageGroupExample.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
   final Map<String, String> messageReceiverMap = new ConcurrentHashMap<>();
   Connection connection = null;
   try {

      // Step 2. Perform a lookup on the queue
      Queue queue = ActiveMQJMSClient.createQueue("queue/exampleQueue");

      // Step 3. Perform a lookup on the Connection Factory
      ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();

      // Step 4. Create a JMS Connection
      connection = cf.createConnection();

      // Step 5. Create a JMS Session
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 6. Create a JMS Message Producer
      MessageProducer producer = session.createProducer(queue);

      // Step 7. Create two consumers
      MessageConsumer consumer1 = session.createConsumer(queue);
      consumer1.setMessageListener(new SimpleMessageListener("consumer-1", messageReceiverMap));
      MessageConsumer consumer2 = session.createConsumer(queue);
      consumer2.setMessageListener(new SimpleMessageListener("consumer-2", messageReceiverMap));

      // Step 8. Create and send 10 text messages with group id 'Group-0'
      int msgCount = 10;
      TextMessage[] groupMessages = new TextMessage[msgCount];
      for (int i = 0; i < msgCount; i++) {
         groupMessages[i] = session.createTextMessage("Group-0 message " + i);
         groupMessages[i].setStringProperty("JMSXGroupID", "Group-0");
         producer.send(groupMessages[i]);
         System.out.println("Sent message: " + groupMessages[i].getText());
      }

      System.out.println("all messages are sent");

      // Step 9. Start the connection
      connection.start();

      Thread.sleep(2000);

      // Step 10. check the group messages are received by only one consumer
      String trueReceiver = messageReceiverMap.get(groupMessages[0].getText());
      for (TextMessage grpMsg : groupMessages) {
         String receiver = messageReceiverMap.get(grpMsg.getText());
         if (!trueReceiver.equals(receiver)) {
            throw new IllegalStateException("Group message [" + grpMsg.getText() + "[ went to wrong receiver: " + receiver);
         }
      }

      cf.close();
   } finally {
      // Step 11. Be sure to close our JMS resources!
      if (connection != null) {
         connection.close();
      }
   }
}
 
Example 9
Source File: ActiveMQConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testGettersAndSetters() {
   ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, liveTC);

   long clientFailureCheckPeriod = RandomUtil.randomPositiveLong();
   long connectionTTL = RandomUtil.randomPositiveLong();
   long callTimeout = RandomUtil.randomPositiveLong();
   int minLargeMessageSize = RandomUtil.randomPositiveInt();
   int consumerWindowSize = RandomUtil.randomPositiveInt();
   int consumerMaxRate = RandomUtil.randomPositiveInt();
   int confirmationWindowSize = RandomUtil.randomPositiveInt();
   int producerMaxRate = RandomUtil.randomPositiveInt();
   boolean blockOnAcknowledge = RandomUtil.randomBoolean();
   boolean blockOnDurableSend = RandomUtil.randomBoolean();
   boolean blockOnNonDurableSend = RandomUtil.randomBoolean();
   boolean autoGroup = RandomUtil.randomBoolean();
   boolean preAcknowledge = RandomUtil.randomBoolean();
   String loadBalancingPolicyClassName = RandomUtil.randomString();
   boolean useGlobalPools = RandomUtil.randomBoolean();
   int scheduledThreadPoolMaxSize = RandomUtil.randomPositiveInt();
   int threadPoolMaxSize = RandomUtil.randomPositiveInt();
   long retryInterval = RandomUtil.randomPositiveLong();
   double retryIntervalMultiplier = RandomUtil.randomDouble();
   int reconnectAttempts = RandomUtil.randomPositiveInt();
   cf.setClientFailureCheckPeriod(clientFailureCheckPeriod);
   cf.setConnectionTTL(connectionTTL);
   cf.setCallTimeout(callTimeout);
   cf.setMinLargeMessageSize(minLargeMessageSize);
   cf.setConsumerWindowSize(consumerWindowSize);
   cf.setConsumerMaxRate(consumerMaxRate);
   cf.setConfirmationWindowSize(confirmationWindowSize);
   cf.setProducerMaxRate(producerMaxRate);
   cf.setBlockOnAcknowledge(blockOnAcknowledge);
   cf.setBlockOnDurableSend(blockOnDurableSend);
   cf.setBlockOnNonDurableSend(blockOnNonDurableSend);
   cf.setAutoGroup(autoGroup);
   cf.setPreAcknowledge(preAcknowledge);
   cf.setConnectionLoadBalancingPolicyClassName(loadBalancingPolicyClassName);
   cf.setUseGlobalPools(useGlobalPools);
   cf.setScheduledThreadPoolMaxSize(scheduledThreadPoolMaxSize);
   cf.setThreadPoolMaxSize(threadPoolMaxSize);
   cf.setRetryInterval(retryInterval);
   cf.setRetryIntervalMultiplier(retryIntervalMultiplier);
   cf.setReconnectAttempts(reconnectAttempts);
   Assert.assertEquals(clientFailureCheckPeriod, cf.getClientFailureCheckPeriod());
   Assert.assertEquals(connectionTTL, cf.getConnectionTTL());
   Assert.assertEquals(callTimeout, cf.getCallTimeout());
   Assert.assertEquals(minLargeMessageSize, cf.getMinLargeMessageSize());
   Assert.assertEquals(consumerWindowSize, cf.getConsumerWindowSize());
   Assert.assertEquals(consumerMaxRate, cf.getConsumerMaxRate());
   Assert.assertEquals(confirmationWindowSize, cf.getConfirmationWindowSize());
   Assert.assertEquals(producerMaxRate, cf.getProducerMaxRate());
   Assert.assertEquals(blockOnAcknowledge, cf.isBlockOnAcknowledge());
   Assert.assertEquals(blockOnDurableSend, cf.isBlockOnDurableSend());
   Assert.assertEquals(blockOnNonDurableSend, cf.isBlockOnNonDurableSend());
   Assert.assertEquals(autoGroup, cf.isAutoGroup());
   Assert.assertEquals(preAcknowledge, cf.isPreAcknowledge());
   Assert.assertEquals(loadBalancingPolicyClassName, cf.getConnectionLoadBalancingPolicyClassName());
   Assert.assertEquals(useGlobalPools, cf.isUseGlobalPools());
   Assert.assertEquals(scheduledThreadPoolMaxSize, cf.getScheduledThreadPoolMaxSize());
   Assert.assertEquals(threadPoolMaxSize, cf.getThreadPoolMaxSize());
   Assert.assertEquals(retryInterval, cf.getRetryInterval());
   Assert.assertEquals(retryIntervalMultiplier, cf.getRetryIntervalMultiplier(), 0.0001);
   Assert.assertEquals(reconnectAttempts, cf.getReconnectAttempts());

   cf.close();
}
 
Example 10
Source File: RemoteConnectionStressTest.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
@Test
public void testSimpleRemoteConnections() throws Exception {
   for (int i = 0; i < 1000; i++) {

      TransportConfiguration config = new TransportConfiguration(NETTY_CONNECTOR_FACTORY);
      ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, config);
      cf.setInitialConnectAttempts(10);
      cf.setRetryInterval(100);

      Connection conn = cf.createConnection();

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

      Queue queue = session.createQueue("SomeQueue");

      MessageProducer producer = session.createProducer(queue);

      TextMessage msg = session.createTextMessage();
      msg.setText("Message " + i);

      producer.send(msg);

      producer.close();
      session.close();
      conn.close();

      cf.close();

   }
}