Search in sources :

Example 6 with BrokerPlugin

use of org.apache.activemq.broker.BrokerPlugin in project beam by apache.

the class JmsIOTest method startBroker.

@Before
public void startBroker() throws Exception {
    broker = new BrokerService();
    broker.setUseJmx(false);
    broker.setPersistenceAdapter(new MemoryPersistenceAdapter());
    broker.addConnector(BROKER_URL);
    broker.setBrokerName("localhost");
    broker.setPopulateJMSXUserID(true);
    broker.setUseAuthenticatedPrincipalForJMSXUserID(true);
    // enable authentication
    List<AuthenticationUser> users = new ArrayList<>();
    // username and password to use to connect to the broker.
    // This user has users privilege (able to browse, consume, produce, list destinations)
    users.add(new AuthenticationUser(USERNAME, PASSWORD, "users"));
    SimpleAuthenticationPlugin plugin = new SimpleAuthenticationPlugin(users);
    BrokerPlugin[] plugins = new BrokerPlugin[] { plugin };
    broker.setPlugins(plugins);
    broker.start();
    // create JMS connection factory
    connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
    connectionFactoryWithSyncAcksAndWithoutPrefetch = new ActiveMQConnectionFactory(BROKER_URL + "?jms.prefetchPolicy.all=0&jms.sendAcksAsync=false");
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MemoryPersistenceAdapter(org.apache.activemq.store.memory.MemoryPersistenceAdapter) SimpleAuthenticationPlugin(org.apache.activemq.security.SimpleAuthenticationPlugin) ArrayList(java.util.ArrayList) BrokerPlugin(org.apache.activemq.broker.BrokerPlugin) BrokerService(org.apache.activemq.broker.BrokerService) AuthenticationUser(org.apache.activemq.security.AuthenticationUser) Before(org.junit.Before)

Example 7 with BrokerPlugin

use of org.apache.activemq.broker.BrokerPlugin in project vertx-proton by vert-x3.

the class ActiveMQTestBase method createBroker.

protected BrokerService createBroker(String name, boolean deleteMessagesOnStartup, Map<String, Integer> portMap) throws Exception {
    BrokerService brokerService = new BrokerService();
    brokerService.setBrokerName(name);
    brokerService.setDeleteAllMessagesOnStartup(deleteMessagesOnStartup);
    brokerService.setUseJmx(true);
    brokerService.getManagementContext().setCreateConnector(false);
    brokerService.setDataDirectory(DATA_PARENT_DIR + File.separator + "data" + File.separator + name);
    brokerService.setPersistent(false);
    brokerService.setSchedulerSupport(false);
    brokerService.setAdvisorySupport(false);
    ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
    BrokerPlugin authenticationPlugin = configureAuthentication();
    if (authenticationPlugin != null) {
        plugins.add(authenticationPlugin);
    }
    if (!plugins.isEmpty()) {
        brokerService.setPlugins(plugins.toArray(new BrokerPlugin[0]));
    }
    addAdditionalConnectors(brokerService, portMap);
    return brokerService;
}
Also used : ArrayList(java.util.ArrayList) BrokerPlugin(org.apache.activemq.broker.BrokerPlugin) BrokerService(org.apache.activemq.broker.BrokerService)

Example 8 with BrokerPlugin

use of org.apache.activemq.broker.BrokerPlugin in project activemq-artemis by apache.

the class AMQ4889Test method createBroker.

protected BrokerService createBroker() throws Exception {
    brokerService = new BrokerService();
    brokerService.setPersistent(false);
    ArrayList<BrokerPlugin> plugins = new ArrayList<>();
    BrokerPlugin authenticationPlugin = configureAuthentication();
    plugins.add(authenticationPlugin);
    BrokerPlugin[] array = new BrokerPlugin[plugins.size()];
    brokerService.setPlugins(plugins.toArray(array));
    transportConnector = brokerService.addConnector(LOCAL_URI);
    proxyConnector = new ProxyConnector();
    proxyConnector.setName("proxy");
    proxyConnector.setBind(new URI(PROXY_URI));
    proxyConnector.setRemote(new URI(LOCAL_URI));
    brokerService.addProxyConnector(proxyConnector);
    brokerService.start();
    brokerService.waitUntilStarted();
    return brokerService;
}
Also used : ArrayList(java.util.ArrayList) BrokerPlugin(org.apache.activemq.broker.BrokerPlugin) BrokerService(org.apache.activemq.broker.BrokerService) URI(java.net.URI)

Example 9 with BrokerPlugin

use of org.apache.activemq.broker.BrokerPlugin in project tomee by apache.

the class ActiveMQ5Factory method createPlugins.

private BrokerPlugin[] createPlugins(final Map<String, String> params) {
    final String plugins = params.remove("amq.plugins");
    if (plugins == null) {
        return null;
    }
    final Collection<BrokerPlugin> instances = new LinkedList<>();
    for (final String p : plugins.split(" *, *")) {
        if (p.isEmpty()) {
            continue;
        }
        final String prefix = p + ".";
        final ObjectRecipe recipe = new ObjectRecipe(params.remove(prefix + "class"));
        final Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
        while (iterator.hasNext()) {
            final Map.Entry<String, String> entry = iterator.next();
            final String key = entry.getKey();
            if (key.startsWith(prefix)) {
                recipe.setProperty(key.substring(prefix.length()), entry.getValue());
                iterator.remove();
            }
        }
        instances.add(BrokerPlugin.class.cast(recipe.create()));
    }
    return instances.toArray(new BrokerPlugin[instances.size()]);
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) BrokerPlugin(org.apache.activemq.broker.BrokerPlugin) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 10 with BrokerPlugin

use of org.apache.activemq.broker.BrokerPlugin 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<>(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 ManagementContext managementContext = new ManagementContext();
        managementContext.setCreateConnector(false);
        broker.setManagementContext(managementContext);
        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) ManagementContext(org.apache.activemq.broker.jmx.ManagementContext) Context(javax.naming.Context) MemoryPersistenceAdapter(org.apache.activemq.store.memory.MemoryPersistenceAdapter) BrokerPlugin(org.apache.activemq.broker.BrokerPlugin) DataSource(javax.sql.DataSource) ManagementContext(org.apache.activemq.broker.jmx.ManagementContext) 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)

Aggregations

BrokerPlugin (org.apache.activemq.broker.BrokerPlugin)10 BrokerService (org.apache.activemq.broker.BrokerService)6 ArrayList (java.util.ArrayList)3 URI (java.net.URI)2 HashMap (java.util.HashMap)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 MemoryPersistenceAdapter (org.apache.activemq.store.memory.MemoryPersistenceAdapter)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 Hashtable (java.util.Hashtable)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Destination (javax.jms.Destination)1 Context (javax.naming.Context)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)1 Broker (org.apache.activemq.broker.Broker)1 BrokerFilter (org.apache.activemq.broker.BrokerFilter)1