Ejemplo n.º 1
0
  private ActiveMQConnectionFactory createFactory() {

    // connect to the embedded broker defined above
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(Info.BROKER_DEFAULT);

    // tuned for performances
    // http://activemq.apache.org/performance-tuning.html
    factory.setUseAsyncSend(true);
    factory.setOptimizeAcknowledge(true);
    factory.setAlwaysSessionAsync(true);
    factory.setObjectMessageSerializationDefered(true);
    factory.setCopyMessageOnSend(false);

    return factory;
  }
Ejemplo n.º 2
0
 protected ConnectionFactory createConnectionFactory(final BrokerService broker) throws Exception {
   String url = broker.getTransportConnectors().get(0).getServer().getConnectURI().toString();
   ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
   connectionFactory.setOptimizedMessageDispatch(true);
   connectionFactory.setCopyMessageOnSend(false);
   connectionFactory.setUseCompression(false);
   connectionFactory.setDispatchAsync(false);
   connectionFactory.setUseAsyncSend(false);
   connectionFactory.setOptimizeAcknowledge(false);
   connectionFactory.setWatchTopicAdvisories(true);
   ActiveMQPrefetchPolicy qPrefetchPolicy = new ActiveMQPrefetchPolicy();
   qPrefetchPolicy.setQueuePrefetch(100);
   qPrefetchPolicy.setTopicPrefetch(1000);
   connectionFactory.setPrefetchPolicy(qPrefetchPolicy);
   connectionFactory.setAlwaysSyncSend(true);
   return connectionFactory;
 }
  @Test
  public void testCloseWhenHunk() throws Exception {

    ActiveMQConnectionFactory connectionFactory1 = new ActiveMQConnectionFactory();
    connectionFactory1.setBrokerURL(
        "vm://localhost?brokerConfig=xbean:embedded-activemq-config.xml");
    connectionFactory1.setUseAsyncSend(true);
    connectionFactory1.setWatchTopicAdvisories(false);
    connectionFactory1.setOptimizeAcknowledge(true);
    connectionFactory1.setAlwaysSessionAsync(false);

    // start up the embedded broker which is running TCP on non-standard port
    connectionFactory1.createConnection().start();

    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    connectionFactory.setBrokerURL("tcp://localhost:61666");

    // TINY QUEUE is flow controlled after 1024 bytes
    final ActiveMQDestination destination =
        ActiveMQDestination.createDestination("queue://TINY_QUEUE", (byte) 0xff);

    Connection connection = connectionFactory.createConnection();
    connection.start();
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final MessageProducer producer = session.createProducer(destination);
    producer.setTimeToLive(0);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

    final AtomicReference<Exception> publishException = new AtomicReference<Exception>(null);
    final AtomicReference<Exception> closeException = new AtomicReference<Exception>(null);
    final AtomicLong lastLoop = new AtomicLong(System.currentTimeMillis() + 100);

    Thread pubThread =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                try {
                  byte[] data = new byte[1000];
                  new Random(0xdeadbeef).nextBytes(data);
                  for (int i = 0; i < 10000; i++) {
                    lastLoop.set(System.currentTimeMillis());
                    ObjectMessage objMsg = session.createObjectMessage();
                    objMsg.setObject(data);
                    producer.send(destination, objMsg);
                  }
                } catch (Exception e) {
                  publishException.set(e);
                }
              }
            },
            "PublishingThread");
    pubThread.start();

    // wait for publisher to deadlock
    while (System.currentTimeMillis() - lastLoop.get() < 1000) {
      Thread.sleep(100);
    }
    System.out.println("Publisher deadlock detected.");

    Thread closeThread =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                try {
                  System.out.println("Attempting close..");
                  producer.close();
                } catch (Exception e) {
                  closeException.set(e);
                }
              }
            },
            "ClosingThread");
    closeThread.start();

    try {
      closeThread.join(10000);
    } catch (InterruptedException ie) {
      assertFalse("Closing thread didn't complete in 10 seconds", true);
    }

    try {
      pubThread.join(10000);
    } catch (InterruptedException ie) {
      assertFalse("Publishing thread didn't complete in 10 seconds", true);
    }

    assertNull(closeException.get());
    assertNotNull(publishException.get());
  }