Java Code Examples for org.apache.activemq.ActiveMQConnectionFactory#setProducerWindowSize()

The following examples show how to use org.apache.activemq.ActiveMQConnectionFactory#setProducerWindowSize() . 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: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testProducerFlowControl() throws Exception {
   ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(urlString);

   factory.setProducerWindowSize(1024 * 64);

   Connection connection = factory.createConnection();
   Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
   Queue queue = session.createQueue(queueName);
   MessageProducer producer = session.createProducer(queue);
   producer.send(session.createTextMessage("test"));

   connection.close();
}
 
Example 2
Source File: TopicProducerFlowControlTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void testTopicProducerFlowControl() throws Exception {

      // Create the connection factory
      ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
      connectionFactory.setAlwaysSyncSend(true);
      connectionFactory.setProducerWindowSize(1024);

      ActiveMQPrefetchPolicy prefetchPolicy = new ActiveMQPrefetchPolicy();
      prefetchPolicy.setAll(5000);
      connectionFactory.setPrefetchPolicy(prefetchPolicy);
      // Start the test destination listener
      Connection c = connectionFactory.createConnection();
      c.start();
      Session listenerSession = c.createSession(false, 1);
      Destination destination = createDestination(listenerSession);

      listenerSession.createConsumer(destination).setMessageListener(new TopicProducerFlowControlTest());
      final AtomicInteger blockedCounter = new AtomicInteger(0);
      listenerSession.createConsumer(new ActiveMQTopic(AdvisorySupport.FULL_TOPIC_PREFIX + ">")).setMessageListener(new MessageListener() {
         @Override
         public void onMessage(Message message) {
            LOG.info("Got full advisory, blockedCounter: " + blockedCounter.get());
            blockedCounter.incrementAndGet();
         }
      });

      // Start producing the test messages
      final Session session = connectionFactory.createConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
      final MessageProducer producer = session.createProducer(destination);

      Thread producingThread = new Thread("Producing Thread") {
         @Override
         public void run() {
            try {
               for (long i = 0; i < numMessagesToSend; i++) {
                  producer.send(session.createTextMessage("test"));

                  long count = produced.incrementAndGet();
                  if (count % 10000 == 0) {
                     LOG.info("Produced " + count + " messages");
                  }
               }
            } catch (Throwable ex) {
               ex.printStackTrace();
            } finally {
               try {
                  producer.close();
                  session.close();
               } catch (Exception e) {
               }
            }
         }
      };

      producingThread.start();

      Wait.waitFor(new Wait.Condition() {
         @Override
         public boolean isSatisified() throws Exception {
            return consumed.get() == numMessagesToSend;
         }
      }, 5 * 60 * 1000); // give it plenty of time before failing

      assertEquals("Didn't produce all messages", numMessagesToSend, produced.get());
      assertEquals("Didn't consume all messages", numMessagesToSend, consumed.get());

      assertTrue("Producer got blocked", Wait.waitFor(new Wait.Condition() {
         @Override
         public boolean isSatisified() throws Exception {
            return blockedCounter.get() > 0;
         }
      }, 5 * 1000));
   }