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

The following examples show how to use org.apache.activemq.artemis.api.core.client.ClientMessage#setBodyInputStream() . 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: LVQTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testLargeMessage() throws Exception {
   ClientProducer producer = clientSessionTxReceives.createProducer(address);
   ClientConsumer consumer = clientSessionTxReceives.createConsumer(qName1);
   SimpleString rh = new SimpleString("SMID1");

   for (int i = 0; i < 50; i++) {
      ClientMessage message = clientSession.createMessage(true);
      message.setBodyInputStream(createFakeLargeStream(300 * 1024));
      message.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
      producer.send(message);
      clientSession.commit();
   }
   clientSessionTxReceives.start();
   ClientMessage m = consumer.receive(1000);
   Assert.assertNotNull(m);
   m.acknowledge();
   Assert.assertNull(consumer.receiveImmediate());
   clientSessionTxReceives.commit();
}
 
Example 2
Source File: LVQTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSizeInReplace() throws Exception {
   ClientProducer producer = clientSession.createProducer(address);
   ClientMessage m1 = createTextMessage(clientSession, "m1");
   SimpleString rh = new SimpleString("SMID1");
   m1.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);

   ClientMessage m2 = clientSession.createMessage(true);
   m2.setBodyInputStream(createFakeLargeStream(10 * 1024));
   m2.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);

   Queue queue = server.locateQueue(qName1);
   producer.send(m1);
   long oldSize = queue.getPersistentSize();
   producer.send(m2);
   assertEquals(queue.getDeliveringSize(), 0);
   assertNotEquals(queue.getPersistentSize(), oldSize);
   assertTrue(queue.getPersistentSize() > 10 * 1024);
}
 
Example 3
Source File: ServerLargeMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendServerMessageWithValidatedUser() throws Exception {
   ActiveMQJAASSecurityManager securityManager = new ActiveMQJAASSecurityManager("PropertiesLogin");
   ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(createDefaultInVMConfig().setSecurityEnabled(true), ManagementFactory.getPlatformMBeanServer(), securityManager, false));
   server.getConfiguration().setPopulateValidatedUser(true);

   Role role = new Role("programmers", true, true, true, true, true, true, true, true, true, true);
   Set<Role> roles = new HashSet<>();
   roles.add(role);
   server.getSecurityRepository().addMatch("#", roles);

   server.start();
   ServerLocator locator = createInVMNonHALocator();
   ClientSessionFactory sf = createSessionFactory(locator);

   try {
      ClientSession session = sf.createSession("first", "secret", false, true, true, false, 0);
      ClientMessage clientMessage = session.createMessage(false);
      clientMessage.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE));

      session.createQueue(new QueueConfiguration("A").setRoutingType(RoutingType.ANYCAST));

      ClientProducer prod = session.createProducer("A");
      prod.send(clientMessage);
      session.commit();
      session.start();

      ClientConsumer cons = session.createConsumer("A");
      ClientMessage msg = cons.receive(5000);

      assertEquals("first", msg.getValidatedUserID());
   } finally {
      sf.close();
      locator.close();
      server.stop();
   }
}
 
Example 4
Source File: LargeMessageTestBase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * @param numberOfMessages
 * @param numberOfBytes
 * @param delayDelivery
 * @param session
 * @param producer
 * @throws Exception
 * @throws IOException
 * @throws ActiveMQException
 */
private void sendMessages(final int numberOfMessages,
                          final long numberOfBytes,
                          final long delayDelivery,
                          final ClientSession session,
                          final ClientProducer producer) throws Exception {
   LargeMessageTestBase.log.debug("NumberOfBytes = " + numberOfBytes);
   for (int i = 0; i < numberOfMessages; i++) {
      ClientMessage message = session.createMessage(true);

      // If the test is using more than 1M, we will only use the Streaming, as it require too much memory from the
      // test
      if (numberOfBytes > 1024 * 1024 || i % 2 == 0) {
         LargeMessageTestBase.log.debug("Sending message (stream)" + i);
         message.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(numberOfBytes));
      } else {
         LargeMessageTestBase.log.debug("Sending message (array)" + i);
         byte[] bytes = new byte[(int) numberOfBytes];
         for (int j = 0; j < bytes.length; j++) {
            bytes[j] = ActiveMQTestBase.getSamplebyte(j);
         }
         message.getBodyBuffer().writeBytes(bytes);
      }
      message.putIntProperty(new SimpleString("counter-message"), i);
      if (delayDelivery > 0) {
         long time = System.currentTimeMillis();
         message.putLongProperty(new SimpleString("original-time"), time);
         message.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, time + delayDelivery);

         producer.send(message);
      } else {
         producer.send(message);
      }
   }
}
 
Example 5
Source File: DeadLetterAddressTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testLargeMessageFileLeak() throws Exception {
   OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();

   // only run this on *nix systems which will have the com.sun.management.UnixOperatingSystemMXBean (needed to check open file count)
   Assume.assumeTrue(os instanceof UnixOperatingSystemMXBean);

   long fdBaseline = ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount();
   final int SIZE = 2 * 1024;
   SimpleString dla = new SimpleString("DLA");
   SimpleString qName = new SimpleString("q1");
   SimpleString adName = new SimpleString("ad1");

   AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(1).setDeadLetterAddress(dla);
   server.getAddressSettingsRepository().addMatch(adName.toString(), addressSettings);
   SimpleString dlq = new SimpleString("DLQ1");
   clientSession.createQueue(new QueueConfiguration(dlq).setAddress(dla).setDurable(false));
   clientSession.createQueue(new QueueConfiguration(qName).setAddress(adName).setDurable(false));
   for (int i = 0; i < 10; i++) {
      ClientProducer producer = clientSession.createProducer(adName);
      ClientMessage clientFile = clientSession.createMessage(true);
      clientFile.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(SIZE));
      producer.send(clientFile);
      clientSession.start();
      ClientConsumer clientConsumer = clientSession.createConsumer(qName);
      ClientMessage m = clientConsumer.receive(500);
      m.acknowledge();
      Assert.assertNotNull(m);

      // force a cancel
      clientSession.rollback();
      m = clientConsumer.receiveImmediate();
      Assert.assertNull(m);
      clientConsumer.close();
   }
   assertTrue("File descriptors are leaking", ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount() - fdBaseline <= 0);
}
 
Example 6
Source File: ClusterTestBase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected void sendInRange(final int node,
                           final String address,
                           final int msgStart,
                           final int msgEnd,
                           final boolean durable,
                           final SimpleString key,
                           final SimpleString val) throws Exception {
   ClientSessionFactory sf = sfs[node];

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

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

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

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

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

         message.putStringProperty(key, val);
         message.putIntProperty(ClusterTestBase.COUNT_PROP, i);

         producer.send(message);
      }
   } finally {
      session.close();
   }
}
 
Example 7
Source File: FailoverTestBase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Large message version of {@link #setBody(int, ClientMessage)}.
 *
 * @param i
 * @param message
 */
protected static void setLargeMessageBody(final int i, final ClientMessage message) {
   try {
      message.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(LARGE_MESSAGE_SIZE));
   } catch (Exception e) {
      throw new RuntimeException(e);
   }
}
 
Example 8
Source File: LargeMessageCompressTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testHugeStreamingSpacesCompressed() throws Exception {
   final long messageSize = 1024L * 1024L;

   ActiveMQServer server = createServer(true, isNetty());

   server.start();

   // big enough to hold the whole message compressed on a single message (about 1M on our tests)
   locator.setMinLargeMessageSize(100 * 1024 * 1024);

   ClientSessionFactory sf = createSessionFactory(locator);

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

   session.createQueue(new QueueConfiguration(ADDRESS));

   ClientProducer producer = session.createProducer(ADDRESS);

   ClientMessage clientMessage = session.createMessage(true);

   clientMessage.setBodyInputStream(new InputStream() {
      private long count;

      private boolean closed = false;

      @Override
      public void close() throws IOException {
         super.close();
         closed = true;
      }

      @Override
      public int read() throws IOException {
         if (closed) {
            throw new IOException("Stream was closed");
         }

         if (count++ < messageSize) {
            return ' ';
         } else {
            return -1;
         }
      }
   });

   producer.send(clientMessage);

   session.commit();

   // this is to make sure the message was sent as a regular message (not taking a file on server)
   validateNoFilesOnLargeDir();

   session.start();

   ClientConsumer consumer = session.createConsumer(ADDRESS);
   ClientMessage msg1 = consumer.receive(1000);
   Assert.assertNotNull(msg1);

   final AtomicLong numberOfSpaces = new AtomicLong();

   msg1.saveToOutputStream(new OutputStream() {
      @Override
      public void write(int content) {
         if (content == ' ') {
            numberOfSpaces.incrementAndGet();
         }
      }
   });

   assertEquals(messageSize, numberOfSpaces.get());

   msg1.acknowledge();

   session.commit();

   session.close();
}
 
Example 9
Source File: LargeMessageAvoidLargeMessagesTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testSendRegularAfterCompression() throws Exception {
   ActiveMQServer server = createServer(true, isNetty());
   server.start();

   ClientSessionFactory sf = createSessionFactory(locator);

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

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

   ClientProducer producer = session.createProducer(ADDRESS);

   int minLargeSize = locator.getMinLargeMessageSize();

   TestLargeMessageInputStream input = new TestLargeMessageInputStream(minLargeSize);
   adjustLargeCompression(true, input, 1024);

   int num = 1;
   for (int i = 0; i < num; i++) {
      ClientMessage clientFile = session.createMessage(true);
      clientFile.setBodyInputStream(input.clone());

      producer.send(clientFile);
   }

   session.start();

   //no file should be in the dir as we send it as regular
   validateNoFilesOnLargeDir();

   ClientConsumer consumer = session.createConsumer(ADDRESS);
   for (int j = 0; j < num; j++) {
      ClientMessage msg1 = consumer.receive(1000);
      Assert.assertNotNull(msg1);

      for (int i = 0; i < input.getSize(); i++) {
         byte b = msg1.getBodyBuffer().readByte();
         Assert.assertEquals("incorrect char ", input.getChar(i), b);
      }
      msg1.acknowledge();
   }

   session.commit();
   consumer.close();

   session.close();
}
 
Example 10
Source File: LargeMessageAvoidLargeMessagesTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testSendLargeAfterUnableToSendRegular() throws Exception {
   ActiveMQServer server = createServer(true, isNetty());
   server.start();

   //reduce the minLargeMessageSize to make the test faster
   locator.setMinLargeMessageSize(5 * 1024);
   ClientSessionFactory sf = createSessionFactory(locator);

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

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

   ClientProducer producer = session.createProducer(ADDRESS);

   int minLargeSize = locator.getMinLargeMessageSize();
   TestLargeMessageInputStream input = new TestLargeMessageInputStream(minLargeSize);
   input.setSize(80 * minLargeSize);
   adjustLargeCompression(false, input, 40 * minLargeSize);

   int num = 10;
   for (int i = 0; i < num; i++) {
      ClientMessage clientFile = session.createMessage(true);
      clientFile.setBodyInputStream(input.clone());

      producer.send(clientFile);
   }

   session.commit();

   session.start();

   //no file should be in the dir as we send it as regular
   validateNoFilesOnLargeDir(server.getConfiguration().getLargeMessagesDirectory(), num);

   ClientConsumer consumer = session.createConsumer(ADDRESS);
   for (int j = 0; j < num; j++) {
      ClientMessage msg1 = consumer.receive(1000);
      Assert.assertNotNull(msg1);

      for (int i = 0; i < input.getSize(); i++) {
         byte b = msg1.getBodyBuffer().readByte();
         Assert.assertEquals("incorrect char", input.getChar(i), b);
      }
      msg1.acknowledge();
   }

   session.commit();
   consumer.close();

   session.close();
}
 
Example 11
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 12
Source File: BridgeTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private ClientMessage createLargeMessage(ClientSession session, long largeMessageSize) throws Exception {
   File fileInput = new File(getTemporaryDir(), "huge_message_to_send.dat");

   createFile(fileInput, largeMessageSize);

   instanceLog.debug("File created at: " + fileInput.getAbsolutePath());

   ClientMessage message = session.createMessage(Message.BYTES_TYPE, true);

   FileInputStream fileInputStream = new FileInputStream(fileInput);
   BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);

   message.setBodyInputStream(bufferedInput);

   return message;
}
 
Example 13
Source File: BackupSyncLargeMessageTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * LargeMessages are passed from the client to the server in chunks. Here we test the backup
 * starting the data synchronization with the live in the middle of a multiple chunks large
 * message upload from the client to the live server.
 *
 * @throws Exception
 */
@Test
public void testBackupStartsWhenLiveIsReceivingLargeMessage() throws Exception {
   final ClientSession session = addClientSession(sessionFactory.createSession(true, true));
   session.createQueue(new QueueConfiguration(FailoverTestBase.ADDRESS));
   final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
   final ClientMessage message = session.createMessage(true);
   final int largeMessageSize = 1000 * MIN_LARGE_MESSAGE;
   message.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(largeMessageSize));

   final AtomicBoolean caughtException = new AtomicBoolean(false);
   final CountDownLatch latch = new CountDownLatch(1);
   final CountDownLatch latch2 = new CountDownLatch(1);

   Runnable r = new Runnable() {
      @Override
      public void run() {
         try {
            latch.countDown();
            producer.send(message);
            sendMessages(session, producer, 20);
            session.commit();
         } catch (ActiveMQException e) {
            e.printStackTrace();
            caughtException.set(true);
         } finally {
            latch2.countDown();
         }
      }
   };
   Executors.defaultThreadFactory().newThread(r).start();
   waitForLatch(latch);
   startBackupFinishSyncing();
   ActiveMQTestBase.waitForLatch(latch2);
   crash(session);
   assertFalse("no exceptions while sending message", caughtException.get());

   session.start();
   ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
   ClientMessage msg = consumer.receive(2000);
   ActiveMQBuffer buffer = msg.getBodyBuffer();

   for (int j = 0; j < largeMessageSize; j++) {
      Assert.assertTrue("large msg , expecting " + largeMessageSize + " bytes, got " + j, buffer.readable());
      Assert.assertEquals("equal at " + j, ActiveMQTestBase.getSamplebyte(j), buffer.readByte());
   }
   receiveMessages(consumer, 0, 20, true);
   assertNull("there should be no more messages!", consumer.receiveImmediate());
   consumer.close();
   session.commit();
}
 
Example 14
Source File: LargeMessageOnShutdownTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
@BMRules(
   rules = {
      @BMRule(
         name = "BlockOnFinalLargeMessagePacket",
         targetClass = "org.apache.activemq.artemis.core.server.impl.ServerSessionImpl",
         targetMethod = "doSend(Transaction,Message,SimpleString,boolean,boolean)",
         targetLocation = "EXIT",
         condition = "!flagged(\"testLargeMessageOnShutdown\")",
         action =
            "org.apache.activemq.artemis.tests.extras.byteman.LargeMessageOnShutdownTest.stopServer();" +
            "waitFor(\"testLargeMessageOnShutdown\", 5000);" +
            "flag(\"testLargeMessageOnShutdown\")"
      ),
      @BMRule(
         name = "ReleaseBlockOnSessionCleanup",
         targetClass = "org.apache.activemq.artemis.core.protocol.core.ServerSessionPacketHandler",
         targetMethod = "clearLargeMessage()",
         targetLocation = "EXIT",
         action = "signalWake(\"testLargeMessageOnShutdown\")"
      )
   }
)
public void testLargeMessageOnShutdown() throws Exception {

   byte[] payload = new byte[ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE * 2];

   // Send Large Message
   ClientSessionFactory csf1 = createSessionFactory(createNettyNonHALocator());
   try {
      ClientSession session1 = csf1.createSession();
      ClientProducer producer1 = session1.createProducer(queueName);
      ClientMessage message = session1.createMessage(true);

      message.setBodyInputStream(new ByteArrayInputStream(payload));
      producer1.send(message);
   } catch (Exception e) {
      // Expected due to shutdown.
   } finally {
      csf1.close();
   }

   waitForStoppedServer();
   startServer();

   // Consume Large Message
   ClientSessionFactory csf2 = createSessionFactory(createNettyNonHALocator());
   try {
      ClientSession session2 = csf2.createSession();
      session2.start();
      ClientConsumer consumer2 = session2.createConsumer(queueName);
      ClientMessage rmessage = consumer2.receive(10000);

      assertEquals(payload.length, rmessage.getBodyBuffer().readableBytes());
      assertEqualsBuffers(payload.length, ActiveMQBuffers.wrappedBuffer(payload), rmessage.getBodyBuffer());
   } finally {
      csf2.close();
   }
}
 
Example 15
Source File: LargeMessageTestBase.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
protected ClientMessage createLargeClientMessageStreaming(final ClientSession session,
                                                          final long numberOfBytes,
                                                          final boolean persistent) throws Exception {

   ClientMessage clientMessage = session.createMessage(persistent);

   clientMessage.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(numberOfBytes));

   return clientMessage;
}
 
Example 16
Source File: LargeMessageAvoidLargeMessagesTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
@Test
public void testSimpleSendOnAvoid() throws Exception {
   ActiveMQServer server = createServer(true, isNetty());
   server.start();

   ClientSessionFactory sf = createSessionFactory(locator);

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

   session.createQueue(new QueueConfiguration(ADDRESS));

   ClientProducer producer = session.createProducer(ADDRESS);

   int minLargeSize = locator.getMinLargeMessageSize();

   TestLargeMessageInputStream input = new TestLargeMessageInputStream(minLargeSize);

   ClientMessage clientFile = session.createMessage(true);
   clientFile.setBodyInputStream(input.clone());

   producer.send(clientFile);

   session.start();

   //no file should be in the dir as we send it as regular
   validateNoFilesOnLargeDir();

   ClientConsumer consumer = session.createConsumer(ADDRESS);

   ClientMessage msg1 = consumer.receive(1000);
   Assert.assertNotNull(msg1);

   for (int i = 0; i < input.getSize(); i++) {
      byte b = msg1.getBodyBuffer().readByte();
      Assert.assertEquals("incorrect char ", input.getChar(i), b);
   }
   msg1.acknowledge();
   consumer.close();

   session.close();
}