Search in sources :

Example 11 with JDBCPersistenceAdapter

use of org.apache.activemq.store.jdbc.JDBCPersistenceAdapter in project tomee by apache.

the class OpenEjbBrokerFactoryTest method testDirectDataSource.

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);
    }
}
Also used : JDBCPersistenceAdapter(org.apache.activemq.store.jdbc.JDBCPersistenceAdapter) JDBCDataSource(org.hsqldb.jdbc.JDBCDataSource) Properties(java.util.Properties) BrokerService(org.apache.activemq.broker.BrokerService) URI(java.net.URI) MemoryPersistenceAdapter(org.apache.activemq.store.memory.MemoryPersistenceAdapter) JDBCPersistenceAdapter(org.apache.activemq.store.jdbc.JDBCPersistenceAdapter) PersistenceAdapter(org.apache.activemq.store.PersistenceAdapter)

Example 12 with JDBCPersistenceAdapter

use of org.apache.activemq.store.jdbc.JDBCPersistenceAdapter in project tomee by apache.

the class ActiveMQ5Factory method createBroker.

@Override
public synchronized BrokerService createBroker(final URI brokerURI) throws Exception {
    Logger.getInstance(LogCategory.OPENEJB_STARTUP, ActiveMQ5Factory.class).getChildLogger("service").info("ActiveMQ5Factory creating broker");
    BrokerService broker = brokers.get(brokerURI);
    if (null == broker || !broker.isStarted()) {
        final Properties properties = getLowerCaseProperties();
        final URISupport.CompositeData compositeData = URISupport.parseComposite(new URI(brokerURI.getRawSchemeSpecificPart()));
        final Map<String, String> params = new HashMap<String, String>(compositeData.getParameters());
        final PersistenceAdapter persistenceAdapter;
        if ("true".equals(params.remove("usekahadb"))) {
            persistenceAdapter = createPersistenceAdapter("org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter", "kahadb", params);
        } else if ("true".equals(params.remove("useleveldb"))) {
            persistenceAdapter = createPersistenceAdapter("org.apache.activemq.store.leveldb.LevelDBPersistenceAdapter", "leveldb", params);
        } else if (params.get("persistenceadapter") != null) {
            final String adapter = params.remove("persistenceadapter");
            persistenceAdapter = createPersistenceAdapter(adapter, "persistence", params);
        } else {
            persistenceAdapter = null;
        }
        final String systemUsage = params.remove("systemUsage");
        final SystemUsage systemUsageInstance;
        if ("true".equalsIgnoreCase(systemUsage)) {
            systemUsageInstance = newSystemUsage(params);
        } else {
            systemUsageInstance = null;
        }
        final BrokerPlugin[] plugins = createPlugins(params);
        final URI uri = new URI(cleanUpUri(brokerURI.getRawSchemeSpecificPart(), compositeData.getParameters(), params));
        broker = "broker".equals(uri.getScheme()) ? newDefaultBroker(uri) : BrokerFactory.createBroker(uri);
        if (plugins != null) {
            broker.setPlugins(plugins);
        }
        if (systemUsageInstance != null) {
            broker.setSystemUsage(systemUsageInstance);
        }
        brokers.put(brokerURI, broker);
        if (persistenceAdapter != null) {
            broker.setPersistenceAdapter(persistenceAdapter);
            // if user didn't set persistent to true then setPersistenceAdapter() alone is ignored so forcing it with the factory
            broker.setPersistenceFactory(new ProvidedPersistenceAdapterPersistenceAdapterFactory(persistenceAdapter));
            broker.setPersistent(true);
            tomeeConfig(broker);
        } else {
            final boolean notXbean = !uri.getScheme().toLowerCase(Locale.ENGLISH).startsWith("xbean");
            if (notXbean) {
                Object value = properties.get("datasource");
                if (String.class.isInstance(value) && value.toString().length() == 0) {
                    value = null;
                }
                final DataSource dataSource;
                if (value != null) {
                    if (DataSource.class.isInstance(value)) {
                        dataSource = DataSource.class.cast(value);
                    } else if (String.class.isInstance(value)) {
                        final String resouceId = String.class.cast(value);
                        try {
                            final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
                            final Context context = containerSystem.getJNDIContext();
                            final Object obj = context.lookup("openejb/Resource/" + resouceId);
                            if (!(obj instanceof DataSource)) {
                                throw new IllegalArgumentException("Resource with id " + resouceId + " is not a DataSource, but is " + obj.getClass().getName());
                            }
                            dataSource = (DataSource) obj;
                        } catch (final NamingException e) {
                            throw new IllegalArgumentException("Unknown datasource " + resouceId);
                        }
                    } else {
                        throw new IllegalArgumentException("Unexpected datasource definition: " + value);
                    }
                } else {
                    dataSource = null;
                }
                if (null != dataSource) {
                    final JDBCPersistenceAdapter adapter = new JDBCPersistenceAdapter();
                    if (properties.containsKey("usedatabaselock")) {
                        // This must be false for hsqldb
                        adapter.setUseLock(Boolean.parseBoolean(properties.getProperty("usedatabaselock", "true")));
                    }
                    adapter.setDataSource(dataSource);
                    broker.setPersistent(true);
                    broker.setPersistenceAdapter(adapter);
                } else {
                    broker.setPersistenceAdapter(new MemoryPersistenceAdapter());
                }
                tomeeConfig(broker);
            }
        }
        // We must close the broker
        broker.setUseShutdownHook(false);
        broker.setSystemExitOnShutdown(false);
        broker.setStartAsync(false);
        final BrokerService bs = broker;
        final Thread start = new Thread("ActiveMQFactory start and checkpoint") {

            @Override
            public void run() {
                Thread.currentThread().setContextClassLoader(ActiveMQResourceAdapter.class.getClassLoader());
                try {
                    // Start before returning - this is known to be safe.
                    if (!bs.isStarted()) {
                        Logger.getInstance(LogCategory.OPENEJB_STARTUP, ActiveMQ5Factory.class).getChildLogger("service").info("Starting ActiveMQ BrokerService");
                        bs.start();
                    }
                    bs.waitUntilStarted();
                    // Force a checkpoint to initialize pools
                    Logger.getInstance(LogCategory.OPENEJB_STARTUP, ActiveMQ5Factory.class).getChildLogger("service").info("Starting ActiveMQ checkpoint");
                    bs.getPersistenceAdapter().checkpoint(true);
                    started.set(true);
                } catch (final Throwable t) {
                    throwable = t;
                }
            }
        };
        /*
             * An application may require immediate access to JMS. So we need to block here until the service
             * has started. How long ActiveMQ requires to actually create a broker is unpredictable.
             *
             * A broker in OpenEJB is usually a wrapper for an embedded ActiveMQ server service. The broker configuration
             * allows the definition of a remote ActiveMQ server, in which case startup is not an issue as the broker is
             * basically a client.
             *
             * If the broker is local and the message store contains millions of messages then the startup time is obviously going to
             * be longer as these need to be indexed by ActiveMQ.
             *
             * A balanced timeout will always be use case dependent.
            */
        int timeout = 30000;
        try {
            timeout = Integer.parseInt(properties.getProperty("startuptimeout", "30000"));
            Logger.getInstance(LogCategory.OPENEJB_STARTUP, ActiveMQ5Factory.class).getChildLogger("service").info("Using ActiveMQ startup timeout of " + timeout + "ms");
        } catch (final Throwable e) {
        // Ignore
        }
        start.setDaemon(true);
        start.start();
        try {
            start.join(timeout);
        } catch (final InterruptedException e) {
        // Ignore
        }
        if (null != throwable) {
            Logger.getInstance(LogCategory.OPENEJB_STARTUP, ActiveMQ5Factory.class).getChildLogger("service").error("ActiveMQ failed to start broker", throwable);
        } else if (started.get()) {
            Logger.getInstance(LogCategory.OPENEJB_STARTUP, ActiveMQ5Factory.class).getChildLogger("service").info("ActiveMQ broker started");
        } else {
            Logger.getInstance(LogCategory.OPENEJB_STARTUP, ActiveMQ5Factory.class).getChildLogger("service").warning("ActiveMQ failed to start broker within " + timeout + " seconds - It may be unusable");
        }
    }
    return broker;
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) JDBCPersistenceAdapter(org.apache.activemq.store.jdbc.JDBCPersistenceAdapter) HashMap(java.util.HashMap) SystemUsage(org.apache.activemq.usage.SystemUsage) Properties(java.util.Properties) URISupport(org.apache.activemq.util.URISupport) URI(java.net.URI) NamingException(javax.naming.NamingException) ActiveMQResourceAdapter(org.apache.activemq.ra.ActiveMQResourceAdapter) Context(javax.naming.Context) MemoryPersistenceAdapter(org.apache.activemq.store.memory.MemoryPersistenceAdapter) BrokerPlugin(org.apache.activemq.broker.BrokerPlugin) DataSource(javax.sql.DataSource) BrokerService(org.apache.activemq.broker.BrokerService) MemoryPersistenceAdapter(org.apache.activemq.store.memory.MemoryPersistenceAdapter) JDBCPersistenceAdapter(org.apache.activemq.store.jdbc.JDBCPersistenceAdapter) PersistenceAdapter(org.apache.activemq.store.PersistenceAdapter)

Example 13 with JDBCPersistenceAdapter

use of org.apache.activemq.store.jdbc.JDBCPersistenceAdapter in project activemq-artemis by apache.

the class TestSupport method setPersistenceAdapter.

public static PersistenceAdapter setPersistenceAdapter(BrokerService broker, PersistenceAdapterChoice choice) throws IOException {
    PersistenceAdapter adapter = null;
    switch(choice) {
        case JDBC:
            JDBCPersistenceAdapter jdbcPersistenceAdapter = new JDBCPersistenceAdapter();
            // rollback (at shutdown) on derby can take a long time with file io etc
            jdbcPersistenceAdapter.setUseLock(false);
            adapter = jdbcPersistenceAdapter;
            break;
        case KahaDB:
            adapter = new KahaDBPersistenceAdapter();
            break;
        case LevelDB:
            adapter = new LevelDBPersistenceAdapter();
            break;
        case MEM:
            adapter = new MemoryPersistenceAdapter();
            break;
    }
    broker.setPersistenceAdapter(adapter);
    adapter.setDirectory(new File(broker.getBrokerDataDirectory(), choice.name()));
    return adapter;
}
Also used : JDBCPersistenceAdapter(org.apache.activemq.store.jdbc.JDBCPersistenceAdapter) MemoryPersistenceAdapter(org.apache.activemq.store.memory.MemoryPersistenceAdapter) LevelDBPersistenceAdapter(org.apache.activemq.store.leveldb.LevelDBPersistenceAdapter) File(java.io.File) JDBCPersistenceAdapter(org.apache.activemq.store.jdbc.JDBCPersistenceAdapter) KahaDBPersistenceAdapter(org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter) MemoryPersistenceAdapter(org.apache.activemq.store.memory.MemoryPersistenceAdapter) LevelDBPersistenceAdapter(org.apache.activemq.store.leveldb.LevelDBPersistenceAdapter) PersistenceAdapter(org.apache.activemq.store.PersistenceAdapter) KahaDBPersistenceAdapter(org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter)

Example 14 with JDBCPersistenceAdapter

use of org.apache.activemq.store.jdbc.JDBCPersistenceAdapter in project activemq-artemis by apache.

the class JDBCQueueMasterSlaveTest method createSlave.

@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();
}
Also used : TransportConnector(org.apache.activemq.broker.TransportConnector) JDBCPersistenceAdapter(org.apache.activemq.store.jdbc.JDBCPersistenceAdapter) BrokerService(org.apache.activemq.broker.BrokerService) URI(java.net.URI) IOException(java.io.IOException)

Example 15 with JDBCPersistenceAdapter

use of org.apache.activemq.store.jdbc.JDBCPersistenceAdapter in project activemq-artemis by apache.

the class JDBCQueueMasterSlaveTest method createMaster.

@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();
}
Also used : JDBCPersistenceAdapter(org.apache.activemq.store.jdbc.JDBCPersistenceAdapter) BrokerService(org.apache.activemq.broker.BrokerService)

Aggregations

JDBCPersistenceAdapter (org.apache.activemq.store.jdbc.JDBCPersistenceAdapter)20 BrokerService (org.apache.activemq.broker.BrokerService)10 PersistenceAdapter (org.apache.activemq.store.PersistenceAdapter)10 MemoryPersistenceAdapter (org.apache.activemq.store.memory.MemoryPersistenceAdapter)5 URI (java.net.URI)4 KahaDBPersistenceAdapter (org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter)4 File (java.io.File)3 Properties (java.util.Properties)3 DataSource (javax.sql.DataSource)3 Connection (java.sql.Connection)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 ResultSet (java.sql.ResultSet)2 PolicyEntry (org.apache.activemq.broker.region.policy.PolicyEntry)2 PolicyMap (org.apache.activemq.broker.region.policy.PolicyMap)2 LevelDBPersistenceAdapter (org.apache.activemq.store.leveldb.LevelDBPersistenceAdapter)2 EmbeddedDataSource (org.apache.derby.jdbc.EmbeddedDataSource)2 JDBCDataSource (org.hsqldb.jdbc.JDBCDataSource)2 Expectations (org.jmock.Expectations)2 Mockery (org.jmock.Mockery)2 Test (org.junit.Test)2