Search in sources :

Example 1 with DiscoveryListener

use of org.apache.activemq.transport.discovery.DiscoveryListener in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testDiscoveryOnRestartCurator.

@Test
public void testDiscoveryOnRestartCurator() throws Exception {
    underTest = new ActiveMQServiceFactory();
    underTest.curator = curator;
    Properties props = new Properties();
    props.put("config", "amq.xml");
    props.put("broker-name", "amq");
    props.put("group", "amq");
    props.put("connectors", "openwire");
    props.put("openwire-port", "0");
    props.put("container.ip", "localhost");
    underTest.updated("b", props);
    final AtomicReference<CuratorFramework> curatorFrameworkAtomicReference = new AtomicReference<>(curator);
    OsgiFabricDiscoveryAgent osgiFabricDiscoveryAgent = new OsgiFabricDiscoveryAgent(new BundleContext() {

        @Override
        public String getProperty(String s) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public Bundle installBundle(String s, InputStream inputStream) throws BundleException {
            return null;
        }

        @Override
        public Bundle installBundle(String s) throws BundleException {
            return null;
        }

        @Override
        public Bundle getBundle(long l) {
            return null;
        }

        @Override
        public Bundle[] getBundles() {
            return new Bundle[0];
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener, String s) throws InvalidSyntaxException {
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void removeServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void addBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void removeBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void addFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public void removeFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public ServiceRegistration<?> registerService(String[] strings, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceRegistration<?> registerService(String s, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public <S> ServiceRegistration<S> registerService(Class<S> aClass, S s, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceReference<?>[] getServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?>[] getAllServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?> getServiceReference(String s) {
            return null;
        }

        @Override
        public <S> ServiceReference<S> getServiceReference(Class<S> aClass) {
            return null;
        }

        @Override
        public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> aClass, String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public <S> S getService(ServiceReference<S> serviceReference) {
            return (S) curatorFrameworkAtomicReference.get();
        }

        @Override
        public boolean ungetService(ServiceReference<?> serviceReference) {
            return false;
        }

        @Override
        public File getDataFile(String s) {
            return null;
        }

        @Override
        public Filter createFilter(String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public Bundle getBundle(String s) {
            return null;
        }
    });
    final LinkedBlockingQueue<DiscoveryEvent> discoveryEvents = new LinkedBlockingQueue<DiscoveryEvent>(10);
    osgiFabricDiscoveryAgent.setDiscoveryListener(new DiscoveryListener() {

        @Override
        public void onServiceAdd(DiscoveryEvent discoveryEvent) {
            discoveryEvents.offer(discoveryEvent);
        }

        @Override
        public void onServiceRemove(DiscoveryEvent discoveryEvent) {
        }
    });
    // will call into dummy bundle and get curator
    osgiFabricDiscoveryAgent.addingService(null);
    osgiFabricDiscoveryAgent.setGroupName("amq");
    osgiFabricDiscoveryAgent.start();
    DiscoveryEvent event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("event: " + event);
    assertNotNull("got added service", event);
    underTest.deleted("b");
    // swap curator ref
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).sessionTimeoutMs(15000).retryPolicy(new RetryNTimes(5000, 1000));
    curator = builder.build();
    LOG.debug("Starting new curator " + curator);
    curator.start();
    curatorFrameworkAtomicReference.get().close();
    curatorFrameworkAtomicReference.set(curator);
    // will call into dummy bundle and get new curator ref
    osgiFabricDiscoveryAgent.addingService(null);
    // start broker again
    underTest.curator = curator;
    underTest.updated("b", props);
    event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("new event: " + event);
    assertNotNull("got newly added service", event);
    underTest.deleted("b");
}
Also used : CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) DiscoveryEvent(org.apache.activemq.command.DiscoveryEvent) Properties(java.util.Properties) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryNTimes(org.apache.curator.retry.RetryNTimes) InputStream(java.io.InputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) OsgiFabricDiscoveryAgent(io.fabric8.mq.fabric.discovery.OsgiFabricDiscoveryAgent) Collection(java.util.Collection) File(java.io.File) DiscoveryListener(org.apache.activemq.transport.discovery.DiscoveryListener) Test(org.junit.Test)

Example 2 with DiscoveryListener

use of org.apache.activemq.transport.discovery.DiscoveryListener in project fabric8 by jboss-fuse.

the class FabricDiscoveryAgent method update.

private void update(Collection<ActiveMQNode> members) {
    // Find new registered services...
    DiscoveryListener discoveryListener = this.discoveryListener.get();
    if (discoveryListener != null) {
        HashSet<String> activeServices = new HashSet<String>();
        for (ActiveMQNode m : members) {
            for (String service : m.services) {
                String resolved = service;
                try {
                    resolved = ZooKeeperUtils.getSubstitutedData(curator, service);
                } catch (Exception e) {
                // ignore, we'll use unresolved value
                }
                // Lets only discover openwire service URLs
                if (resolved.startsWith("tcp:") || resolved.startsWith("ssl:") || resolved.startsWith("nio:") || resolved.startsWith("nio+ssl:")) {
                    activeServices.add(resolved);
                }
            }
        }
        // If there is error talking the the central server, then activeServices == null
        if (members != null && members.size() > 0) {
            synchronized (discoveredServices) {
                HashSet<String> removedServices = new HashSet<String>(discoveredServices.keySet());
                removedServices.removeAll(activeServices);
                HashSet<String> addedServices = new HashSet<String>(activeServices);
                addedServices.removeAll(discoveredServices.keySet());
                addedServices.removeAll(removedServices);
                for (String service : addedServices) {
                    SimpleDiscoveryEvent e = new SimpleDiscoveryEvent(service);
                    discoveredServices.put(service, e);
                    discoveryListener.onServiceAdd(e);
                }
                for (String service : removedServices) {
                    SimpleDiscoveryEvent e = discoveredServices.remove(service);
                    if (e != null) {
                        e.removed.set(true);
                    }
                    discoveryListener.onServiceRemove(e);
                }
            }
        }
    }
}
Also used : DiscoveryListener(org.apache.activemq.transport.discovery.DiscoveryListener) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 3 with DiscoveryListener

use of org.apache.activemq.transport.discovery.DiscoveryListener in project fabric8 by jboss-fuse.

the class MQProfileTest method waitForBroker.

protected void waitForBroker(String groupName) throws Exception {
    ServiceProxy<CuratorFramework> curatorProxy = ServiceProxy.createServiceProxy(bundleContext, CuratorFramework.class);
    try {
        CuratorFramework curator = curatorProxy.getService();
        final CountDownLatch serviceLatch = new CountDownLatch(1);
        final FabricDiscoveryAgent discoveryAgent = new FabricDiscoveryAgent();
        discoveryAgent.setCurator(curator);
        discoveryAgent.setGroupName(groupName);
        discoveryAgent.setDiscoveryListener(new DiscoveryListener() {

            @Override
            public void onServiceAdd(DiscoveryEvent discoveryEvent) {
                System.out.println("Service added:" + discoveryEvent.getServiceName());
                serviceLatch.countDown();
                try {
                    discoveryAgent.stop();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceRemove(DiscoveryEvent discoveryEvent) {
                System.out.println("Service removed:" + discoveryEvent.getServiceName());
            }
        });
        discoveryAgent.start();
        Assert.assertTrue(serviceLatch.await(15, TimeUnit.MINUTES));
    } finally {
        curatorProxy.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) FabricDiscoveryAgent(io.fabric8.mq.fabric.discovery.FabricDiscoveryAgent) DiscoveryEvent(org.apache.activemq.command.DiscoveryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) DiscoveryListener(org.apache.activemq.transport.discovery.DiscoveryListener)

Example 4 with DiscoveryListener

use of org.apache.activemq.transport.discovery.DiscoveryListener in project fabric8 by jboss-fuse.

the class OsgiDiscoveryAgent method update.

private void update() {
    // synchronized(registeredServices) {
    // for (String service : registeredServices) {
    // doRegister(service);
    // }
    // }
    // Find new registered services...
    DiscoveryListener discoveryListener = this.discoveryListener.get();
    if (discoveryListener != null) {
        Set<String> activeServices = doLookup(updateInterval * 3);
        // If there is error talking the the central server, then activeServices == null
        if (activeServices != null) {
            synchronized (discoveredServices) {
                HashSet<String> removedServices = new HashSet<String>(discoveredServices.keySet());
                removedServices.removeAll(activeServices);
                HashSet<String> addedServices = new HashSet<String>(activeServices);
                addedServices.removeAll(discoveredServices.keySet());
                addedServices.removeAll(removedServices);
                for (String service : addedServices) {
                    SimpleDiscoveryEvent e = new SimpleDiscoveryEvent(service);
                    discoveredServices.put(service, e);
                    discoveryListener.onServiceAdd(e);
                }
                for (String service : removedServices) {
                    SimpleDiscoveryEvent e = discoveredServices.remove(service);
                    if (e != null) {
                        e.removed.set(true);
                    }
                    discoveryListener.onServiceRemove(e);
                }
            }
        }
    }
}
Also used : DiscoveryListener(org.apache.activemq.transport.discovery.DiscoveryListener)

Aggregations

DiscoveryListener (org.apache.activemq.transport.discovery.DiscoveryListener)4 DiscoveryEvent (org.apache.activemq.command.DiscoveryEvent)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 FabricDiscoveryAgent (io.fabric8.mq.fabric.discovery.FabricDiscoveryAgent)1 OsgiFabricDiscoveryAgent (io.fabric8.mq.fabric.discovery.OsgiFabricDiscoveryAgent)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1 RetryNTimes (org.apache.curator.retry.RetryNTimes)1 Test (org.junit.Test)1