Beispiel #1
0
  private void addMessageIDInHeader(final Message msg) throws Exception {
    // We concatenate the old message id as a header in the message
    // This allows the target to then use this as the JMSCorrelationID of any response message
    // thus enabling a distributed request-response pattern.
    // Each bridge (if there are more than one) in the chain can concatenate the message id
    // So in the case of multiple bridges having routed the message this can be used in a multi-hop
    // distributed request/response
    if (JMSBridgeImpl.trace) {
      HornetQJMSServerLogger.LOGGER.trace("Adding old message id in Message header");
    }

    JMSBridgeImpl.copyProperties(msg);

    String val = null;

    val = msg.getStringProperty(HornetQJMSConstants.JBOSS_MESSAGING_BRIDGE_MESSAGE_ID_LIST);

    if (val == null) {
      val = msg.getJMSMessageID();
    } else {
      StringBuffer sb = new StringBuffer(val);

      sb.append(",").append(msg.getJMSMessageID());

      val = sb.toString();
    }

    msg.setStringProperty(HornetQJMSConstants.JBOSS_MESSAGING_BRIDGE_MESSAGE_ID_LIST, val);
  }
Beispiel #2
0
  public synchronized void setTargetConnectionFactoryFactory(final ConnectionFactoryFactory cff) {
    checkBridgeNotStarted();
    JMSBridgeImpl.checkNotNull(cff, "TargetConnectionFactoryFactory");

    targetCff = cff;
  }
Beispiel #3
0
  public synchronized void setMaxBatchTime(final long time) {
    checkBridgeNotStarted();
    JMSBridgeImpl.checkValidValue(time, "MaxBatchTime");

    maxBatchTime = time;
  }
Beispiel #4
0
  public synchronized void setMaxBatchSize(final int size) {
    checkBridgeNotStarted();
    JMSBridgeImpl.checkMaxBatchSize(size);

    maxBatchSize = size;
  }
Beispiel #5
0
  public synchronized void setQualityOfServiceMode(final QualityOfServiceMode mode) {
    checkBridgeNotStarted();
    JMSBridgeImpl.checkNotNull(mode, "QualityOfServiceMode");

    qualityOfServiceMode = mode;
  }
Beispiel #6
0
  public synchronized void setMaxRetries(final int retries) {
    checkBridgeNotStarted();
    JMSBridgeImpl.checkValidValue(retries, "MaxRetries");

    maxRetries = retries;
  }
Beispiel #7
0
  public void setTargetDestinationFactory(final DestinationFactory dest) {
    checkBridgeNotStarted();
    JMSBridgeImpl.checkNotNull(dest, "TargetDestinationFactory");

    targetDestinationFactory = dest;
  }
Beispiel #8
0
  @Test
  public void testExceptionOnSourceAndRetryFails() throws Exception {
    final AtomicReference<Connection> sourceConn = new AtomicReference<Connection>();
    HornetQJMSConnectionFactory failingSourceCF =
        new HornetQJMSConnectionFactory(false, new TransportConfiguration(INVM_CONNECTOR_FACTORY)) {
          private static final long serialVersionUID = 8216804886099984645L;
          boolean firstTime = true;

          @Override
          public Connection createConnection() throws JMSException {
            if (firstTime) {
              firstTime = false;
              sourceConn.set(super.createConnection());
              return sourceConn.get();
            } else {
              throw new JMSException("exception while retrying to connect");
            }
          }
        };
    // Note! We disable automatic reconnection on the session factory. The bridge needs to do the
    // reconnection
    failingSourceCF.setReconnectAttempts(0);
    failingSourceCF.setBlockOnNonDurableSend(true);
    failingSourceCF.setBlockOnDurableSend(true);

    ConnectionFactoryFactory sourceCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(failingSourceCF);
    ConnectionFactoryFactory targetCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();

    JMSBridgeImpl bridge = new JMSBridgeImpl();
    Assert.assertNotNull(bridge);

    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    bridge.setFailureRetryInterval(100);
    bridge.setMaxRetries(1);
    bridge.setMaxBatchSize(1);
    bridge.setMaxBatchTime(-1);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);

    Assert.assertFalse(bridge.isStarted());
    bridge.start();
    Assert.assertTrue(bridge.isStarted());

    sourceConn
        .get()
        .getExceptionListener()
        .onException(new JMSException("exception on the source"));
    Thread.sleep(4 * bridge.getFailureRetryInterval());
    // reconnection must have failed
    Assert.assertFalse(bridge.isStarted());
  }
Beispiel #9
0
  @Test
  public void testExceptionOnSourceAndRetrySucceeds() throws Exception {
    final AtomicReference<Connection> sourceConn = new AtomicReference<Connection>();
    HornetQJMSConnectionFactory failingSourceCF =
        new HornetQJMSConnectionFactory(
            false, new TransportConfiguration(InVMConnectorFactory.class.getName())) {
          private static final long serialVersionUID = -8866390811966688830L;

          @Override
          public Connection createConnection() throws JMSException {
            sourceConn.set(super.createConnection());
            return sourceConn.get();
          }
        };
    // Note! We disable automatic reconnection on the session factory. The bridge needs to do the
    // reconnection
    failingSourceCF.setReconnectAttempts(0);
    failingSourceCF.setBlockOnNonDurableSend(true);
    failingSourceCF.setBlockOnDurableSend(true);

    ConnectionFactoryFactory sourceCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(failingSourceCF);
    ConnectionFactoryFactory targetCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();

    JMSBridgeImpl bridge = new JMSBridgeImpl();
    Assert.assertNotNull(bridge);

    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    bridge.setFailureRetryInterval(10);
    bridge.setMaxRetries(2);
    bridge.setMaxBatchSize(1);
    bridge.setMaxBatchTime(-1);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);

    Assert.assertFalse(bridge.isStarted());
    bridge.start();
    Assert.assertTrue(bridge.isStarted());

    sourceConn
        .get()
        .getExceptionListener()
        .onException(new JMSException("exception on the source"));
    Thread.sleep(4 * bridge.getFailureRetryInterval());
    // reconnection must have succeeded
    Assert.assertTrue(bridge.isStarted());

    bridge.stop();
    Assert.assertFalse(bridge.isStarted());
  }
Beispiel #10
0
  @Test
  public void testSendMessagesWithMaxBatchSize() throws Exception {
    final int numMessages = 10;

    ConnectionFactoryFactory sourceCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    ConnectionFactoryFactory targetCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();

    JMSBridgeImpl bridge = new JMSBridgeImpl();
    Assert.assertNotNull(bridge);

    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    bridge.setFailureRetryInterval(10);
    bridge.setMaxRetries(-1);
    bridge.setMaxBatchSize(numMessages);
    bridge.setMaxBatchTime(-1);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);

    Assert.assertFalse(bridge.isStarted());
    bridge.start();
    Assert.assertTrue(bridge.isStarted());

    Connection targetConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
    Session targetSess = targetConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = targetSess.createConsumer(targetDF.createDestination());
    final List<Message> messages = new LinkedList<Message>();
    final CountDownLatch latch = new CountDownLatch(numMessages);
    MessageListener listener =
        new MessageListener() {
          public void onMessage(final Message message) {
            messages.add(message);
            latch.countDown();
          }
        };
    consumer.setMessageListener(listener);
    targetConn.start();

    Connection sourceConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
    Session sourceSess = sourceConn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    MessageProducer producer = sourceSess.createProducer(sourceDF.createDestination());

    for (int i = 0; i < numMessages - 1; i++) {
      TextMessage msg = sourceSess.createTextMessage();
      producer.send(msg);
      JMSBridgeImplTest.log.info("sent message " + i);
    }

    Thread.sleep(1000);

    Assert.assertEquals(0, messages.size());

    TextMessage msg = sourceSess.createTextMessage();

    producer.send(msg);

    Assert.assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));

    sourceConn.close();

    Assert.assertEquals(numMessages, messages.size());

    bridge.stop();
    Assert.assertFalse(bridge.isStarted());

    targetConn.close();
  }
Beispiel #11
0
  /*
   * we receive only 1 message. The message is sent when the maxBatchTime
   * expires even if the maxBatchSize is not reached
   */
  @Test
  public void testSendMessagesWhenMaxBatchTimeExpires() throws Exception {
    int maxBatchSize = 2;
    long maxBatchTime = 500;

    ConnectionFactoryFactory sourceCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    ConnectionFactoryFactory targetCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();

    JMSBridgeImpl bridge = new JMSBridgeImpl();
    Assert.assertNotNull(bridge);

    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    bridge.setFailureRetryInterval(10);
    bridge.setMaxRetries(-1);
    bridge.setMaxBatchSize(maxBatchSize);
    bridge.setMaxBatchTime(maxBatchTime);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);

    Assert.assertFalse(bridge.isStarted());
    bridge.start();
    Assert.assertTrue(bridge.isStarted());

    Connection targetConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
    Session targetSess = targetConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = targetSess.createConsumer(targetDF.createDestination());
    final List<Message> messages = new LinkedList<Message>();
    MessageListener listener =
        new MessageListener() {

          public void onMessage(final Message message) {
            messages.add(message);
          }
        };
    consumer.setMessageListener(listener);
    targetConn.start();

    Connection sourceConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
    Session sourceSess = sourceConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = sourceSess.createProducer(sourceDF.createDestination());
    producer.send(sourceSess.createTextMessage());
    sourceConn.close();

    Assert.assertEquals(0, messages.size());
    Thread.sleep(3 * maxBatchTime);

    Assert.assertEquals(1, messages.size());

    bridge.stop();
    Assert.assertFalse(bridge.isStarted());

    targetConn.close();
  }
Beispiel #12
0
  @Test
  public void testStartWithFailureThenSuccess() throws Exception {
    HornetQJMSConnectionFactory failingSourceCF =
        new HornetQJMSConnectionFactory(
            false, new TransportConfiguration(InVMConnectorFactory.class.getName())) {
          private static final long serialVersionUID = 4657153922210359725L;
          boolean firstTime = true;

          @Override
          public Connection createConnection() throws JMSException {
            if (firstTime) {
              firstTime = false;
              throw new JMSException("unable to create a conn");
            } else {
              return super.createConnection();
            }
          }
        };
    // Note! We disable automatic reconnection on the session factory. The bridge needs to do the
    // reconnection
    failingSourceCF.setReconnectAttempts(0);
    failingSourceCF.setBlockOnNonDurableSend(true);
    failingSourceCF.setBlockOnDurableSend(true);

    ConnectionFactoryFactory sourceCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(failingSourceCF);
    ConnectionFactoryFactory targetCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();

    JMSBridgeImpl bridge = new JMSBridgeImpl();

    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    // retry after 10 ms
    bridge.setFailureRetryInterval(10);
    // retry only once
    bridge.setMaxRetries(1);
    bridge.setMaxBatchSize(1);
    bridge.setMaxBatchTime(-1);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);

    Assert.assertFalse(bridge.isStarted());
    bridge.start();

    Thread.sleep(500);
    Assert.assertTrue(bridge.isStarted());
    Assert.assertFalse(bridge.isFailed());

    bridge.stop();
  }
Beispiel #13
0
  @Test
  public void testStartWithRepeatedFailure() throws Exception {
    HornetQJMSConnectionFactory failingSourceCF =
        new HornetQJMSConnectionFactory(
            false, new TransportConfiguration(InVMConnectorFactory.class.getName())) {
          private static final long serialVersionUID = 2834937512213001068L;

          @Override
          public Connection createConnection() throws JMSException {
            throw new JMSException("unable to create a conn");
          }
        };

    ConnectionFactoryFactory sourceCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(failingSourceCF);
    ConnectionFactoryFactory targetCFF =
        JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF =
        JMSBridgeImplTest.newDestinationFactory(
            HornetQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();

    JMSBridgeImpl bridge = new JMSBridgeImpl();

    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    // retry after 10 ms
    bridge.setFailureRetryInterval(10);
    // retry only once
    bridge.setMaxRetries(1);
    bridge.setMaxBatchSize(1);
    bridge.setMaxBatchTime(-1);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);

    Assert.assertFalse(bridge.isStarted());
    bridge.start();

    Thread.sleep(50);
    Assert.assertFalse(bridge.isStarted());
    Assert.assertTrue(bridge.isFailed());

    bridge.stop();
  }