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

The following examples show how to use org.apache.activemq.artemis.api.core.client.ClientMessage#putLongProperty() . 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: Sender.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
   super.run();
   while (running) {
      try {
         beginTX();
         for (int i = 0; i < 1000; i++) {
            ClientMessage msg = session.createMessage(true);
            msg.putLongProperty("count", pendingMsgs + msgs);
            msg.getBodyBuffer().writeBytes(new byte[10 * 1024]);
            producer.send(msg);
            pendingMsgs++;
         }
         endTX();
      } catch (Exception e) {
         connect();
      }
   }
}
 
Example 2
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testListMessagesAsJSONWithFilter() throws Exception {
   SimpleString key = new SimpleString("key");
   long matchingValue = RandomUtil.randomLong();
   long unmatchingValue = matchingValue + 1;
   String filter = key + " =" + matchingValue;

   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));
   QueueControl queueControl = createManagementControl(address, queue);

   ClientProducer producer = session.createProducer(address);
   ClientMessage matchingMessage = session.createMessage(durable);
   matchingMessage.putLongProperty(key, matchingValue);
   producer.send(matchingMessage);
   ClientMessage unmatchingMessage = session.createMessage(durable);
   unmatchingMessage.putLongProperty(key, unmatchingValue);
   producer.send(unmatchingMessage);

   String jsonString = queueControl.listMessagesAsJSON(filter);
   Assert.assertNotNull(jsonString);
   JsonArray array = JsonUtil.readJsonArray(jsonString);
   Assert.assertEquals(1, array.size());

   long l = Long.parseLong(array.getJsonObject(0).get("key").toString().replaceAll("\"", ""));
   Assert.assertEquals(matchingValue, l);

   consumeMessages(2, session, queue);

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

   session.deleteQueue(queue);
}
 
Example 3
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testCountDeliveringMessageCountGroupFilter() throws Exception {
   String key = new String("key_group");
   long valueGroup1 = RandomUtil.randomLong();
   long valueGroup2 = valueGroup1 + 1;

   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(false));
   ClientProducer producer = session.createProducer(address);

   ClientMessage message1 = session.createMessage(false);
   message1.putLongProperty(key, valueGroup1);
   ClientMessage message2 = session.createMessage(false);
   message2.putLongProperty(key, valueGroup2);
   producer.send(message1);
   producer.send(message2);
   producer.send(message1);
   session.commit();

   QueueControl queueControl = createManagementControl(address, queue);
   String result = queueControl.countDeliveringMessages(key + " =" + valueGroup1, key);
   JsonObject jsonObject = JsonUtil.readJsonObject(result);
   Assert.assertTrue(jsonObject.isEmpty());

   ClientConsumer consumer = session.createConsumer(queue, null, 1024 * 1024, 1, false);
   ClientMessage message = consumer.receive(500);
   Assert.assertNotNull(message);

   result = queueControl.countDeliveringMessages(key + " =" + valueGroup1, key);
   jsonObject = JsonUtil.readJsonObject(result);
   Assert.assertEquals(2, jsonObject.getInt(String.valueOf(valueGroup1)));
   Assert.assertFalse(jsonObject.containsKey(String.valueOf(valueGroup2)));
   Assert.assertFalse(jsonObject.containsKey(null));

   consumer.close();
   session.deleteQueue(queue);
}
 
Example 4
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testCountDeliveringMessageCountNoFilter() throws Exception {
   SimpleString key = new SimpleString("key");
   long matchingValue = RandomUtil.randomLong();
   long unmatchingValue = matchingValue + 1;

   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(false));
   ClientProducer producer = session.createProducer(address);

   ClientMessage matchingMessage = session.createMessage(false);
   matchingMessage.putLongProperty(key, matchingValue);
   ClientMessage unmatchingMessage = session.createMessage(false);
   unmatchingMessage.putLongProperty(key, unmatchingValue);
   producer.send(matchingMessage);
   producer.send(unmatchingMessage);
   producer.send(matchingMessage);

   QueueControl queueControl = createManagementControl(address, queue);
   Assert.assertEquals(0, queueControl.countDeliveringMessages(null));

   ClientConsumer consumer = session.createConsumer(queue, null, 1024 * 1024, 1, false);
   ClientMessage message = consumer.receive(500);
   Assert.assertNotNull(message);
   Assert.assertEquals(3, queueControl.countDeliveringMessages(null));

   consumer.close();
   session.deleteQueue(queue);
}
 
Example 5
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testCountMessagesWithFilter() throws Exception {
   SimpleString key = new SimpleString("key");
   long matchingValue = RandomUtil.randomLong();
   long unmatchingValue = matchingValue + 1;

   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));
   ClientProducer producer = session.createProducer(address);

   // send on queue
   ClientMessage matchingMessage = session.createMessage(durable);
   matchingMessage.putLongProperty(key, matchingValue);
   ClientMessage unmatchingMessage = session.createMessage(durable);
   unmatchingMessage.putLongProperty(key, unmatchingValue);
   producer.send(matchingMessage);
   producer.send(unmatchingMessage);
   producer.send(matchingMessage);

   QueueControl queueControl = createManagementControl(address, queue);
   Assert.assertEquals(3, getMessageCount(queueControl));
   assertMessageMetrics(queueControl, 3, durable);

   Assert.assertEquals(2, queueControl.countMessages(key + " =" + matchingValue));
   Assert.assertEquals(1, queueControl.countMessages(key + " =" + unmatchingValue));

   session.deleteQueue(queue);
}
 
Example 6
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveMessagesToUnknownQueue() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();
   SimpleString unknownQueue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));
   ClientProducer producer = session.createProducer(address);

   // send on queue
   ClientMessage message = session.createMessage(durable);
   SimpleString key = RandomUtil.randomSimpleString();
   long value = RandomUtil.randomLong();
   message.putLongProperty(key, value);
   producer.send(message);

   QueueControl queueControl = createManagementControl(address, queue);
   assertMessageMetrics(queueControl, 1, durable);

   // moved all messages to unknown queue
   try {
      queueControl.moveMessages(null, unknownQueue.toString());
      Assert.fail("operation must fail if the other queue does not exist");
   } catch (Exception e) {
   }
   Assert.assertEquals(1, getMessageCount(queueControl));
   assertMessageMetrics(queueControl, 1, durable);

   consumeMessages(1, session, queue);

   session.deleteQueue(queue);
}
 
Example 7
Source File: RequestorTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequest() throws Exception {
   final SimpleString key = RandomUtil.randomSimpleString();
   long value = RandomUtil.randomLong();
   SimpleString requestAddress = new SimpleString("AdTest");
   SimpleString requestQueue = RandomUtil.randomSimpleString();

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

   session.start();

   session.createQueue(new QueueConfiguration(requestQueue).setAddress(requestAddress).setDurable(false).setTemporary(true));

   ClientConsumer requestConsumer = session.createConsumer(requestQueue);
   requestConsumer.setMessageHandler(new SimpleMessageHandler(key, session));

   ClientRequestor requestor = new ClientRequestor(session, requestAddress);
   ClientMessage request = session.createMessage(false);
   request.putLongProperty(key, value);

   ClientMessage reply = requestor.request(request, 500);
   Assert.assertNotNull("reply was not received", reply);
   Assert.assertEquals(value, reply.getObjectProperty(key));

   Thread.sleep(5000);
   session.close();
}
 
Example 8
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testListMessagesWithFilter() throws Exception {
   SimpleString key = new SimpleString("key");
   long matchingValue = RandomUtil.randomLong();
   long unmatchingValue = matchingValue + 1;
   String filter = key + " =" + matchingValue;

   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));
   QueueControl queueControl = createManagementControl(address, queue);

   ClientProducer producer = session.createProducer(address);
   ClientMessage matchingMessage = session.createMessage(durable);
   matchingMessage.putLongProperty(key, matchingValue);
   producer.send(matchingMessage);
   ClientMessage unmatchingMessage = session.createMessage(durable);
   unmatchingMessage.putLongProperty(key, unmatchingValue);
   producer.send(unmatchingMessage);

   assertMessageMetrics(queueControl, 2, durable);
   Map<String, Object>[] messages = queueControl.listMessages(filter);
   Assert.assertEquals(1, messages.length);
   Assert.assertEquals(matchingValue, Long.parseLong(messages[0].get("key").toString()));

   consumeMessages(2, session, queue);

   messages = queueControl.listMessages(filter);
   Assert.assertEquals(0, messages.length);
   assertMessageMetrics(queueControl, 0, durable);

   session.deleteQueue(queue);
}
 
Example 9
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testListScheduledMessages() 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(durable);
   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));

   Map<String, Object>[] messages = queueControl.listScheduledMessages();
   Assert.assertEquals(1, messages.length);
   assertScheduledMetrics(queueControl, 1, durable);

   Assert.assertEquals(intValue, Integer.parseInt((messages[0].get("key")).toString()));

   Thread.sleep(delay + 500);

   messages = queueControl.listScheduledMessages();
   Assert.assertEquals(0, messages.length);

   consumeMessages(2, session, queue);

   session.deleteQueue(queue);
}
 
Example 10
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetScheduledCount() throws Exception {
   long delay = 500;
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));

   QueueControl queueControl = createManagementControl(address, queue);
   Assert.assertEquals(0, queueControl.getScheduledCount());

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(durable);
   message.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, System.currentTimeMillis() + delay);
   producer.send(message);

   long timeout = System.currentTimeMillis() + 5000;
   while (timeout > System.currentTimeMillis() && queueControl.getScheduledCount() != 1) {
      Thread.sleep(100);
   }

   assertScheduledMetrics(queueControl, 1, durable);
   assertMessageMetrics(queueControl, 1, durable);

   consumeMessages(0, session, queue);

   Thread.sleep(delay * 2);

   Assert.assertEquals(0, queueControl.getScheduledCount());
   consumeMessages(1, session, queue);
   assertMessageMetrics(queueControl, 0, durable);
   assertScheduledMetrics(queueControl, 0, durable);

   session.deleteQueue(queue);
}
 
Example 11
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetScheduledCountOnRemove() throws Exception {
   long delay = Integer.MAX_VALUE;
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));

   QueueControl queueControl = createManagementControl(address, queue);
   Assert.assertEquals(0, queueControl.getScheduledCount());

   Field queueMemorySizeField = QueueImpl.class.getDeclaredField("queueMemorySize");
   queueMemorySizeField.setAccessible(true);
   final LocalQueueBinding binding = (LocalQueueBinding) server.getPostOffice().getBinding(queue);
   Queue q = binding.getQueue();
   AtomicInteger queueMemorySize1 = (AtomicInteger) queueMemorySizeField.get(q);
   assertEquals(0, queueMemorySize1.get());

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(durable);
   message.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, System.currentTimeMillis() + delay);
   producer.send(message);

   queueControl.removeAllMessages();

   Assert.assertEquals(0, queueControl.getMessageCount());

   //Verify that original queue has a memory size of 0
   assertEquals(0, queueMemorySize1.get());

   session.deleteQueue(queue);
}
 
Example 12
Source File: RequestorTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoRequests() throws Exception {
   final SimpleString key = RandomUtil.randomSimpleString();
   long value = RandomUtil.randomLong();
   SimpleString requestAddress = RandomUtil.randomSimpleString();
   SimpleString requestQueue = RandomUtil.randomSimpleString();

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

   session.start();

   session.createQueue(new QueueConfiguration(requestQueue).setAddress(requestAddress).setDurable(false).setTemporary(true));

   ClientConsumer requestConsumer = session.createConsumer(requestQueue);
   requestConsumer.setMessageHandler(new SimpleMessageHandler(key, session));

   ClientRequestor requestor = new ClientRequestor(session, requestAddress);
   ClientMessage request = session.createMessage(false);
   request.putLongProperty(key, value);

   ClientMessage reply = requestor.request(request, 500);
   Assert.assertNotNull("reply was not received", reply);
   Assert.assertEquals(value, reply.getObjectProperty(key));

   request = session.createMessage(false);
   request.putLongProperty(key, value + 1);

   reply = requestor.request(request, 500);
   Assert.assertNotNull("reply was not received", reply);
   Assert.assertEquals(value + 1, reply.getObjectProperty(key));

   session.close();
}
 
Example 13
Source File: ScheduledMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testPagedMessageDeliveredCorrectly() throws Exception {
   // then we create a client as normal
   ClientSessionFactory sessionFactory = createSessionFactory(locator);
   ClientSession session = sessionFactory.createSession(false, true, false);
   session.createQueue(new QueueConfiguration(atestq));
   ClientProducer producer = session.createProducer(atestq);
   ClientMessage message = createDurableMessage(session, "m1");
   long time = System.currentTimeMillis();
   time += 10000;
   message.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, time);
   producer.send(message);

   producer.close();

   ClientConsumer consumer = session.createConsumer(atestq);

   session.start();

   ClientMessage message2 = consumer.receive(10250);
   Assert.assertTrue(System.currentTimeMillis() >= time);
   Assert.assertEquals("m1", message2.getBodyBuffer().readString());

   message2.acknowledge();

   // Make sure no more messages
   consumer.close();
   consumer = session.createConsumer(atestq);
   Assert.assertNull(consumer.receiveImmediate());

   session.close();
}
 
Example 14
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testExpireMessagesWithFilter() throws Exception {
   SimpleString key = new SimpleString("key");
   long matchingValue = RandomUtil.randomLong();
   long unmatchingValue = matchingValue + 1;

   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));
   ClientProducer producer = session.createProducer(address);

   // send on queue
   ClientMessage matchingMessage = session.createMessage(durable);
   matchingMessage.putLongProperty(key, matchingValue);
   producer.send(matchingMessage);
   ClientMessage unmatchingMessage = session.createMessage(durable);
   unmatchingMessage.putLongProperty(key, unmatchingValue);
   producer.send(unmatchingMessage);

   QueueControl queueControl = createManagementControl(address, queue);
   Assert.assertEquals(2, getMessageCount(queueControl));

   int expiredMessagesCount = queueControl.expireMessages(key + " =" + matchingValue);
   Assert.assertEquals(1, expiredMessagesCount);
   assertMessageMetrics(queueControl, 1, durable);

   // consume the unmatched message from queue
   ClientConsumer consumer = session.createConsumer(queue);
   ClientMessage m = consumer.receive(500);
   Assert.assertNotNull(m);
   Assert.assertEquals(unmatchingValue, m.getObjectProperty(key));

   m.acknowledge();

   // check there is no other message to consume:
   m = consumer.receiveImmediate();
   Assert.assertNull(m);

   consumer.close();
   session.deleteQueue(queue);
   session.close();
}
 
Example 15
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * <ol>
 * <li>send a message to queue</li>
 * <li>move all messages from queue to otherQueue using management method</li>
 * <li>check there is no message to consume from queue</li>
 * <li>consume the message from otherQueue</li>
 * </ol>
 */
@Test
public void testMoveMessages() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();
   SimpleString otherAddress = RandomUtil.randomSimpleString();
   SimpleString otherQueue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));
   session.createQueue(new QueueConfiguration(otherQueue).setAddress(otherAddress).setDurable(durable));
   ClientProducer producer = session.createProducer(address);

   // send on queue
   ClientMessage message = session.createMessage(durable);
   SimpleString key = RandomUtil.randomSimpleString();
   long value = RandomUtil.randomLong();
   message.putLongProperty(key, value);
   producer.send(message);

   final LocalQueueBinding binding = (LocalQueueBinding) server.getPostOffice().getBinding(queue);
   Queue q = binding.getQueue();
   Field queueMemorySizeField = QueueImpl.class.getDeclaredField("queueMemorySize");
   queueMemorySizeField.setAccessible(true);

   //Get memory size counters to verify
   AtomicInteger queueMemorySize = (AtomicInteger) queueMemorySizeField.get(q);

   QueueControl queueControl = createManagementControl(address, queue);
   assertMessageMetrics(queueControl, 1, durable);

   //verify memory usage is greater than 0
   Assert.assertTrue(queueMemorySize.get() > 0);

   // moved all messages to otherQueue
   int movedMessagesCount = queueControl.moveMessages(null, otherQueue.toString());
   Assert.assertEquals(1, movedMessagesCount);
   assertMessageMetrics(queueControl, 0, durable);

   //verify memory usage is 0 after move
   Assert.assertEquals(0, queueMemorySize.get());

   // check there is no message to consume from queue
   consumeMessages(0, session, queue);

   // consume the message from otherQueue
   ClientConsumer otherConsumer = session.createConsumer(otherQueue);
   ClientMessage m = otherConsumer.receive(500);
   Assert.assertEquals(value, m.getObjectProperty(key));

   m.acknowledge();

   session.deleteQueue(queue);
   otherConsumer.close();
   session.deleteQueue(otherQueue);
}
 
Example 16
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * <ol>
 * <li>send 2 message to queue</li>
 * <li>move messages from queue to otherQueue using management method <em>with filter</em></li>
 * <li>consume the message which <strong>did not</strong> matches the filter from queue</li>
 * <li>consume the message which <strong>did</strong> matches the filter from otherQueue</li>
 * </ol>
 */
@Test
public void testMoveMessagesWithFilter() throws Exception {
   SimpleString key = new SimpleString("key");
   long matchingValue = RandomUtil.randomLong();
   long unmatchingValue = matchingValue + 1;

   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();
   SimpleString otherAddress = RandomUtil.randomSimpleString();
   SimpleString otherQueue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));
   session.createQueue(new QueueConfiguration(otherQueue).setAddress(otherAddress).setDurable(durable));
   ClientProducer producer = session.createProducer(address);

   // send on queue
   ClientMessage matchingMessage = session.createMessage(durable);
   matchingMessage.putLongProperty(key, matchingValue);
   producer.send(matchingMessage);
   ClientMessage unmatchingMessage = session.createMessage(durable);
   unmatchingMessage.putLongProperty(key, unmatchingValue);
   producer.send(unmatchingMessage);

   QueueControl queueControl = createManagementControl(address, queue);
   assertMessageMetrics(queueControl, 2, durable);

   // moved matching messages to otherQueue
   int movedMatchedMessagesCount = queueControl.moveMessages(key + " =" + matchingValue, otherQueue.toString());
   Assert.assertEquals(1, movedMatchedMessagesCount);
   Assert.assertEquals(1, getMessageCount(queueControl));

   // consume the unmatched message from queue
   ClientConsumer consumer = session.createConsumer(queue);
   ClientMessage m = consumer.receive(500);
   Assert.assertNotNull(m);
   Assert.assertEquals(unmatchingValue, m.getObjectProperty(key));

   // consume the matched message from otherQueue
   ClientConsumer otherConsumer = session.createConsumer(otherQueue);
   m = otherConsumer.receive(500);
   Assert.assertNotNull(m);
   Assert.assertEquals(matchingValue, m.getObjectProperty(key));

   m.acknowledge();

   consumer.close();
   session.deleteQueue(queue);
   otherConsumer.close();
   session.deleteQueue(otherQueue);
}
 
Example 17
Source File: RequestorTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testManyRequestsOverBlocked() throws Exception {
   final SimpleString key = RandomUtil.randomSimpleString();
   long value = RandomUtil.randomLong();

   AddressSettings settings = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK).setMaxSizeBytes(1024);
   server.getAddressSettingsRepository().addMatch("#", settings);

   SimpleString requestAddress = new SimpleString("RequestAddress");

   SimpleString requestQueue = new SimpleString("RequestAddress Queue");

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

   sessionRequest.createQueue(new QueueConfiguration(requestQueue).setAddress(requestAddress));

   sessionRequest.start();

   ClientConsumer requestConsumer = sessionRequest.createConsumer(requestQueue);
   requestConsumer.setMessageHandler(new SimpleMessageHandler(key, sessionRequest));

   for (int i = 0; i < 2000; i++) {
      final ClientSession session = sf.createSession(false, true, true);

      session.start();

      ClientRequestor requestor = new ClientRequestor(session, requestAddress);
      ClientMessage request = session.createMessage(false);
      request.putLongProperty(key, value);

      ClientMessage reply = requestor.request(request, 5000);
      Assert.assertNotNull("reply was not received", reply);
      reply.acknowledge();
      Assert.assertEquals(value, reply.getObjectProperty(key));
      requestor.close();
      session.close();
   }

   sessionRequest.close();

}
 
Example 18
Source File: QueueControlTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testRemoveMessagesWithLimit() throws Exception {
   SimpleString key = new SimpleString("key");
   long matchingValue = RandomUtil.randomLong();
   long unmatchingValue = matchingValue + 1;

   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(durable));
   ClientProducer producer = session.createProducer(address);

   // send on queue
   ClientMessage matchingMessage = session.createMessage(durable);
   matchingMessage.putLongProperty(key, matchingValue);
   producer.send(matchingMessage);
   ClientMessage unmatchingMessage = session.createMessage(durable);
   unmatchingMessage.putLongProperty(key, unmatchingValue);
   producer.send(unmatchingMessage);

   QueueControl queueControl = createManagementControl(address, queue);
   assertMessageMetrics(queueControl, 2, durable);

   // removed matching messages to otherQueue
   int removedMatchedMessagesCount = queueControl.removeMessages(5, key + " =" + matchingValue);
   Assert.assertEquals(1, removedMatchedMessagesCount);
   assertMessageMetrics(queueControl, 1, durable);

   // consume the unmatched message from queue
   ClientConsumer consumer = session.createConsumer(queue);
   ClientMessage m = consumer.receive(500);
   Assert.assertNotNull(m);
   Assert.assertEquals(unmatchingValue, m.getObjectProperty(key));

   m.acknowledge();

   // check there is no other message to consume:
   m = consumer.receiveImmediate();
   Assert.assertNull(m);

   consumer.close();
   session.deleteQueue(queue);
}
 
Example 19
Source File: ScheduledMessageTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void testScheduledMessagesDeliveredCorrectly(final boolean recover) throws Exception {

      ClientSessionFactory sessionFactory = createSessionFactory(locator);
      ClientSession session = sessionFactory.createSession(false, true, false);
      session.createQueue(new QueueConfiguration(atestq));
      ClientProducer producer = session.createProducer(atestq);
      ClientMessage m1 = createDurableMessage(session, "m1");
      ClientMessage m2 = createDurableMessage(session, "m2");
      ClientMessage m3 = createDurableMessage(session, "m3");
      ClientMessage m4 = createDurableMessage(session, "m4");
      ClientMessage m5 = createDurableMessage(session, "m5");
      long time = System.currentTimeMillis();
      time += 10000;
      m1.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, time);
      producer.send(m1);
      time += 1000;
      m2.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, time);
      producer.send(m2);
      time += 1000;
      m3.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, time);
      producer.send(m3);
      time += 1000;
      m4.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, time);
      producer.send(m4);
      time += 1000;
      m5.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, time);
      producer.send(m5);
      time -= 4000;
      if (recover) {
         producer.close();
         session.close();
         server.stop();
         server = null;
         server = createServer(true, configuration);
         server.start();

         sessionFactory = createSessionFactory(locator);

         session = sessionFactory.createSession(false, true, true);
      }

      ClientConsumer consumer = session.createConsumer(atestq);

      session.start();

      ClientMessage message = consumer.receive(11000);
      Assert.assertTrue(System.currentTimeMillis() >= time);
      Assert.assertEquals("m1", message.getBodyBuffer().readString());
      message.acknowledge();
      time += 1000;
      message = consumer.receive(1250);
      Assert.assertTrue(System.currentTimeMillis() >= time);
      Assert.assertEquals("m2", message.getBodyBuffer().readString());
      message.acknowledge();
      time += 1000;
      message = consumer.receive(1250);
      Assert.assertTrue(System.currentTimeMillis() >= time);
      Assert.assertEquals("m3", message.getBodyBuffer().readString());
      message.acknowledge();
      time += 1000;
      message = consumer.receive(1250);
      Assert.assertTrue(System.currentTimeMillis() >= time);
      Assert.assertEquals("m4", message.getBodyBuffer().readString());
      message.acknowledge();
      time += 1000;
      message = consumer.receive(1250);
      Assert.assertTrue(System.currentTimeMillis() >= time);
      Assert.assertEquals("m5", message.getBodyBuffer().readString());
      message.acknowledge();

      // Make sure no more messages
      consumer.close();
      consumer = session.createConsumer(atestq);
      Assert.assertNull(consumer.receiveImmediate());

      session.close();
   }
 
Example 20
Source File: PagingWithFailoverAndCountersTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateDeliveryAndCounters() throws Exception {
   startLive();

   ServerLocator locator = SpawnedServerSupport.createLocator(PORT1).setInitialConnectAttempts(300).setReconnectAttempts(300).setRetryInterval(100);

   ClientSessionFactory factory = locator.createSessionFactory();

   ClientSession session = factory.createSession();

   session.createQueue(new QueueConfiguration("DeadConsumer").setAddress("myAddress"));
   session.createQueue(new QueueConfiguration("cons2").setAddress("myAddress"));

   startBackupInProcess();

   waitForRemoteBackup(factory, 10);

   ConsumerThread tConsumer = new ConsumerThread(factory, "cons2", 0, 10);
   tConsumer.start();

   MonitorThread monitor = new MonitorThread();

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

   long i = 0;

   long timeRun = System.currentTimeMillis() + 5000;
   long timeKill = System.currentTimeMillis() + 2000;
   while (System.currentTimeMillis() < timeRun) {
      i++;
      if (System.currentTimeMillis() > timeKill && liveProcess != null) {
         killLive();
         monitor.start();
      }

      try {
         ClientMessage msg = session.createMessage(true);
         msg.putLongProperty("count", i);
         prod.send(msg);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   try {
      tConsumer.stopTest();
      monitor.stopTest();
   } finally {
      killBackup();
      killLive();
   }

   factory.close();

   verifyServer();
}