Java Code Examples for org.apache.activemq.artemis.api.core.client.ClientMessage#putIntProperty()

The following examples show how to use org.apache.activemq.artemis.api.core.client.ClientMessage#putIntProperty() . 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: RingQueueTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
   try {
      ClientProducer producer = prodSession.createProducer(address);
      for (int i = 0; i < numberOfMessages; i++) {
         ClientMessage message = prodSession.createMessage(true);
         message.putIntProperty("prodNR", i % nThreads);
         producer.send(message);
      }

   } catch (Throwable e) {
      e.printStackTrace();
      errors.incrementAndGet();
   } finally {
      try {
         prodSession.close();
         locator.close();
      } catch (Throwable ignored) {
         ignored.printStackTrace();
      }
   }
}
 
Example 2
Source File: MultipleThreadFilterOneTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * @throws ActiveMQException
 */
private void sendMessages(int msgs) throws ActiveMQException {
   ClientProducer producer = prodSession.createProducer(ADDRESS);

   for (int i = 0; i < msgs; i++) {
      ClientMessage message = prodSession.createMessage(true);
      message.putIntProperty("prodNR", i % nThreads);
      producer.send(message);

      if (i % 100 == 0) {
         prodSession.commit();
      }
   }
   prodSession.commit();

   producer.close();
}
 
Example 3
Source File: MultiThreadRandomReattachTestBase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void sendMessages(final ClientSession sessSend,
                          final ClientProducer producer,
                          final int numMessages,
                          final int threadNum) throws Exception {
   for (int i = 0; i < numMessages; i++) {
      ClientMessage message = sessSend.createMessage(ActiveMQBytesMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
      message.putIntProperty(new SimpleString("threadnum"), threadNum);
      message.putIntProperty(new SimpleString("count"), i);
      setBody(message);
      producer.send(message);
   }
}
 
Example 4
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testListScheduledMessagesAsJSON() throws Exception {
   long delay = 2000;
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();
   int intValue = RandomUtil.randomInt();
   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));

   QueueControl queueControl = createManagementControl(address, queue);

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(false);
   message.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, System.currentTimeMillis() + delay);
   message.putIntProperty(new SimpleString("key"), intValue);
   producer.send(message);
   // unscheduled message
   producer.send(session.createMessage(durable));

   String jsonString = queueControl.listScheduledMessagesAsJSON();
   Assert.assertNotNull(jsonString);
   JsonArray array = JsonUtil.readJsonArray(jsonString);
   Assert.assertEquals(1, array.size());
   int i = Integer.parseInt(array.getJsonObject(0).get("key").toString().replaceAll("\"", ""));
   Assert.assertEquals(intValue, i);

   Thread.sleep(delay + 500);

   jsonString = queueControl.listScheduledMessagesAsJSON();
   Assert.assertNotNull(jsonString);
   array = JsonUtil.readJsonArray(jsonString);
   Assert.assertEquals(0, array.size());

   consumeMessages(2, session, queue);

   session.deleteQueue(queue);
}
 
Example 5
Source File: ActiveMQTestBase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected final ClientMessage createMessage(ClientSession session,
                                            int counter,
                                            boolean durable) throws ActiveMQException {
   ClientMessage message = session.createMessage(durable);
   setBody(counter, message);
   message.putIntProperty("counter", counter);
   message.putIntProperty(SEND_CALL_NUMBER, sendMsgCount++);
   return message;
}
 
Example 6
Source File: FailoverOnFlowControlTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testOverflowSend() throws Exception {
   ServerLocator locator = getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(300).setProducerWindowSize(1000).setRetryInterval(100);
   final ArrayList<ClientSession> sessionList = new ArrayList<>();
   Interceptor interceptorClient = new Interceptor() {
      AtomicInteger count = new AtomicInteger(0);

      @Override
      public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
         log.debug("Intercept..." + packet.getClass().getName());

         if (packet instanceof SessionProducerCreditsMessage) {
            SessionProducerCreditsMessage credit = (SessionProducerCreditsMessage) packet;

            log.debug("Credits: " + credit.getCredits());
            if (count.incrementAndGet() == 2) {
               log.debug("### crashing server");
               try {
                  InVMConnection.setFlushEnabled(false);
                  crash(false, sessionList.get(0));
               } catch (Exception e) {
                  e.printStackTrace();
               } finally {
                  InVMConnection.setFlushEnabled(true);
               }
               return false;
            }
         }
         return true;
      }
   };

   locator.addIncomingInterceptor(interceptorClient);

   ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 2);
   ClientSession session = sf.createSession(true, true);
   sessionList.add(session);

   session.createQueue(new QueueConfiguration(ADDRESS));

   ClientProducer producer = session.createProducer(ADDRESS);

   final int numMessages = 10;

   for (int i = 0; i < numMessages; i++) {
      ClientMessage message = session.createMessage(true);

      message.getBodyBuffer().writeBytes(new byte[5000]);

      message.putIntProperty("counter", i);

      producer.send(message);
   }

   session.close();
}
 
Example 7
Source File: BridgeReconnectTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testReconnectSameNode() throws Exception {
   server0 = createActiveMQServer(0, isNetty(), server0Params);

   TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");

   server0.getConfiguration().setConnectorConfigurations(connectors);
   server1.getConfiguration().setConnectorConfigurations(connectors);

   BridgeConfiguration bridgeConfiguration = createBridgeConfig();

   List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
   bridgeConfigs.add(bridgeConfiguration);
   server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);

   QueueConfiguration queueConfig0 = new QueueConfiguration(queueName).setAddress(testAddress);
   List<QueueConfiguration> queueConfigs0 = new ArrayList<>();
   queueConfigs0.add(queueConfig0);
   server0.getConfiguration().setQueueConfigs(queueConfigs0);

   QueueConfiguration queueConfig1 = new QueueConfiguration(queueName).setAddress(forwardAddress);
   List<QueueConfiguration> queueConfigs1 = new ArrayList<>();
   queueConfigs1.add(queueConfig1);
   server1.getConfiguration().setQueueConfigs(queueConfigs1);

   startServers();

   locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(server0tc, server1tc));
   ClientSessionFactory csf0 = locator.createSessionFactory(server0tc);
   session0 = csf0.createSession(false, true, true);

   ClientSessionFactory csf1 = locator.createSessionFactory(server1tc);
   session1 = csf1.createSession(false, true, true);

   ClientProducer prod0 = session0.createProducer(testAddress);

   ClientConsumer cons1 = session1.createConsumer(queueName);

   session1.start();

   // Now we will simulate a failure of the bridge connection between server0 and server1
   Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
   assertNotNull(bridge);
   RemotingConnection forwardingConnection = getForwardingConnection(bridge);
   InVMConnector.failOnCreateConnection = true;
   InVMConnector.numberOfFailures = reconnectAttempts - 1;
   forwardingConnection.fail(new ActiveMQNotConnectedException());

   forwardingConnection = getForwardingConnection(bridge);
   forwardingConnection.fail(new ActiveMQNotConnectedException());

   final ManagementService managementService = server0.getManagementService();
   QueueControl coreQueueControl = (QueueControl) managementService.getResource(ResourceNames.QUEUE + queueName);
   assertEquals(0, coreQueueControl.getDeliveringCount());

   final int numMessages = NUM_MESSAGES;

   SimpleString propKey = new SimpleString("propkey");

   for (int i = 0; i < numMessages; i++) {
      ClientMessage message = session0.createMessage(false);
      message.putIntProperty(propKey, i);

      prod0.send(message);
   }

   for (int i = 0; i < numMessages; i++) {
      ClientMessage r1 = cons1.receive(1500);
      assertNotNull(r1);
      assertEquals(i, r1.getObjectProperty(propKey));
   }
   closeServers();

   assertNoMoreConnections();
}
 
Example 8
Source File: ClusterTestBase.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
protected void sendInRange(final int node,
                           final String address,
                           final int msgStart,
                           final int msgEnd,
                           final boolean durable,
                           final String filterVal,
                           final AtomicInteger duplicateDetectionSeq) throws Exception {
   ClientSessionFactory sf = sfs[node];

   if (sf == null) {
      throw new IllegalArgumentException("No sf at " + node);
   }

   ClientSession session = sf.createSession(false, false, false);

   try {
      ClientProducer producer = session.createProducer(address);

      for (int i = msgStart; i < msgEnd; i++) {
         ClientMessage message = session.createMessage(durable);

         if (filterVal != null) {
            message.putStringProperty(ClusterTestBase.FILTER_PROP, new SimpleString(filterVal));
         }

         if (duplicateDetectionSeq != null) {
            String str = Integer.toString(duplicateDetectionSeq.incrementAndGet());
            message.putStringProperty(Message.HDR_DUPLICATE_DETECTION_ID, new SimpleString(str));
         }

         message.putIntProperty(ClusterTestBase.COUNT_PROP, i);

         if (isLargeMessage()) {
            message.setBodyInputStream(createFakeLargeStream(getLargeMessageSize()));
         }

         producer.send(message);

         if (i % 100 == 0) {
            session.commit();
         }
      }

      session.commit();
   } finally {
      session.close();
   }
}
 
Example 9
Source File: RoutingTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testRouteToMultipleQueueWithFilters() throws Exception {
   ClientSession sendSession = cf.createSession(false, true, true);
   sendSession.createQueue(new QueueConfiguration(queueA).setAddress(addressA).setFilterString(new SimpleString("foo = 'bar'")).setDurable(false));
   sendSession.createQueue(new QueueConfiguration(queueB).setAddress(addressA).setFilterString(new SimpleString("x = 1")).setDurable(false));
   sendSession.createQueue(new QueueConfiguration(queueC).setAddress(addressA).setFilterString(new SimpleString("b = false")).setDurable(false));
   int numMessages = 300;
   ClientProducer p = sendSession.createProducer(addressA);
   for (int i = 0; i < numMessages; i++) {
      ClientMessage clientMessage = sendSession.createMessage(false);
      if (i % 3 == 0) {
         clientMessage.putStringProperty(new SimpleString("foo"), new SimpleString("bar"));
      } else if (i % 3 == 1) {
         clientMessage.putIntProperty(new SimpleString("x"), 1);
      } else {
         clientMessage.putBooleanProperty(new SimpleString("b"), false);
      }
      p.send(clientMessage);
   }
   ClientSession session = cf.createSession(false, true, true);
   ClientConsumer c1 = session.createConsumer(queueA);
   ClientConsumer c2 = session.createConsumer(queueB);
   ClientConsumer c3 = session.createConsumer(queueC);
   session.start();
   for (int i = 0; i < numMessages / 3; i++) {
      ClientMessage m = c1.receive(5000);
      Assert.assertNotNull(m);
      m.acknowledge();
      m = c2.receive(5000);
      Assert.assertNotNull(m);
      m.acknowledge();
      m = c3.receive(5000);
      Assert.assertNotNull(m);
      m.acknowledge();
   }
   Assert.assertNull(c1.receiveImmediate());
   Assert.assertNull(c2.receiveImmediate());
   Assert.assertNull(c3.receiveImmediate());
   sendSession.close();
   session.close();
}
 
Example 10
Source File: SessionStopStartTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testStopStartMultipleConsumers() throws Exception {
   locator.setConsumerWindowSize(getMessageEncodeSize(QUEUE) * 33);
   ClientSessionFactory sf = createSessionFactory(locator);

   final ClientSession session = sf.createSession(false, true, true);

   session.createQueue(new QueueConfiguration(QUEUE).setDurable(false));

   ClientProducer producer = session.createProducer(QUEUE);

   final int numMessages = 100;

   for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(session, "m" + i);
      message.putIntProperty(new SimpleString("i"), i);
      producer.send(message);
   }

   ClientConsumer consumer = session.createConsumer(QUEUE);
   ClientConsumer consumer2 = session.createConsumer(QUEUE);
   ClientConsumer consumer3 = session.createConsumer(QUEUE);

   session.start();

   ClientMessage cm = consumer.receive(5000);
   Assert.assertNotNull(cm);
   cm.acknowledge();
   cm = consumer2.receive(5000);
   Assert.assertNotNull(cm);
   cm.acknowledge();
   cm = consumer3.receive(5000);
   Assert.assertNotNull(cm);
   cm.acknowledge();

   session.stop();
   cm = consumer.receiveImmediate();
   Assert.assertNull(cm);
   cm = consumer2.receiveImmediate();
   Assert.assertNull(cm);
   cm = consumer3.receiveImmediate();
   Assert.assertNull(cm);

   session.start();
   cm = consumer.receive(5000);
   Assert.assertNotNull(cm);
   cm = consumer2.receive(5000);
   Assert.assertNotNull(cm);
   cm = consumer3.receive(5000);
   Assert.assertNotNull(cm);
   session.close();
}
 
Example 11
Source File: OrphanedConsumerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * @param useManagement true = it will use a management operation to make the connection failure, false through ping
 * @throws Exception
 */
private void internalTestOrphanedConsumers(boolean useManagement) throws Exception {
   final int NUMBER_OF_MESSAGES = 2;
   server = createServer(true, true);
   server.start();
   staticServer = server;

   // We are not interested on consumer-window-size on this test
   // We want that every message is delivered
   // as we asserting for number of consumers available and round-robin on delivery
   locator.setConsumerWindowSize(-1).setBlockOnNonDurableSend(false).setBlockOnDurableSend(false).setBlockOnAcknowledge(true).setConnectionTTL(1000).setClientFailureCheckPeriod(100).setReconnectAttempts(0);

   ClientSessionFactoryImpl sf = (ClientSessionFactoryImpl) createSessionFactory(locator);

   ClientSession session = sf.createSession(true, true, 0);

   session.createQueue(new QueueConfiguration("queue1").setAddress("queue"));
   session.createQueue(new QueueConfiguration("queue2").setAddress("queue"));

   ClientProducer prod = session.createProducer("queue");

   ClientConsumer consumer = session.createConsumer("queue1");
   ClientConsumer consumer2 = session.createConsumer("queue2");

   Queue queue1 = server.locateQueue(new SimpleString("queue1"));

   Queue queue2 = server.locateQueue(new SimpleString("queue2"));

   session.start();

   if (!useManagement) {
      sf.stopPingingAfterOne();

      for (long timeout = System.currentTimeMillis() + 6000; timeout > System.currentTimeMillis() && server.getConnectionCount() != 0; ) {
         Thread.sleep(100);
      }

      // an extra second to avoid races of something closing the session while we are asserting it
      Thread.sleep(1000);
   } else {
      server.getActiveMQServerControl().closeConnectionsForAddress("127.0.0.1");
   }

   if (verification != null) {
      throw verification;
   }

   assertEquals(0, queue1.getConsumerCount());
   assertEquals(0, queue2.getConsumerCount());

   setConditionActive(false);

   locator = internalCreateNonHALocator(true).setBlockOnNonDurableSend(false).setBlockOnDurableSend(false).setBlockOnAcknowledge(true).setReconnectAttempts(0).setConsumerWindowSize(-1);

   sf = (ClientSessionFactoryImpl) locator.createSessionFactory();
   session = sf.createSession(true, true, 0);

   session.start();

   prod = session.createProducer("queue");

   for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
      ClientMessage message = session.createMessage(true);
      message.putIntProperty("i", i);
      prod.send(message);
   }

   consumer = session.createConsumer("queue1");
   consumer2 = session.createConsumer("queue2");

   for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
      assertNotNull(consumer.receive(5000));
      assertNotNull(consumer2.receive(5000));
   }

   session.close();
}
 
Example 12
Source File: BridgeFailoverTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimpleConnectOnMultipleNodes() throws Exception {
   BridgeConfiguration bridgeConfiguration = new BridgeConfiguration();

   String ORIGINAL_QUEUE = "noCluster.originalQueue";
   String TARGET_QUEUE = "noCluster.targetQueue";

   bridgeConfiguration.setHA(true);
   List<String> connectors = new ArrayList<>();
   connectors.add("target-4");
   connectors.add("backup-4");
   bridgeConfiguration.setName("Bridge-for-test");
   bridgeConfiguration.setStaticConnectors(connectors);
   bridgeConfiguration.setQueueName(ORIGINAL_QUEUE);
   bridgeConfiguration.setForwardingAddress(TARGET_QUEUE);
   bridgeConfiguration.setRetryInterval(100);
   bridgeConfiguration.setConfirmationWindowSize(1);
   bridgeConfiguration.setReconnectAttempts(-1);
   servers[2].getConfiguration().getBridgeConfigurations().add(bridgeConfiguration);

   for (ActiveMQServer server : servers) {
      server.getConfiguration().addQueueConfiguration(new QueueConfiguration(ORIGINAL_QUEUE));
      server.getConfiguration().addQueueConfiguration(new QueueConfiguration(TARGET_QUEUE));
   }

   startServers();

   // The server where the bridge source is configured at
   ServerLocator locator = createLocator(false, 2);

   ClientSessionFactory factory = addSessionFactory(locator.createSessionFactory());
   ClientSession session = addClientSession(factory.createSession(false, false));
   ClientProducer producer = addClientProducer(session.createProducer(ORIGINAL_QUEUE));

   for (int i = 0; i < 100; i++) {
      ClientMessage msg = session.createMessage(true);
      msg.putIntProperty("i", i);
      producer.send(msg);
   }
   session.commit();

   ServerLocator locatorConsumer = createLocator(false, 4);
   ClientSessionFactory factoryConsumer = addSessionFactory(locatorConsumer.createSessionFactory());
   ClientSession sessionConsumer = addClientSession(factoryConsumer.createSession(false, false));
   ClientConsumer consumer = sessionConsumer.createConsumer(TARGET_QUEUE);

   sessionConsumer.start();

   for (int i = 0; i < 100; i++) {
      ClientMessage message = consumer.receive(10000);
      assertNotNull(message);
      message.acknowledge();
   }

   sessionConsumer.commit();
}
 
Example 13
Source File: BridgeReconnectTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeliveringCountOnBridgeConnectionFailure() throws Exception {
   server0 = createActiveMQServer(0, isNetty(), server0Params);

   TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");

   server0.getConfiguration().setConnectorConfigurations(connectors);

   BridgeConfiguration bridgeConfiguration = createBridgeConfig();

   List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
   bridgeConfigs.add(bridgeConfiguration);
   server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);

   QueueConfiguration queueConfig0 = new QueueConfiguration(queueName).setAddress(testAddress);
   List<QueueConfiguration> queueConfigs0 = new ArrayList<>();
   queueConfigs0.add(queueConfig0);
   server0.getConfiguration().setQueueConfigs(queueConfigs0);

   QueueConfiguration queueConfig1 = new QueueConfiguration(queueName).setAddress(forwardAddress);
   List<QueueConfiguration> queueConfigs1 = new ArrayList<>();
   queueConfigs1.add(queueConfig1);
   server1.getConfiguration().setQueueConfigs(queueConfigs1);

   startServers();

   locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(server0tc, server1tc));
   ClientSessionFactory csf0 = locator.createSessionFactory(server0tc);
   session0 = csf0.createSession(false, true, true);

   ClientSessionFactory csf1 = locator.createSessionFactory(server1tc);
   session1 = csf1.createSession(false, true, true);

   ClientProducer prod0 = session0.createProducer(testAddress);

   session1.start();

   Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
   RemotingConnection forwardingConnection = getForwardingConnection(bridge);
   InVMConnector.failOnCreateConnection = true;
   InVMConnector.numberOfFailures = reconnectAttempts - 1;
   //forwardingConnection.fail(new ActiveMQNotConnectedException());

   final int numMessages = NUM_MESSAGES;

   SimpleString propKey = new SimpleString("propkey");

   final Queue queue = (Queue) server0.getPostOffice().getBinding(new SimpleString(queueName)).getBindable();

   for (int i = 0; i < numMessages; i++) {
      ClientMessage message = session0.createMessage(false);
      message.putIntProperty(propKey, i);

      prod0.send(message);

      if (i == 50) {
         forwardingConnection.fail(new ActiveMQException(ActiveMQExceptionType.UNBLOCKED));
      }
   }

   Wait.assertEquals(0, queue::getDeliveringCount);

   closeServers();

   assertNoMoreConnections();
}
 
Example 14
Source File: BridgeFailoverTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testInitialConnectionNodeAlreadyDown() throws Exception {
   BridgeConfiguration bridgeConfiguration = new BridgeConfiguration();

   String ORIGINAL_QUEUE = "noCluster.originalQueue";
   String TARGET_QUEUE = "noCluster.targetQueue";

   bridgeConfiguration.setHA(true);
   List<String> connectors = new ArrayList<>();
   connectors.add("target-4");
   connectors.add("backup-4");
   bridgeConfiguration.setName("Bridge-for-test");
   bridgeConfiguration.setStaticConnectors(connectors);
   bridgeConfiguration.setQueueName(ORIGINAL_QUEUE);
   bridgeConfiguration.setForwardingAddress(TARGET_QUEUE);
   bridgeConfiguration.setRetryInterval(100);
   bridgeConfiguration.setConfirmationWindowSize(1);
   bridgeConfiguration.setReconnectAttempts(-1);
   servers[2].getConfiguration().getBridgeConfigurations().add(bridgeConfiguration);

   for (ActiveMQServer server : servers) {
      server.getConfiguration().addQueueConfiguration(new QueueConfiguration(ORIGINAL_QUEUE));
      server.getConfiguration().addQueueConfiguration(new QueueConfiguration(TARGET_QUEUE));
   }

   startBackups(0, 1, 3, 4);
   startServers(0, 1, 3, 4);

   waitForTopology(servers[4], getNumberOfServers() - 1, getNumberOfServers() - 1);

   crashAndWaitForFailure(servers[4], createLocator(false, 4));
   waitForServerToStart(backupServers[4]);

   startBackups(2);
   startServers(2);

   // The server where the bridge source is configured at
   ServerLocator locator = createLocator(false, 2); // connecting to the backup

   ClientSessionFactory factory = addSessionFactory(locator.createSessionFactory());
   ClientSession session = addClientSession(factory.createSession(false, false));
   ClientProducer producer = addClientProducer(session.createProducer(ORIGINAL_QUEUE));

   for (int i = 0; i < 100; i++) {
      ClientMessage msg = session.createMessage(true);
      msg.putIntProperty("i", i);
      producer.send(msg);
   }
   session.commit();

   ServerLocator locatorConsumer = createLocator(false, 9);
   ClientSessionFactory factoryConsumer = addSessionFactory(locatorConsumer.createSessionFactory());
   ClientSession sessionConsumer = addClientSession(factoryConsumer.createSession(false, false));
   ClientConsumer consumer = sessionConsumer.createConsumer(TARGET_QUEUE);

   sessionConsumer.start();

   for (int i = 0; i < 100; i++) {
      ClientMessage message = consumer.receive(10000);
      assertNotNull(message);
      message.acknowledge();
   }

   sessionConsumer.commit();
}
 
Example 15
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testListDeliveringMessages() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();
   int intValue = RandomUtil.randomInt();
   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));

   Queue srvqueue = server.locateQueue(queue);

   QueueControl queueControl = createManagementControl(address, queue);

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(durable);
   message.putIntProperty(new SimpleString("key"), intValue);
   producer.send(message);
   producer.send(session.createMessage(durable));

   ClientConsumer consumer = session.createConsumer(queue);
   session.start();
   ClientMessage msgRec = consumer.receive(5000);
   assertNotNull(msgRec);
   assertEquals(msgRec.getIntProperty("key").intValue(), intValue);

   ClientSessionFactory sf2 = createSessionFactory(locator);
   ClientSession session2 = sf2.createSession(false, true, false);
   ClientConsumer consumer2 = session2.createConsumer(queue);
   session2.start();
   ClientMessage msgRec2 = consumer2.receive(5000);
   assertNotNull(msgRec2);

   assertEquals(2, srvqueue.getDeliveringCount());
   assertEquals(2, srvqueue.getConsumerCount());

   Map<String, Map<String, Object>[]> deliveringMap = queueControl.listDeliveringMessages();
   assertEquals(2, deliveringMap.size());

   consumer.close();
   consumer2.close();

   session.deleteQueue(queue);
}
 
Example 16
Source File: ReplicationOrderTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private void doTestMixedPersistentAndNonPersistentMessagesOrderWithReplicatedBackup(final boolean transactional) throws Exception {
   String address = RandomUtil.randomString();
   String queue = RandomUtil.randomString();
   ServerLocator locator = ActiveMQClient.createServerLocatorWithoutHA(getConnectorTransportConfiguration(true));
   addServerLocator(locator);
   locator.setBlockOnNonDurableSend(false).setBlockOnDurableSend(false);
   ClientSessionFactory csf = createSessionFactory(locator);
   ClientSession session = null;
   if (transactional) {
      session = csf.createSession(false, false);
   } else {
      session = csf.createSession(true, true);
   }
   addClientSession(session);
   session.createQueue(new QueueConfiguration(queue).setAddress(address));
   ClientProducer producer = session.createProducer(address);
   boolean durable = false;
   for (int i = 0; i < ReplicationOrderTest.NUM; i++) {
      ClientMessage msg = session.createMessage(durable);
      msg.putIntProperty("counter", i);
      producer.send(msg);
      if (transactional) {
         if (i % 10 == 0) {
            session.commit();
            durable = !durable;
         }
      } else {
         durable = !durable;
      }
   }
   if (transactional) {
      session.commit();
   }
   session.close();

   locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(getConnectorTransportConfiguration(true)));
   csf = createSessionFactory(locator);
   session = csf.createSession(true, true);
   session.start();
   ClientConsumer consumer = session.createConsumer(queue);
   for (int i = 0; i < ReplicationOrderTest.NUM; i++) {
      ClientMessage message = consumer.receive(1000);
      Assert.assertNotNull(message);
      Assert.assertEquals(i, message.getIntProperty("counter").intValue());
   }

   consumer.close();
   session.deleteQueue(queue);

   session.close();
}
 
Example 17
Source File: FailoverTest.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
protected ClientSession sendAndConsume(final ClientSessionFactory sf1, final boolean createQueue) throws Exception {
   ClientSession session = createSession(sf1, false, true, true);

   if (createQueue) {
      session.createQueue(new QueueConfiguration(FailoverTestBase.ADDRESS).setDurable(false));
   }

   ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);

   for (int i = 0; i < NUM_MESSAGES; i++) {
      ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
      message.putIntProperty(new SimpleString("count"), i);
      message.getBodyBuffer().writeString("aardvarks");
      producer.send(message);
   }

   ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);

   session.start();

   for (int i = 0; i < NUM_MESSAGES; i++) {
      ClientMessage message2 = consumer.receive();

      Assert.assertEquals("aardvarks", message2.getBodyBuffer().readString());

      Assert.assertEquals(i, message2.getObjectProperty(new SimpleString("count")));

      message2.acknowledge();
   }

   ClientMessage message3 = consumer.receiveImmediate();

   Assert.assertNull(message3);

   return session;
}
 
Example 18
Source File: ClusterConnectionImpl.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
private synchronized void doConsumerClosed(final ClientMessage message) throws Exception {
   if (logger.isTraceEnabled()) {
      logger.trace(ClusterConnectionImpl.this + " Consumer closed " + message);
   }
   if (!message.containsProperty(ManagementHelper.HDR_DISTANCE)) {
      throw new IllegalStateException("distance is null");
   }

   if (!message.containsProperty(ManagementHelper.HDR_CLUSTER_NAME)) {
      throw new IllegalStateException("clusterName is null");
   }

   Integer distance = message.getIntProperty(ManagementHelper.HDR_DISTANCE);

   SimpleString clusterName = message.getSimpleStringProperty(ManagementHelper.HDR_CLUSTER_NAME);

   message.putIntProperty(ManagementHelper.HDR_DISTANCE, distance + 1);

   SimpleString filterString = message.getSimpleStringProperty(ManagementHelper.HDR_FILTERSTRING);

   RemoteQueueBinding binding = bindings.get(clusterName);

   if (binding == null) {
      throw new IllegalStateException("Cannot find binding for " + clusterName);
   }

   binding.removeConsumer(filterString);

   // Need to propagate the consumer close
   TypedProperties props = new TypedProperties();

   props.putSimpleStringProperty(ManagementHelper.HDR_ADDRESS, binding.getAddress());

   props.putSimpleStringProperty(ManagementHelper.HDR_CLUSTER_NAME, clusterName);

   props.putSimpleStringProperty(ManagementHelper.HDR_ROUTING_NAME, binding.getRoutingName());

   props.putIntProperty(ManagementHelper.HDR_DISTANCE, distance + 1);

   Queue theQueue = (Queue) binding.getBindable();

   props.putIntProperty(ManagementHelper.HDR_CONSUMER_COUNT, theQueue.getConsumerCount());

   if (filterString != null) {
      props.putSimpleStringProperty(ManagementHelper.HDR_FILTERSTRING, filterString);
   }
   Notification notification = new Notification(null, CoreNotificationType.CONSUMER_CLOSED, props);

   managementService.sendNotification(notification);
}
 
Example 19
Source File: QueueBrowserTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
@Test
public void testBrowseWithZeroConsumerWindowSize() throws Exception {
   locator.setConsumerWindowSize(0);

   ClientSessionFactory sf = createSessionFactory(locator);

   ClientSession session = sf.createSession(false, true, true);

   session.createQueue(new QueueConfiguration(QUEUE).setDurable(false));

   ClientProducer producer = session.createProducer(QUEUE);

   final int numMessages = 100;

   byte[] bytes = new byte[240];

   for (int i = 0; i < numMessages; i++) {
      ClientMessage message = session.createMessage(false);

      message.getBodyBuffer().writeBytes(bytes);

      message.putIntProperty("foo", i);

      producer.send(message);
   }

   //Create a normal non browsing consumer
   session.createConsumer(QUEUE);

   session.start();

   ClientConsumer browser = session.createConsumer(QUEUE, true);

   for (int i = 0; i < numMessages; i++) {
      ClientMessage message2 = browser.receive(1000);

      assertEquals(i, message2.getIntProperty("foo").intValue());
   }

   session.close();
}
 
Example 20
Source File: ClusterConnectionImpl.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
private synchronized void doConsumerCreated(final ClientMessage message) throws Exception {
   if (logger.isTraceEnabled()) {
      logger.trace(ClusterConnectionImpl.this + " Consumer created " + message);
   }
   if (!message.containsProperty(ManagementHelper.HDR_DISTANCE)) {
      throw new IllegalStateException("distance is null");
   }

   if (!message.containsProperty(ManagementHelper.HDR_CLUSTER_NAME)) {
      throw new IllegalStateException("clusterName is null");
   }

   Integer distance = message.getIntProperty(ManagementHelper.HDR_DISTANCE);

   SimpleString clusterName = message.getSimpleStringProperty(ManagementHelper.HDR_CLUSTER_NAME);

   message.putIntProperty(ManagementHelper.HDR_DISTANCE, distance + 1);

   SimpleString filterString = message.getSimpleStringProperty(ManagementHelper.HDR_FILTERSTRING);

   RemoteQueueBinding binding = bindings.get(clusterName);

   if (binding == null) {
      throw new IllegalStateException("Cannot find binding for " + clusterName +
                                         " on " +
                                         ClusterConnectionImpl.this);
   }

   binding.addConsumer(filterString);

   // Need to propagate the consumer add
   TypedProperties props = new TypedProperties();

   props.putSimpleStringProperty(ManagementHelper.HDR_ADDRESS, binding.getAddress());

   props.putSimpleStringProperty(ManagementHelper.HDR_CLUSTER_NAME, clusterName);

   props.putSimpleStringProperty(ManagementHelper.HDR_ROUTING_NAME, binding.getRoutingName());

   props.putIntProperty(ManagementHelper.HDR_DISTANCE, distance + 1);

   Queue theQueue = (Queue) binding.getBindable();

   props.putIntProperty(ManagementHelper.HDR_CONSUMER_COUNT, theQueue.getConsumerCount());

   if (filterString != null) {
      props.putSimpleStringProperty(ManagementHelper.HDR_FILTERSTRING, filterString);
   }

   Notification notification = new Notification(null, CoreNotificationType.CONSUMER_CREATED, props);

   managementService.sendNotification(notification);
}