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

The following examples show how to use org.apache.activemq.artemis.api.core.client.ClientMessage#setExpiration() . 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: MessageExpirationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageWithTooLargeExpirationMaxExpiryDelayOverride() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.close();

   session = addClientSession(sf.createSession(false, true, false));
   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(false));

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(false);

   AddressSettings addressSettings = new AddressSettings().setMaxExpiryDelay((long) MAX_EXPIRATION);
   server.getAddressSettingsRepository().addMatch(address.toString(), addressSettings);

   message.setExpiration(System.currentTimeMillis() + (MAX_EXPIRATION * 2));
   producer.send(message);

   long start = System.currentTimeMillis();
   org.apache.activemq.artemis.utils.Wait.assertTrue(() -> server.locateQueue(queue).getMessagesExpired() == 1, MAX_EXPIRATION + 100, 50);
   assertTrue(System.currentTimeMillis() - start <= (MAX_EXPIRATION + 200));

   session.deleteQueue(queue);
}
 
Example 2
Source File: LocalMessageQueue.java    From elasticactors with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    ClientMessage clientMessage = clientSession.createMessage(message.isDurable());
    clientMessage.getBodyBuffer().writeBytes(message.toByteArray());
    clientMessage.putStringProperty("routingKey", routingKey);
    // use the duplicate detection from ActiveMQ
    clientMessage.putBytesProperty(HDR_DUPLICATE_DETECTION_ID, toByteArray(message.getId()));
    // set timeout if needed
    if(message.getTimeout() >= 0) {
        clientMessage.setExpiration(System.currentTimeMillis() + message.getTimeout());
    }
    try {
        producer.send(clientMessage);
    } catch (ActiveMQException e) {
        throw new MessageDeliveryException("IOException while publishing message",e,false);
    } /*catch(SomeRecoverableException e) { @todo: figure out which exceptions are recoverable
        this.recovering.set(true);
        throw new MessageDeliveryException("MessagingService is recovering",true);
    } */
}
 
Example 3
Source File: ExpiryAddressTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasicSendToNoQueue() throws Exception {
   SimpleString ea = new SimpleString("EA");
   SimpleString qName = new SimpleString("q1");
   SimpleString eq = new SimpleString("EQ1");
   SimpleString eq2 = new SimpleString("EQ2");
   clientSession.createQueue(new QueueConfiguration(eq).setAddress(ea).setDurable(false));
   clientSession.createQueue(new QueueConfiguration(eq2).setAddress(ea).setDurable(false));
   clientSession.createQueue(new QueueConfiguration(qName).setDurable(false));
   ClientProducer producer = clientSession.createProducer(qName);
   ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
   clientMessage.setExpiration(System.currentTimeMillis());
   producer.send(clientMessage);
   clientSession.start();
   ClientConsumer clientConsumer = clientSession.createConsumer(qName);
   ClientMessage m = clientConsumer.receiveImmediate();
   Assert.assertNull(m);

   clientConsumer.close();
}
 
Example 4
Source File: MessageExpirationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessagesExpiredNoExpiryAddress() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

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

   Assert.assertEquals(0, server.locateQueue(queue).getMessagesExpired());

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(true);
   message.setExpiration(System.currentTimeMillis() + EXPIRATION);
   producer.send(message);

   Assert.assertTrue(Wait.waitFor(() -> server.locateQueue(queue).getMessagesExpired() == 1, 2000, 100));

   assertEquals(0, server.locateQueue(queue).getMessageCount());
   assertEquals(0, server.locateQueue(queue).getDeliveringCount());

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

   session.close();

   session = addClientSession(sf.createSession(false, true, false));
   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(false));

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(false);

   AddressSettings addressSettings = new AddressSettings().setMinExpiryDelay((long) MIN_EXPIRATION);
   server.getAddressSettingsRepository().addMatch(address.toString(), addressSettings);

   message.setExpiration(System.currentTimeMillis() + (MIN_EXPIRATION / 2));
   producer.send(message);

   long start = System.currentTimeMillis();
   org.apache.activemq.artemis.utils.Wait.assertTrue(() -> server.locateQueue(queue).getMessagesExpired() == 1, MIN_EXPIRATION + 200, 50);
   assertTrue(System.currentTimeMillis() - start > MIN_EXPIRATION);

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

   AddressSettings addressSettings = new AddressSettings().setExpiryAddress(expiryAddress);
   server.getAddressSettingsRepository().addMatch(address.toString(), addressSettings);

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

   Assert.assertEquals(0, server.locateQueue(queue).getMessagesExpired());

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(true);
   message.setExpiration(System.currentTimeMillis() + EXPIRATION);
   producer.send(message);

   Assert.assertTrue(Wait.waitFor(() -> server.locateQueue(queue).getMessagesExpired() == 1, 2000, 100));

   assertEquals(0, server.locateQueue(queue).getMessageCount());
   assertEquals(0, server.locateQueue(queue).getDeliveringCount());

   session.deleteQueue(queue);
}
 
Example 7
Source File: ExpiryRunnerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpireConsumeHalf() throws Exception {
   ClientProducer producer = clientSession.createProducer(qName);
   int numMessages = 100;
   long expiration = System.currentTimeMillis() + 1000;
   for (int i = 0; i < numMessages; i++) {
      ClientMessage m = createTextMessage(clientSession, "m" + i);
      m.setExpiration(expiration);
      producer.send(m);
   }
   ClientConsumer consumer = clientSession.createConsumer(qName);
   clientSession.start();
   for (int i = 0; i < numMessages / 2; i++) {
      ClientMessage cm = consumer.receive(500);
      Assert.assertNotNull("message not received " + i, cm);
      cm.acknowledge();
      Assert.assertEquals("m" + i, cm.getBodyBuffer().readString());
   }
   consumer.close();
   Thread.sleep(2100);
   Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(qName).getBindable()).getMessageCount());
   Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(qName).getBindable()).getDeliveringCount());
}
 
Example 8
Source File: MessageExpirationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageWithAcceptableExpirationMinMaxExpiryDelayOverride() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.close();

   session = addClientSession(sf.createSession(false, true, false));
   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(false));

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(false);

   AddressSettings addressSettings = new AddressSettings().setMinExpiryDelay((long) MIN_EXPIRATION).setMaxExpiryDelay((long) MAX_EXPIRATION);
   server.getAddressSettingsRepository().addMatch(address.toString(), addressSettings);

   message.setExpiration(System.currentTimeMillis() + EXPIRATION);
   producer.send(message);

   long start = System.currentTimeMillis();
   org.apache.activemq.artemis.utils.Wait.assertTrue(() -> server.locateQueue(queue).getMessagesExpired() == 1, EXPIRATION + 100, 50);
   assertTrue(System.currentTimeMillis() - start > MIN_EXPIRATION);
   assertTrue(System.currentTimeMillis() - start < MAX_EXPIRATION);

   session.deleteQueue(queue);
}
 
Example 9
Source File: ExpiryRunnerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpireHalf() throws Exception {
   ClientProducer producer = clientSession.createProducer(qName);
   int numMessages = 100;
   long expiration = System.currentTimeMillis();
   for (int i = 0; i < numMessages; i++) {
      ClientMessage m = createTextMessage(clientSession, "m" + i);
      if (i % 2 == 0) {
         m.setExpiration(expiration);
      }
      producer.send(m);
   }
   Thread.sleep(1600);
   Assert.assertEquals(numMessages / 2, ((Queue) server.getPostOffice().getBinding(qName).getBindable()).getMessageCount());
   Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(qName).getBindable()).getDeliveringCount());
}
 
Example 10
Source File: ExpiryRunnerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpireFromMultipleQueues() throws Exception {
   ClientProducer producer = clientSession.createProducer(qName);
   clientSession.createQueue(new QueueConfiguration(qName2).setDurable(false));
   AddressSettings addressSettings = new AddressSettings().setExpiryAddress(expiryAddress);
   server.getAddressSettingsRepository().addMatch(qName2.toString(), addressSettings);
   ClientProducer producer2 = clientSession.createProducer(qName2);
   int numMessages = 100;
   long expiration = System.currentTimeMillis();
   for (int i = 0; i < numMessages; i++) {
      ClientMessage m = createTextMessage(clientSession, "m" + i);
      m.setExpiration(expiration);
      producer.send(m);
      m = createTextMessage(clientSession, "m" + i);
      m.setExpiration(expiration);
      producer2.send(m);
   }
   Thread.sleep(1600);
   Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(qName).getBindable()).getMessageCount());
   Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(qName).getBindable()).getDeliveringCount());
}
 
Example 11
Source File: ExpiryAddressTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicSend() throws Exception {
   SimpleString ea = new SimpleString("EA");
   SimpleString adSend = new SimpleString("a1");
   SimpleString qName = new SimpleString("q1");
   SimpleString eq = new SimpleString("EA1");
   AddressSettings addressSettings = new AddressSettings().setExpiryAddress(ea);
   server.getAddressSettingsRepository().addMatch("#", addressSettings);
   clientSession.createQueue(new QueueConfiguration(eq).setAddress(ea).setDurable(false));
   clientSession.createQueue(new QueueConfiguration(qName).setAddress(adSend).setDurable(false));

   ClientProducer producer = clientSession.createProducer(adSend);
   ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
   clientMessage.setExpiration(System.currentTimeMillis());
   producer.send(clientMessage);

   clientSession.start();
   ClientConsumer clientConsumer = clientSession.createConsumer(qName);
   ClientMessage m = clientConsumer.receiveImmediate();
   Assert.assertNull(m);
   m = clientConsumer.receiveImmediate();
   Assert.assertNull(m);
   clientConsumer.close();
   clientConsumer = clientSession.createConsumer(eq);
   m = clientConsumer.receive(500);
   Assert.assertNotNull(m);
   Assert.assertEquals(qName.toString(), m.getStringProperty(Message.HDR_ORIGINAL_QUEUE));
   Assert.assertEquals(adSend.toString(), m.getStringProperty(Message.HDR_ORIGINAL_ADDRESS));
   Assert.assertNotNull(m);
   Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
   m.acknowledge();
}
 
Example 12
Source File: PostMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected ClientMessage createActiveMQMessage(HttpHeaders headers,
                                              byte[] body,
                                              boolean durable,
                                              Long ttl,
                                              Long expiration,
                                              Integer priority,
                                              ClientSession session) throws Exception {
   ClientMessage message = session.createMessage(Message.BYTES_TYPE, durable);

   // HORNETQ-962
   UUID uid = UUIDGenerator.getInstance().generateUUID();
   message.setUserID(uid);

   if (expiration != null) {
      message.setExpiration(expiration.longValue());
   } else if (ttl != null) {
      message.setExpiration(System.currentTimeMillis() + ttl.longValue());
   } else if (producerTimeToLive > 0) {
      message.setExpiration(System.currentTimeMillis() + producerTimeToLive);
   }
   if (priority != null) {
      byte p = priority.byteValue();
      if (p >= 0 && p <= 9) {
         message.setPriority(p);
      }
   }
   HttpMessageHelper.writeHttpMessage(headers, body, message);
   return message;
}
 
Example 13
Source File: ExpiryAddressTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpireWithWildcardAddressSettings() throws Exception {
   SimpleString ea = new SimpleString("EA");
   SimpleString qName = new SimpleString("q1");
   SimpleString eq = new SimpleString("EA1");
   AddressSettings addressSettings = new AddressSettings().setExpiryAddress(ea);
   server.getAddressSettingsRepository().addMatch("*", addressSettings);
   clientSession.createQueue(new QueueConfiguration(eq).setAddress(ea).setDurable(false));
   clientSession.createQueue(new QueueConfiguration(qName).setDurable(false));

   ClientProducer producer = clientSession.createProducer(qName);
   ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
   clientMessage.setExpiration(System.currentTimeMillis());
   producer.send(clientMessage);

   clientSession.start();
   ClientConsumer clientConsumer = clientSession.createConsumer(qName);
   ClientMessage m = clientConsumer.receiveImmediate();
   Assert.assertNull(m);
   clientConsumer.close();

   clientConsumer = clientSession.createConsumer(eq);
   m = clientConsumer.receive(500);
   Assert.assertNotNull(m);
   Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
   m.acknowledge();
}
 
Example 14
Source File: MessageExpirationTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageExpirationOnServer() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

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

   ClientProducer producer = session.createProducer(address);
   ClientConsumer consumer = session.createConsumer(queue);
   ClientMessage message = session.createMessage(false);
   message.setExpiration(System.currentTimeMillis() + MessageExpirationTest.EXPIRATION);
   producer.send(message);

   Thread.sleep(MessageExpirationTest.EXPIRATION * 2);

   session.start();

   Thread.sleep(500);

   Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(queue).getBindable()).getDeliveringCount());
   Assert.assertEquals(0, getMessageCount(((Queue) server.getPostOffice().getBinding(queue).getBindable())));

   ClientMessage message2 = consumer.receiveImmediate();
   Assert.assertNull(message2);

   consumer.close();
   session.deleteQueue(queue);
}
 
Example 15
Source File: ExpiryRunnerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicExpire() throws Exception {
   ClientProducer producer = clientSession.createProducer(qName);
   int numMessages = 100;
   long expiration = System.currentTimeMillis();
   for (int i = 0; i < numMessages; i++) {
      ClientMessage m = createTextMessage(clientSession, "m" + i);
      m.setExpiration(expiration);
      producer.send(m);
   }
   Thread.sleep(1600);
   Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(qName).getBindable()).getMessageCount());
   Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(qName).getBindable()).getDeliveringCount());
}
 
Example 16
Source File: ExpiryRunnerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testExpireWhilstConsumingMessagesStillInOrder() throws Exception {
   ClientProducer producer = clientSession.createProducer(qName);
   ClientConsumer consumer = clientSession.createConsumer(qName);
   CountDownLatch latch = new CountDownLatch(1);
   DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(consumer, latch);
   clientSession.start();
   Thread thr = new Thread(dummyMessageHandler);
   thr.start();
   long expiration = System.currentTimeMillis() + 1000;
   int numMessages = 0;
   long sendMessagesUntil = System.currentTimeMillis() + 2000;
   do {
      ClientMessage m = createTextMessage(clientSession, "m" + numMessages++);
      m.setExpiration(expiration);
      producer.send(m);
      Thread.sleep(100);
   }
   while (System.currentTimeMillis() < sendMessagesUntil);
   Assert.assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
   consumer.close();

   consumer = clientSession.createConsumer(expiryQueue);
   do {
      ClientMessage cm = consumer.receive(2000);
      if (cm == null) {
         break;
      }
      String text = cm.getBodyBuffer().readString();
      cm.acknowledge();
      Assert.assertFalse(dummyMessageHandler.payloads.contains(text));
      dummyMessageHandler.payloads.add(text);
   }
   while (true);

   for (int i = 0; i < numMessages; i++) {
      if (dummyMessageHandler.payloads.isEmpty()) {
         break;
      }
      Assert.assertTrue("m" + i, dummyMessageHandler.payloads.remove("m" + i));
   }
   consumer.close();
   thr.join();
}
 
Example 17
Source File: ConsumerWindowSizeTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private void internalTestSlowConsumerNoBuffer(final boolean largeMessages) throws Exception {
   ActiveMQServer server = createServer(false, isNetty());

   ClientSession sessionB = null;
   ClientSession session = null;

   try {
      final int numberOfMessages = 100;

      server.start();

      locator.setConsumerWindowSize(0);
      if (largeMessages) {
         locator.setMinLargeMessageSize(100);
      }

      ClientSessionFactory sf = createSessionFactory(locator);

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

      SimpleString ADDRESS = addressA;

      session.createQueue(new QueueConfiguration(ADDRESS));

      sessionB = sf.createSession(false, true, true);
      sessionB.start();

      session.start();

      ClientConsumerInternal consNeverUsed = (ClientConsumerInternal) sessionB.createConsumer(ADDRESS);

      ClientProducer prod = session.createProducer(ADDRESS);

      // This will force a credit to be sent, but if the message wasn't received we need to take out that credit from
      // the server
      // or the client will be buffering messages
      Assert.assertNull(consNeverUsed.receive(1));

      ClientMessage msg = createTextMessage(session, "This one will expire");
      if (largeMessages) {
         msg.getBodyBuffer().writeBytes(new byte[600]);
      }

      msg.setExpiration(System.currentTimeMillis() + 100);
      prod.send(msg);

      msg = createTextMessage(session, "First-on-non-buffered");

      prod.send(msg);

      Thread.sleep(110);

      // It will be able to receive another message, but it shouldn't send a credit again, as the credit was already
      // sent
      msg = consNeverUsed.receive(TIMEOUT * 1000);
      Assert.assertNotNull(msg);
      Assert.assertEquals("First-on-non-buffered", getTextMessage(msg));
      msg.acknowledge();

      ClientConsumer cons1 = session.createConsumer(ADDRESS);

      for (int i = 0; i < numberOfMessages; i++) {
         msg = createTextMessage(session, "Msg" + i);

         if (largeMessages) {
            msg.getBodyBuffer().writeBytes(new byte[600]);
         }

         prod.send(msg);
      }

      for (int i = 0; i < numberOfMessages; i++) {
         msg = cons1.receive(1000);
         Assert.assertNotNull("expected message at i = " + i, msg);
         Assert.assertEquals("Msg" + i, getTextMessage(msg));
         msg.acknowledge();
      }

      Assert.assertEquals(0, consNeverUsed.getBufferSize());

      session.close();
      session = null;

      sessionB.close();
      sessionB = null;

      Assert.assertEquals(0, getMessageCount(server, ADDRESS.toString()));

   } finally {
      try {
         if (session != null) {
            session.close();
         }
         if (sessionB != null) {
            sessionB.close();
         }
      } catch (Exception ignored) {
      }
   }
}
 
Example 18
Source File: MessageExpirationTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageExpiredWithExpiryAddress() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();
   final SimpleString expiryAddress = RandomUtil.randomSimpleString();
   SimpleString expiryQueue = RandomUtil.randomSimpleString();

   server.getAddressSettingsRepository().addMatch(address.toString(), new AddressSettings() {
      private static final long serialVersionUID = -6476053400596299130L;

      @Override
      public SimpleString getExpiryAddress() {
         return expiryAddress;
      }
   });

   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(false));
   session.createQueue(new QueueConfiguration(expiryQueue).setAddress(expiryAddress).setDurable(false));

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(false);
   message.setExpiration(System.currentTimeMillis() + MessageExpirationTest.EXPIRATION);
   producer.send(message);

   Thread.sleep(MessageExpirationTest.EXPIRATION * 2);

   session.start();

   ClientConsumer consumer = session.createConsumer(queue);
   ClientMessage message2 = consumer.receiveImmediate();
   Assert.assertNull(message2);

   ClientConsumer expiryConsumer = session.createConsumer(expiryQueue);
   ClientMessage expiredMessage = expiryConsumer.receive(500);
   Assert.assertNotNull(expiredMessage);
   Assert.assertNotNull(expiredMessage.getObjectProperty(Message.HDR_ACTUAL_EXPIRY_TIME));
   Assert.assertEquals(address, expiredMessage.getObjectProperty(Message.HDR_ORIGINAL_ADDRESS));
   Assert.assertEquals(queue, expiredMessage.getObjectProperty(Message.HDR_ORIGINAL_QUEUE));
   consumer.close();
   expiryConsumer.close();
   session.deleteQueue(queue);
   session.deleteQueue(expiryQueue);
}
 
Example 19
Source File: MessageExpirationTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageExpiredWithoutExpiryAddressWithExpiryDelayOverride() throws Exception {
   SimpleString address = RandomUtil.randomSimpleString();
   SimpleString queue = RandomUtil.randomSimpleString();

   session.close();

   session = addClientSession(sf.createSession(false, false, false));
   session.createQueue(new QueueConfiguration(queue).setAddress(address).setDurable(false));

   ClientProducer producer = session.createProducer(address);
   ClientMessage message = session.createMessage(false);

   AddressSettings addressSettings = new AddressSettings().setExpiryDelay((long) MessageExpirationTest.EXPIRATION);
   server.getAddressSettingsRepository().addMatch(address.toString(), addressSettings);

   producer.send(message);

   // second message, this message shouldn't be overridden
   message = session.createMessage(false);
   message.setExpiration(System.currentTimeMillis() + EXPIRATION * 3);
   producer.send(message);
   session.commit();

   session.start();
   ClientConsumer consumer = session.createConsumer(queue);
   // non expired.. should receive both
   assertNotNull(consumer.receiveImmediate());
   assertNotNull(consumer.receiveImmediate());

   // stopping the consumer to cleanup the client's buffer
   session.stop();

   // we receive the message and then rollback...   then we wait some time > expiration, the message must be gone
   session.rollback();

   Thread.sleep(MessageExpirationTest.EXPIRATION * 2);
   session.start();

   // one expired as we changed the expiry in one of the messages... should receive just one
   assertNotNull(consumer.receiveImmediate());
   assertNull(consumer.receiveImmediate());

   session.stop();
   session.rollback();

   Thread.sleep(MessageExpirationTest.EXPIRATION * 2);
   session.start();

   // both expired... nothing should be received
   assertNull(consumer.receiveImmediate());

   consumer.close();
   session.deleteQueue(queue);
}
 
Example 20
Source File: ExpiryAddressTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
@Test
public void testBasicSendToMultipleQueues() throws Exception {
   SimpleString ea = new SimpleString("EA");
   SimpleString qName = new SimpleString("q1");
   SimpleString eq = new SimpleString("EQ1");
   SimpleString eq2 = new SimpleString("EQ2");
   AddressSettings addressSettings = new AddressSettings().setExpiryAddress(ea);
   server.getAddressSettingsRepository().addMatch(qName.toString(), addressSettings);
   clientSession.createQueue(new QueueConfiguration(eq).setAddress(ea).setDurable(false));
   clientSession.createQueue(new QueueConfiguration(eq2).setAddress(ea).setDurable(false));
   clientSession.createQueue(new QueueConfiguration(qName).setDurable(false));
   ClientProducer producer = clientSession.createProducer(qName);
   ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
   clientMessage.setExpiration(System.currentTimeMillis());

   producer.send(clientMessage);

   clientSession.start();
   ClientConsumer clientConsumer = clientSession.createConsumer(qName);
   ClientMessage m = clientConsumer.receiveImmediate();

   Assert.assertNull(m);

   clientConsumer.close();

   clientConsumer = clientSession.createConsumer(eq);

   m = clientConsumer.receive(500);

   Assert.assertNotNull(m);

   assertNotNull(m.getStringProperty(Message.HDR_ORIGINAL_ADDRESS));
   assertNotNull(m.getStringProperty(Message.HDR_ORIGINAL_QUEUE));

   m.acknowledge();

   Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");

   clientConsumer.close();

   clientConsumer = clientSession.createConsumer(eq2);

   m = clientConsumer.receive(500);

   Assert.assertNotNull(m);

   assertNotNull(m.getStringProperty(Message.HDR_ORIGINAL_ADDRESS));
   assertNotNull(m.getStringProperty(Message.HDR_ORIGINAL_QUEUE));

   m.acknowledge();

   Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");

   clientConsumer.close();

   clientSession.commit();
}