Esempio n. 1
0
  @Test
  public void testSendPacketsWithFailure() throws Exception {
    final int nMsg = 100;
    final int stop = 37;
    setupServer(true, TestInterceptor.class.getName());

    manager = liveServer.getReplicationManager();
    waitForComponent(manager);
    ClientSessionFactory sf = createSessionFactory(locator);
    final ClientSession session = sf.createSession();
    final ClientSession session2 = sf.createSession();
    session.createQueue(ADDRESS, ADDRESS, null, true);

    final ClientProducer producer = session.createProducer(ADDRESS);

    session.start();
    session2.start();
    try {
      final ClientConsumer consumer = session2.createConsumer(ADDRESS);
      for (int i = 0; i < nMsg; i++) {

        ClientMessage message = session.createMessage(true);
        setBody(i, message);
        message.putIntProperty("counter", i);
        producer.send(message);
        if (i == stop) {
          // Now we start intercepting the communication with the backup
          TestInterceptor.value.set(false);
        }
        ClientMessage msgRcvd = consumer.receive(1000);
        Assert.assertNotNull("Message should exist!", msgRcvd);
        assertMessageBody(i, msgRcvd);
        Assert.assertEquals(i, msgRcvd.getIntProperty("counter").intValue());
        msgRcvd.acknowledge();
      }
    } finally {
      TestInterceptor.value.set(false);
      if (!session.isClosed()) session.close();
      if (!session2.isClosed()) session2.close();
    }
  }
  @Test
  public void testSessionClosedOnRemotingConnectionFailure() throws Exception {
    ClientSession session = addClientSession(sf.createSession());

    session.createQueue("fooaddress", "fooqueue");

    ClientProducer prod = session.createProducer("fooaddress");

    ClientConsumer cons = session.createConsumer("fooqueue");

    session.start();

    prod.send(session.createMessage(false));

    Assert.assertNotNull(cons.receive());

    // Now fail the underlying connection

    RemotingConnection connection = ((ClientSessionInternal) session).getConnection();

    connection.fail(new HornetQNotConnectedException());

    Assert.assertTrue(session.isClosed());

    Assert.assertTrue(prod.isClosed());

    Assert.assertTrue(cons.isClosed());

    // Now try and use the producer

    try {
      prod.send(session.createMessage(false));

      Assert.fail("Should throw exception");
    } catch (HornetQObjectClosedException oce) {
      // ok
    } catch (HornetQException e) {
      fail("Invalid Exception type:" + e.getType());
    }

    try {
      cons.receive();

      Assert.fail("Should throw exception");
    } catch (HornetQObjectClosedException oce) {
      // ok
    } catch (HornetQException e) {
      fail("Invalid Exception type:" + e.getType());
    }

    session.close();
  }