org.apache.activemq.store.jdbc.JDBCPersistenceAdapter Java Examples

The following examples show how to use org.apache.activemq.store.jdbc.JDBCPersistenceAdapter. 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: JdbcDurableSubDupTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Before
public void startBroker() throws Exception {
   exceptions.clear();
   for (int i = 0; i < MAX_MESSAGES; i++) {
      dupChecker[i] = 0;
   }
   broker = new BrokerService();
   broker.setAdvisorySupport(false);
   broker.setPersistenceAdapter(new JDBCPersistenceAdapter());
   PolicyEntry policyEntry = new PolicyEntry();
   policyEntry.setMaxAuditDepth(3000);
   policyEntry.setMaxPageSize(150);
   policyEntry.setPrioritizedMessages(true);
   PolicyMap policyMap = new PolicyMap();
   policyMap.setDefaultEntry(policyEntry);
   broker.setDestinationPolicy(policyMap);

   broker.addConnector("tcp://localhost:0");
   broker.setDeleteAllMessagesOnStartup(true);
   broker.start();
   broker.waitUntilStarted();
   url = broker.getTransportConnectors().get(0).getConnectUri().toString() + "?" + urlOptions;
}
 
Example #2
Source File: DbRestartJDBCQueueTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
   setAutoFail(true);
   topic = false;
   verbose = true;
   // startup db
   sharedDs = (EmbeddedDataSource) DataSourceServiceSupport.createDataSource(IOHelper.getDefaultDataDirectory());

   broker = new BrokerService();

   DefaultIOExceptionHandler handler = new DefaultIOExceptionHandler();
   handler.setIgnoreSQLExceptions(false);
   handler.setStopStartConnectors(true);
   broker.setIoExceptionHandler(handler);
   broker.addConnector("tcp://localhost:0");
   broker.setUseJmx(false);
   broker.setPersistent(true);
   broker.setDeleteAllMessagesOnStartup(true);
   JDBCPersistenceAdapter persistenceAdapter = new JDBCPersistenceAdapter();
   persistenceAdapter.setDataSource(sharedDs);
   persistenceAdapter.setUseLock(false);
   persistenceAdapter.setLockKeepAlivePeriod(500);
   persistenceAdapter.getLocker().setLockAcquireSleepInterval(500);
   broker.setPersistenceAdapter(persistenceAdapter);
   broker.start();
   super.setUp();
}
 
Example #3
Source File: JDBCDurableSubscriptionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected PersistenceAdapter createPersistenceAdapter() throws IOException {
   JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
   EmbeddedDataSource dataSource = new EmbeddedDataSource();
   dataSource.setDatabaseName("derbyDb");
   dataSource.setCreateDatabase("create");
   jdbc.setDataSource(dataSource);
   jdbc.setCleanupPeriod(1000); // set up small cleanup period
   return jdbc;
}
 
Example #4
Source File: DurableSubscriptionOfflineTestBase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected void createBroker(boolean deleteAllMessages) throws Exception {
   String currentTestName = getName(true);
   broker = BrokerFactory.createBroker("broker:(vm://" + currentTestName + ")");
   broker.setBrokerName(currentTestName);
   broker.setDeleteAllMessagesOnStartup(deleteAllMessages);
   broker.getManagementContext().setCreateConnector(false);
   broker.setAdvisorySupport(false);
   broker.setKeepDurableSubsActive(keepDurableSubsActive);
   broker.addConnector("tcp://0.0.0.0:0");

   if (usePrioritySupport) {
      PolicyEntry policy = new PolicyEntry();
      policy.setPrioritizedMessages(true);
      PolicyMap policyMap = new PolicyMap();
      policyMap.setDefaultEntry(policy);
      broker.setDestinationPolicy(policyMap);
   }

   setDefaultPersistenceAdapter(broker);
   if (broker.getPersistenceAdapter() instanceof JDBCPersistenceAdapter) {
      // ensure it kicks in during tests
      ((JDBCPersistenceAdapter) broker.getPersistenceAdapter()).setCleanupPeriod(2 * 1000);
   } else if (broker.getPersistenceAdapter() instanceof KahaDBPersistenceAdapter) {
      // have lots of journal files
      ((KahaDBPersistenceAdapter) broker.getPersistenceAdapter()).setJournalMaxFileLength(journalMaxFileLength);
   }
   broker.start();
   broker.waitUntilStarted();
}
 
Example #5
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 #6
Source File: ConfigTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void testJdbcLockConfigDefault() throws Exception {

      JDBCPersistenceAdapter adapter = new JDBCPersistenceAdapter();
      Mockery context = new Mockery();
      final DataSource dataSource = context.mock(DataSource.class);
      final Connection connection = context.mock(Connection.class);
      final DatabaseMetaData metadata = context.mock(DatabaseMetaData.class);
      final ResultSet result = context.mock(ResultSet.class);
      adapter.setDataSource(dataSource);
      adapter.setCreateTablesOnStartup(false);

      context.checking(new Expectations() {{
         allowing(dataSource).getConnection();
         will(returnValue(connection));
         allowing(connection).getMetaData();
         will(returnValue(metadata));
         allowing(connection);
         allowing(metadata).getDriverName();
         will(returnValue("Some_Unknown_driver"));
         allowing(result).next();
         will(returnValue(true));
      }});

      adapter.start();
      assertEquals("has the default locker", adapter.getLocker().getClass(), DefaultDatabaseLocker.class);
      adapter.stop();
   }
 
Example #7
Source File: ConfigTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testJdbcLockConfigOverride() throws Exception {

   JDBCPersistenceAdapter adapter = new JDBCPersistenceAdapter();
   Mockery context = new Mockery();
   final DataSource dataSource = context.mock(DataSource.class);
   final Connection connection = context.mock(Connection.class);
   final DatabaseMetaData metadata = context.mock(DatabaseMetaData.class);
   final ResultSet result = context.mock(ResultSet.class);
   adapter.setDataSource(dataSource);
   adapter.setCreateTablesOnStartup(false);

   context.checking(new Expectations() {{
      allowing(dataSource).getConnection();
      will(returnValue(connection));
      allowing(connection).getMetaData();
      will(returnValue(metadata));
      allowing(connection);
      allowing(metadata).getDriverName();
      will(returnValue("Microsoft_SQL_Server_2005_jdbc_driver"));
      allowing(result).next();
      will(returnValue(true));
   }});

   adapter.start();
   assertTrue("has the locker override", adapter.getLocker() instanceof TransactDatabaseLocker);
   adapter.stop();
}
 
Example #8
Source File: RecoverExpiredMessagesTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void initCombosForTestRecovery() throws Exception {
   addCombinationValues("queuePendingPolicy", new PendingQueueMessageStoragePolicy[]{new FilePendingQueueMessageStoragePolicy(), new VMPendingQueueMessageStoragePolicy()});
   PersistenceAdapter[] persistenceAdapters = new PersistenceAdapter[]{new KahaDBPersistenceAdapter(), new JDBCPersistenceAdapter(DataSourceServiceSupport.createDataSource(IOHelper.getDefaultDataDirectory()), new OpenWireFormat())};
   for (PersistenceAdapter adapter : persistenceAdapters) {
      adapter.setDirectory(new File(IOHelper.getDefaultDataDirectory()));
   }
   addCombinationValues("persistenceAdapter", persistenceAdapters);
}
 
Example #9
Source File: ActivemqConfiguration.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Bean
public PersistenceAdapter jdbcPersistenceAdapter(DataSource dataSource){
	JdbcStoreProps jdbcProps = activemqProperties.getJdbcStore();
	JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter(dataSource, new OpenWireFormat());
	jdbc.setCreateTablesOnStartup(jdbcProps.isCreateTablesOnStartup());
	return jdbc;
}
 
Example #10
Source File: DurableSubscriptionReactivationTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected BrokerService createBroker() throws Exception {
   BrokerService answer = super.createBroker();
   answer.setKeepDurableSubsActive(keepDurableSubsActive);
   answer.setPersistenceAdapter(new JDBCPersistenceAdapter());
   answer.setDeleteAllMessagesOnStartup(true);
   return answer;
}
 
Example #11
Source File: JDBCQueueMasterSlaveTest.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);
            // no need for broker.setMasterConnectorURI(masterConnectorURI)
            // as the db lock provides the slave/master initialisation
            broker.setUseJmx(false);
            broker.setPersistent(true);
            JDBCPersistenceAdapter persistenceAdapter = new JDBCPersistenceAdapter();
            persistenceAdapter.setDataSource(getExistingDataSource());
            persistenceAdapter.setCreateTablesOnStartup(false);
            broker.setPersistenceAdapter(persistenceAdapter);
            configureJdbcPersistenceAdapter(persistenceAdapter);
            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 #12
Source File: JDBCQueueMasterSlaveTest.java    From activemq-artemis with Apache License 2.0 5 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);
   JDBCPersistenceAdapter persistenceAdapter = new JDBCPersistenceAdapter();
   persistenceAdapter.setDataSource(getExistingDataSource());
   configureJdbcPersistenceAdapter(persistenceAdapter);
   master.setPersistenceAdapter(persistenceAdapter);
   configureBroker(master);
   master.start();
}
 
Example #13
Source File: DbRestartJDBCQueueMasterSlaveLeaseTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected void configureJdbcPersistenceAdapter(JDBCPersistenceAdapter persistenceAdapter) throws IOException {
   super.configureJdbcPersistenceAdapter(persistenceAdapter);
   persistenceAdapter.setLocker(new LeaseDatabaseLocker());
   persistenceAdapter.getLocker().setLockAcquireSleepInterval(getLockAcquireSleepInterval());
   persistenceAdapter.setLockKeepAlivePeriod(getLockKeepAlivePeriod());
}
 
Example #14
Source File: StoreQueueCursorJDBCNoDuplicateTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected BrokerService createBroker() throws Exception {
   BrokerService broker = super.createBroker();
   PersistenceAdapter persistenceAdapter = new JDBCPersistenceAdapter();
   broker.setPersistenceAdapter(persistenceAdapter);
   return broker;
}
 
Example #15
Source File: JdbcXARecoveryBrokerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected void configureBroker(BrokerService broker) throws Exception {
   super.configureBroker(broker);

   JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
   jdbc.setDataSource(dataSource);
   broker.setPersistenceAdapter(jdbc);
}
 
Example #16
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 #17
Source File: DbRestartJDBCQueueMasterSlaveTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
protected void consumeMessage(Message message, List<Message> messageList) {
   try {
      receiveSession.commit();
      super.consumeMessage(message, messageList);
   } catch (JMSException e) {
      LOG.info("Failed to commit message receipt: " + message, e);
      try {
         receiveSession.rollback();
      } catch (JMSException ignored) {
      }

      if (e instanceof TransactionRolledBackException) {
         TransactionRolledBackException transactionRolledBackException = (TransactionRolledBackException) e;
         if (transactionRolledBackException.getMessage().indexOf("in doubt") != -1) {
            // failover chucked bc there is a missing reply to a commit.
            // failover is involved b/c the store exception is handled broker side and the client just
            // sees a disconnect (socket.close()).
            // If the client needs to be aware of the failure then it should not use IOExceptionHandler
            // so that the exception will propagate back

            // for this test case:
            // the commit may have got there and the reply is lost "or" the commit may be lost.
            // so we may or may not get a resend.
            //
            // At the application level we need to determine if the message is there or not which is not trivial
            // for this test we assert received == sent
            // so we need to know whether the message will be replayed.
            // we can ask the store b/c we know it is jdbc - guess we could go through a destination
            // message store interface also or use jmx
            java.sql.Connection dbConnection = null;
            try {
               ActiveMQMessage mqMessage = (ActiveMQMessage) message;
               MessageId id = mqMessage.getMessageId();
               dbConnection = sharedDs.getConnection();
               PreparedStatement s = dbConnection.prepareStatement(((JDBCPersistenceAdapter) connectedToBroker().getPersistenceAdapter()).getStatements().getFindMessageStatement());
               s.setString(1, id.getProducerId().toString());
               s.setLong(2, id.getProducerSequenceId());
               ResultSet rs = s.executeQuery();

               if (!rs.next()) {
                  // message is gone, so lets count it as consumed
                  LOG.info("On TransactionRolledBackException we know that the ack/commit got there b/c message is gone so we count it: " + mqMessage);
                  super.consumeMessage(message, messageList);
               } else {
                  LOG.info("On TransactionRolledBackException we know that the ack/commit was lost so we expect a replay of: " + mqMessage);
               }
            } catch (Exception dbe) {
               dbe.printStackTrace();
            } finally {
               try {
                  dbConnection.close();
               } catch (SQLException e1) {
                  e1.printStackTrace();
               }
            }
         }
      }
   }
}
 
Example #18
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);
        }
    }
 
Example #19
Source File: JDBCQueueMasterSlaveTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
protected void configureJdbcPersistenceAdapter(JDBCPersistenceAdapter persistenceAdapter) throws IOException {
   persistenceAdapter.setLockKeepAlivePeriod(500);
   persistenceAdapter.getLocker().setLockAcquireSleepInterval(500);
}
 
Example #20
Source File: JDBCPersistenceAdapterXBeanConfigTest.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
public void testConfiguredCorrectly() throws Exception {

      PersistenceAdapter persistenceAdapter = brokerService.getPersistenceAdapter();
      assertNotNull(persistenceAdapter);
      assertTrue(persistenceAdapter instanceof JDBCPersistenceAdapter);

      JDBCPersistenceAdapter jpa = (JDBCPersistenceAdapter) persistenceAdapter;
      assertEquals("BROKER1.", jpa.getStatements().getTablePrefix());

   }
 
Example #21
Source File: JDBCPersistenceXBeanConfigTest.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
public void testConfiguredCorrectly() throws Exception {

      PersistenceAdapter persistenceAdapter = brokerService.getPersistenceAdapter();
      assertNotNull(persistenceAdapter);
      assertTrue(persistenceAdapter instanceof JDBCPersistenceAdapter);

      JDBCPersistenceAdapter jpa = (JDBCPersistenceAdapter) persistenceAdapter;
      assertEquals("BROKER1.", jpa.getStatements().getTablePrefix());

   }