Exemplo n.º 1
0
 protected void setupCF() throws Exception {
   if (spec.isHasBeenUpdated()) {
     factory = ra.createHornetQConnectionFactory(spec);
   } else {
     factory = ra.getDefaultHornetQConnectionFactory();
   }
 }
  // https://issues.jboss.org/browse/JBPAPP-8017
  @Test
  public void testNonDurableSubscriptionDeleteAfterCrash() throws Exception {
    HornetQResourceAdapter qResourceAdapter = newResourceAdapter();
    qResourceAdapter.setTransactionManagerLocatorClass("");
    qResourceAdapter.setTransactionManagerLocatorMethod("");
    MyBootstrapContext ctx = new MyBootstrapContext();
    qResourceAdapter.start(ctx);
    HornetQActivationSpec spec = new HornetQActivationSpec();
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Topic");
    spec.setDestination("mdbTopic");
    qResourceAdapter.setConnectorClassName(INVM_CONNECTOR_FACTORY);
    CountDownLatch latch = new CountDownLatch(1);
    DummyMessageEndpoint endpoint = new DummyMessageEndpoint(latch);
    DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, false);
    qResourceAdapter.endpointActivation(endpointFactory, spec);

    ClientSession session = locator.createSessionFactory().createSession();
    ClientProducer clientProducer = session.createProducer("jms.topic.mdbTopic");
    ClientMessage message = session.createMessage(true);
    message.getBodyBuffer().writeString("1");
    clientProducer.send(message);

    latch.await(5, TimeUnit.SECONDS);

    assertNotNull(endpoint.lastMessage);
    assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "1");

    HornetQActivation activation = lookupActivation(qResourceAdapter);

    SimpleString tempQueueName = activation.getTopicTemporaryQueue();

    QueueQuery query = session.queueQuery(tempQueueName);
    assertTrue(query.isExists());

    // this should be enough to simulate the crash
    qResourceAdapter.getDefaultHornetQConnectionFactory().close();
    qResourceAdapter.stop();

    query = session.queueQuery(tempQueueName);

    assertFalse(query.isExists());
  }
  @Test
  public void testServerShutdownAndReconnect() throws Exception {
    HornetQResourceAdapter qResourceAdapter = newResourceAdapter();
    qResourceAdapter.setReconnectAttempts(-1);
    qResourceAdapter.setCallTimeout(500L);
    qResourceAdapter.setTransactionManagerLocatorClass("");
    qResourceAdapter.setTransactionManagerLocatorMethod("");
    qResourceAdapter.setRetryInterval(500L);
    MyBootstrapContext ctx = new MyBootstrapContext();
    qResourceAdapter.start(ctx);
    // This is just to register a listener
    final CountDownLatch failedLatch = new CountDownLatch(1);
    ClientSessionFactoryInternal factoryListener =
        (ClientSessionFactoryInternal)
            qResourceAdapter
                .getDefaultHornetQConnectionFactory()
                .getServerLocator()
                .createSessionFactory();
    factoryListener.addFailureListener(
        new SessionFailureListener() {

          @Override
          public void connectionFailed(HornetQException exception, boolean failedOver) {}

          @Override
          public void connectionFailed(
              HornetQException exception, boolean failedOver, String scaleDownTargetNodeID) {
            connectionFailed(exception, failedOver);
          }

          @Override
          public void beforeReconnect(HornetQException exception) {
            failedLatch.countDown();
          }
        });
    HornetQActivationSpec spec = new HornetQActivationSpec();
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Queue");
    spec.setDestination(MDBQUEUE);
    CountDownLatch latch = new CountDownLatch(1);
    DummyMessageEndpoint endpoint = new DummyMessageEndpoint(latch);
    DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, false);
    qResourceAdapter.endpointActivation(endpointFactory, spec);
    ClientSession session = locator.createSessionFactory().createSession();
    ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED);
    ClientMessage message = session.createMessage(true);
    message.getBodyBuffer().writeString("teststring");
    clientProducer.send(message);
    session.close();
    latch.await(5, TimeUnit.SECONDS);

    assertNotNull(endpoint.lastMessage);
    assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "teststring");

    server.stop();

    assertTrue(failedLatch.await(5, TimeUnit.SECONDS));

    qResourceAdapter.endpointDeactivation(endpointFactory, spec);

    qResourceAdapter.stop();
  }