Java Code Examples for org.apache.activemq.broker.BrokerService#getPersistenceAdapter()

The following examples show how to use org.apache.activemq.broker.BrokerService#getPersistenceAdapter() . 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: kahaDbJdbcLeaseQueueMasterSlaveTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected void createMaster() throws Exception {
   master = new BrokerService();
   master.setBrokerName("master");
   master.addConnector(MASTER_URL);
   master.setUseJmx(false);
   master.setPersistent(true);
   master.setDeleteAllMessagesOnStartup(true);
   KahaDBPersistenceAdapter kahaDBPersistenceAdapter = (KahaDBPersistenceAdapter) master.getPersistenceAdapter();
   LeaseDatabaseLocker leaseDatabaseLocker = new LeaseDatabaseLocker();
   leaseDatabaseLocker.setCreateTablesOnStartup(true);
   leaseDatabaseLocker.setDataSource(getExistingDataSource());
   leaseDatabaseLocker.setStatements(new Statements());
   kahaDBPersistenceAdapter.setLocker(leaseDatabaseLocker);
   configureLocker(kahaDBPersistenceAdapter);
   configureBroker(master);
   master.start();
}
 
Example 2
Source File: ConfigTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testJournalConfig() throws Exception {
   File journalFile = new File(JOURNAL_ROOT + "testJournalConfig/journal");
   recursiveDelete(journalFile);

   BrokerService broker;
   broker = createBroker(new FileSystemResource(CONF_ROOT + "journal-example.xml"));
   try {
      assertEquals("Broker Config Error (brokerName)", "brokerJournalConfigTest", broker.getBrokerName());

      PersistenceAdapter adapter = broker.getPersistenceAdapter();

      assertTrue("Should have created a journal persistence adapter", adapter instanceof JournalPersistenceAdapter);
      assertTrue("Should have created a journal directory at " + journalFile.getAbsolutePath(), journalFile.exists());

      LOG.info("Success");
   } finally {
      if (broker != null) {
         broker.stop();
      }
   }
}
 
Example 3
Source File: kahaDbJdbcLeaseQueueMasterSlaveTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected void createSlave() throws Exception {
   // use a separate thread as the slave will block waiting for
   // the exclusive db lock
   Thread t = new Thread() {
      @Override
      public void run() {
         try {
            BrokerService broker = new BrokerService();
            broker.setBrokerName("slave");
            TransportConnector connector = new TransportConnector();
            connector.setUri(new URI(SLAVE_URL));
            broker.addConnector(connector);
            broker.setUseJmx(false);
            broker.setPersistent(true);
            KahaDBPersistenceAdapter kahaDBPersistenceAdapter = (KahaDBPersistenceAdapter) broker.getPersistenceAdapter();
            LeaseDatabaseLocker leaseDatabaseLocker = new LeaseDatabaseLocker();
            leaseDatabaseLocker.setDataSource(getExistingDataSource());
            leaseDatabaseLocker.setStatements(new Statements());
            kahaDBPersistenceAdapter.setLocker(leaseDatabaseLocker);
            configureLocker(kahaDBPersistenceAdapter);
            configureBroker(broker);
            broker.start();
            slave.set(broker);
            slaveStarted.countDown();
         } catch (IllegalStateException expectedOnShutdown) {
         } catch (Exception e) {
            fail("failed to start slave broker, reason:" + e);
         }
      }
   };
   t.start();
}
 
Example 4
Source File: ConfigTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testJournaledJDBCConfig() throws Exception {

   File journalFile = new File(JOURNAL_ROOT + "testJournaledJDBCConfig/journal");
   recursiveDelete(journalFile);

   File derbyFile = new File(DERBY_ROOT + "testJournaledJDBCConfig/derbydb"); // Default
   recursiveDelete(derbyFile);

   BrokerService broker;
   broker = createBroker(new FileSystemResource(CONF_ROOT + "journaledjdbc-example.xml"));
   try {
      assertEquals("Broker Config Error (brokerName)", "brokerJournaledJDBCConfigTest", broker.getBrokerName());

      PersistenceAdapter adapter = broker.getPersistenceAdapter();

      assertTrue("Should have created a journal persistence adapter", adapter instanceof JournalPersistenceAdapter);
      assertTrue("Should have created a derby directory at " + derbyFile.getAbsolutePath(), derbyFile.exists());
      assertTrue("Should have created a journal directory at " + journalFile.getAbsolutePath(), journalFile.exists());

      // Check persistence factory configurations
      broker.getPersistenceAdapter();

      assertTrue(broker.getSystemUsage().getStoreUsage().getStore() instanceof JournalPersistenceAdapter);

      LOG.info("Success");
   } finally {
      if (broker != null) {
         broker.stop();
      }
   }
}
 
Example 5
Source File: ConfigTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testMemoryConfig() throws Exception {
   File journalFile = new File(JOURNAL_ROOT + "testMemoryConfig");
   recursiveDelete(journalFile);

   File derbyFile = new File(DERBY_ROOT + "testMemoryConfig");
   recursiveDelete(derbyFile);

   BrokerService broker;
   broker = createBroker(new FileSystemResource(CONF_ROOT + "memory-example.xml"));

   try {
      assertEquals("Broker Config Error (brokerName)", "brokerMemoryConfigTest", broker.getBrokerName());

      PersistenceAdapter adapter = broker.getPersistenceAdapter();

      assertTrue("Should have created a memory persistence adapter", adapter instanceof MemoryPersistenceAdapter);
      assertTrue("Should have not created a derby directory at " + derbyFile.getAbsolutePath(), !derbyFile.exists());
      assertTrue("Should have not created a journal directory at " + journalFile.getAbsolutePath(), !journalFile.exists());

      LOG.info("Success");
   } finally {
      if (broker != null) {
         broker.stop();
      }
   }
}
 
Example 6
Source File: JDBCConfigTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testJdbcConfig() throws Exception {
   File journalFile = new File(JOURNAL_ROOT + "testJDBCConfig/journal");
   recursiveDelete(journalFile);

   File derbyFile = new File(DERBY_ROOT + "testJDBCConfig/derbydb"); // Default
   recursiveDelete(derbyFile);

   BrokerService broker;
   broker = createBroker(new FileSystemResource(CONF_ROOT + "jdbc-example.xml"));
   try {
      assertEquals("Broker Config Error (brokerName)", "brokerJdbcConfigTest", broker.getBrokerName());

      PersistenceAdapter adapter = broker.getPersistenceAdapter();

      assertTrue("Should have created a jdbc persistence adapter", adapter instanceof JDBCPersistenceAdapter);
      assertEquals("JDBC Adapter Config Error (cleanupPeriod)", 60000, ((JDBCPersistenceAdapter) adapter).getCleanupPeriod());
      assertTrue("Should have created an EmbeddedDataSource", ((JDBCPersistenceAdapter) adapter).getDataSource() instanceof EmbeddedDataSource);
      assertTrue("Should have created a DefaultWireFormat", ((JDBCPersistenceAdapter) adapter).getWireFormat() instanceof ObjectStreamWireFormat);

      LOG.info("Success");
   } finally {
      if (broker != null) {
         broker.stop();
      }
   }
}
 
Example 7
Source File: OpenEjbBrokerFactoryTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void testNoDataSource() throws Exception {
    final BrokerService broker = BrokerFactory.createBroker(new URI(getBrokerUri(
        "broker:(tcp://localhost:" + brokerPort + ")?useJmx=false")));
    assertNotNull("broker is null", broker);

    final PersistenceAdapter persistenceAdapter = broker.getPersistenceAdapter();
    assertNotNull("persistenceAdapter is null", persistenceAdapter);

    assertTrue("persistenceAdapter should be an instance of MemoryPersistenceAdapter",
        persistenceAdapter instanceof MemoryPersistenceAdapter);

    stopBroker(broker);
}
 
Example 8
Source File: OpenEjbBrokerFactoryTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void testDirectDataSource() throws Exception {

        final Properties properties = new Properties();

        final JDBCDataSource dataSource = new JDBCDataSource();
        dataSource.setDatabase("jdbc:hsqldb:mem:testdb" + System.currentTimeMillis());
        dataSource.setUser("sa");
        dataSource.setPassword("");
        dataSource.getConnection().close();

        properties.put("DataSource", dataSource);
        properties.put("UseDatabaseLock", "false");
        properties.put("StartupTimeout", "10000");

        ActiveMQFactory.setThreadProperties(properties);
        BrokerService broker = null;
        try {
            broker = BrokerFactory.createBroker(new URI(getBrokerUri(
                "broker:(tcp://localhost:" + brokerPort + ")?useJmx=false")));
            assertNotNull("broker is null", broker);

            final PersistenceAdapter persistenceAdapter = broker.getPersistenceAdapter();
            assertNotNull("persistenceAdapter is null", persistenceAdapter);

            assertTrue("persistenceAdapter should be an instance of JDBCPersistenceAdapter",
                persistenceAdapter instanceof JDBCPersistenceAdapter);
            final JDBCPersistenceAdapter jdbcPersistenceAdapter = (JDBCPersistenceAdapter) persistenceAdapter;

            assertSame(dataSource, jdbcPersistenceAdapter.getDataSource());
        } finally {
            stopBroker(broker);
            ActiveMQFactory.setThreadProperties(null);
        }
    }
 
Example 9
Source File: TestSupport.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void stopBrokerWithStoreFailure(BrokerService broker, PersistenceAdapterChoice choice) throws Exception {
   switch (choice) {
      case KahaDB:
         KahaDBPersistenceAdapter kahaDBPersistenceAdapter = (KahaDBPersistenceAdapter) broker.getPersistenceAdapter();

         // have the broker stop with an IOException on next checkpoint so it has a pending local transaction to recover
         kahaDBPersistenceAdapter.getStore().getJournal().close();
         break;
      default:
         // just stop normally by default
         broker.stop();
   }
   broker.waitUntilStopped();
}
 
Example 10
Source File: OpenEjbBrokerFactoryTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void testLookupDataSource() throws Exception {

        final Properties properties = new Properties();

        final JDBCDataSource dataSource = new JDBCDataSource();
        dataSource.setDatabase("jdbc:hsqldb:mem:testdb" + System.currentTimeMillis());
        dataSource.setUser("sa");
        dataSource.setPassword("");
        dataSource.getConnection().close();

        MockInitialContextFactory.install(Collections.singletonMap("openejb/Resource/TestDs", dataSource));
        assertSame(dataSource, new InitialContext().lookup("openejb/Resource/TestDs"));

        final CoreContainerSystem containerSystem = new CoreContainerSystem(new IvmJndiFactory());
        containerSystem.getJNDIContext().bind("openejb/Resource/TestDs", dataSource);
        SystemInstance.get().setComponent(ContainerSystem.class, containerSystem);

        properties.put("DataSource", "TestDs");
        properties.put("UseDatabaseLock", "false");
        properties.put("StartupTimeout", "10000");

        ActiveMQFactory.setThreadProperties(properties);
        BrokerService broker = null;
        try {
            broker = BrokerFactory.createBroker(new URI(getBrokerUri(
                "broker:(tcp://localhost:" + brokerPort + ")?useJmx=false")));
            assertNotNull("broker is null", broker);

            final PersistenceAdapter persistenceAdapter = broker.getPersistenceAdapter();
            assertNotNull("persistenceAdapter is null", persistenceAdapter);

            assertTrue("persistenceAdapter should be an instance of JDBCPersistenceAdapter",
                persistenceAdapter instanceof JDBCPersistenceAdapter);
            final JDBCPersistenceAdapter jdbcPersistenceAdapter = (JDBCPersistenceAdapter) persistenceAdapter;

            assertSame(dataSource, jdbcPersistenceAdapter.getDataSource());
        } finally {
            stopBroker(broker);
            ActiveMQFactory.setThreadProperties(null);
        }
    }