Search in sources :

Example 6 with CopyOnWriteArraySet

use of java.util.concurrent.CopyOnWriteArraySet in project hazelcast by hazelcast.

the class MultiMapListenerTest method testMultiMapEntryListener.

@Test
public void testMultiMapEntryListener() {
    final HazelcastInstance instance = createHazelcastInstance();
    MultiMap<String, String> map = instance.getMultiMap("testMultiMapEntryListener");
    final CountDownLatch latchAdded = new CountDownLatch(3);
    final CountDownLatch latchRemoved = new CountDownLatch(1);
    final CountDownLatch latchCleared = new CountDownLatch(1);
    final Set<String> expectedValues = new CopyOnWriteArraySet<String>();
    expectedValues.add("hello");
    expectedValues.add("world");
    expectedValues.add("again");
    map.addEntryListener(new EntryAdapter<String, String>() {

        public void entryAdded(EntryEvent<String, String> event) {
            String key = event.getKey();
            String value = event.getValue();
            if ("2".equals(key)) {
                assertEquals("again", value);
            } else {
                assertEquals("1", key);
            }
            assertContains(expectedValues, value);
            expectedValues.remove(value);
            latchAdded.countDown();
        }

        public void entryRemoved(EntryEvent<String, String> event) {
            assertEquals("2", event.getKey());
            assertEquals("again", event.getOldValue());
            latchRemoved.countDown();
        }

        public void entryUpdated(EntryEvent<String, String> event) {
            throw new AssertionError("MultiMap cannot get update event!");
        }

        public void entryEvicted(EntryEvent<String, String> event) {
            entryRemoved(event);
        }

        @Override
        public void mapEvicted(MapEvent event) {
        }

        @Override
        public void mapCleared(MapEvent event) {
            latchCleared.countDown();
        }
    }, true);
    map.put("1", "hello");
    map.put("1", "world");
    map.put("2", "again");
    Collection<String> values = map.get("1");
    assertEquals(2, values.size());
    assertContains(values, "hello");
    assertContains(values, "world");
    assertEquals(1, map.get("2").size());
    assertEquals(3, map.size());
    map.remove("2");
    assertEquals(2, map.size());
    map.clear();
    try {
        assertTrue(latchAdded.await(5, TimeUnit.SECONDS));
        assertTrue(latchRemoved.await(5, TimeUnit.SECONDS));
        assertTrue(latchCleared.await(5, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : MapEvent(com.hazelcast.core.MapEvent) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstance(com.hazelcast.core.HazelcastInstance) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with CopyOnWriteArraySet

use of java.util.concurrent.CopyOnWriteArraySet in project RxBus by ViTess.

the class BusProxy method mount.

protected void mount(Map<String, Set<BusProcessor>> map) {
    for (Iterator iter = SubjectMap.entrySet().iterator(); iter.hasNext(); ) {
        Map.Entry<String, Set<BusProcessor>> entry = (Map.Entry<String, Set<BusProcessor>>) iter.next();
        String tag = entry.getKey();
        Set<BusProcessor> bPros = entry.getValue();
        Set<BusProcessor> allBPros = map.get(tag);
        if (allBPros == null) {
            allBPros = new CopyOnWriteArraySet<>();
            map.put(tag, allBPros);
        }
        allBPros.addAll(bPros);
    }
}
Also used : Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with CopyOnWriteArraySet

use of java.util.concurrent.CopyOnWriteArraySet in project karaf by apache.

the class Activator method start.

@Override
public void start(final BundleContext context) throws Exception {
    threadIO = new ThreadIOImpl();
    threadIO.start();
    sessionFactory = new SecuredSessionFactoryImpl(context, threadIO);
    sessionFactory.getCommandProcessor().addConverter(new Converters(context));
    sessionFactory.getCommandProcessor().addConstant(".context", context.getBundle(0).getBundleContext());
    final CopyOnWriteArraySet<CommandLoggingFilter> listeners = new CopyOnWriteArraySet<>();
    filterTracker = new ServiceTracker<>(context, CommandLoggingFilter.class, new ServiceTrackerCustomizer<CommandLoggingFilter, CommandLoggingFilter>() {

        @Override
        public CommandLoggingFilter addingService(ServiceReference<CommandLoggingFilter> reference) {
            CommandLoggingFilter service = context.getService(reference);
            listeners.add(service);
            return service;
        }

        @Override
        public void modifiedService(ServiceReference<CommandLoggingFilter> reference, CommandLoggingFilter service) {
        }

        @Override
        public void removedService(ServiceReference<CommandLoggingFilter> reference, CommandLoggingFilter service) {
            listeners.remove(service);
            context.ungetService(reference);
        }
    });
    filterTracker.open();
    LoggingCommandSessionListener loggingCommandSessionListener = new LoggingCommandSessionListener();
    loggingCommandSessionListener.setFilters(listeners);
    sessionFactory.getCommandProcessor().addListener(loggingCommandSessionListener);
    try {
        EventAdminListener listener = new EventAdminListener(context);
        sessionFactory.getCommandProcessor().addListener(listener);
        eventAdminListener = listener;
    } catch (NoClassDefFoundError error) {
    // Ignore the listener if EventAdmin package isn't present
    }
    sessionFactory.register(new ManagerImpl(sessionFactory, sessionFactory));
    sessionFactoryRegistration = context.registerService(SessionFactory.class, sessionFactory, null);
    actionExtender = new CommandExtender(sessionFactory);
    actionExtender.start(context);
    commandTracker = new CommandTracker(sessionFactory, context);
    commandTracker.open();
    if (Boolean.parseBoolean(context.getProperty(START_CONSOLE))) {
        localConsoleManager = new LocalConsoleManager(context, sessionFactory);
        localConsoleManager.start();
    } else {
        LOGGER.info("Not starting local console. To activate set " + START_CONSOLE + "=true");
    }
}
Also used : SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) SecuredSessionFactoryImpl(org.apache.karaf.shell.impl.console.osgi.secured.SecuredSessionFactoryImpl) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ServiceReference(org.osgi.framework.ServiceReference) CommandExtender(org.apache.karaf.shell.impl.action.osgi.CommandExtender) ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) CommandLoggingFilter(org.apache.karaf.shell.api.console.CommandLoggingFilter)

Example 9 with CopyOnWriteArraySet

use of java.util.concurrent.CopyOnWriteArraySet in project wildfly by wildfly.

the class ServiceContainerEndpointRegistry method getEndpoints.

@Override
public Set<ObjectName> getEndpoints() {
    Set<ObjectName> endpoints = new CopyOnWriteArraySet<ObjectName>();
    for (ServiceName sname : currentServiceContainer().getServiceNames()) {
        if (sname.getCanonicalName().startsWith(endpointPrefix)) {
            String contextPath = sname.getParent().getSimpleName().substring(8);
            String endpointName = sname.getSimpleName();
            final StringBuilder name = new StringBuilder(Endpoint.SEPID_DOMAIN + ":");
            name.append(Endpoint.SEPID_PROPERTY_CONTEXT + "=").append(contextPath).append(",");
            name.append(Endpoint.SEPID_PROPERTY_ENDPOINT + "=").append(endpointName);
            endpoints.add(ObjectNameFactory.create(name.toString()));
        }
    }
    return endpoints;
}
Also used : ServiceName(org.jboss.msc.service.ServiceName) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ObjectName(javax.management.ObjectName)

Example 10 with CopyOnWriteArraySet

use of java.util.concurrent.CopyOnWriteArraySet in project bnd by bndtools.

the class Processor method getPlugins.

/**
	 * Return a list of plugins. Plugins are defined with the -plugin command.
	 * They are class names, optionally associated with attributes. Plugins can
	 * implement the Plugin interface to see these attributes. Any object can be
	 * a plugin.
	 */
public Set<Object> getPlugins() {
    Set<Object> p;
    synchronized (this) {
        p = plugins;
        if (p != null)
            return p;
        plugins = p = new CopyOnWriteArraySet<>();
        missingCommand = new HashSet<String>();
    }
    // We only use plugins now when they are defined on our level
    // and not if it is in our parent. We inherit from our parent
    // through the previous block.
    String spe = getProperty(PLUGIN);
    if (NONE.equals(spe))
        return p;
    // The owner of the plugin is always in there.
    p.add(this);
    setTypeSpecificPlugins(p);
    if (parent != null)
        p.addAll(parent.getPlugins());
    //
    // Look only local
    //
    spe = mergeLocalProperties(PLUGIN);
    String pluginPath = mergeProperties(PLUGINPATH);
    loadPlugins(p, spe, pluginPath);
    addExtensions(p);
    for (RegistryDonePlugin rdp : getPlugins(RegistryDonePlugin.class)) {
        try {
            rdp.done();
        } catch (Exception e) {
            error("Calling done on %s, gives an exception %s", rdp, e);
        }
    }
    return p;
}
Also used : RegistryDonePlugin(aQute.bnd.service.RegistryDonePlugin) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)24 Set (java.util.Set)6 Collection (java.util.Collection)5 Test (org.junit.Test)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 MapEvent (com.hazelcast.core.MapEvent)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Properties (java.util.Properties)2 ObjectName (javax.management.ObjectName)2 RegistryDonePlugin (aQute.bnd.service.RegistryDonePlugin)1 DefaultExecutorServiceHandler (com.dangdang.ddframe.job.executor.handler.impl.DefaultExecutorServiceHandler)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1