Search in sources :

Example 1 with DiscoveryListener

use of org.apache.openejb.server.DiscoveryListener in project tomee by apache.

the class Tracker method fireServiceAddedEvent.

private void fireServiceAddedEvent(final URI uri) {
    if (log.isInfoEnabled()) {
        log.info(String.format("Added Service{uri=%s}", uri));
    }
    if (discoveryListener != null) {
        final DiscoveryListener discoveryListener = this.discoveryListener;
        // Have the listener process the event async so that
        // he does not block this thread since we are doing time sensitive
        // processing of events.
        executor.execute(new Runnable() {

            @Override
            public void run() {
                discoveryListener.serviceAdded(uri);
            }
        });
    }
}
Also used : DiscoveryListener(org.apache.openejb.server.DiscoveryListener)

Example 2 with DiscoveryListener

use of org.apache.openejb.server.DiscoveryListener in project tomee by apache.

the class Tracker method fireServiceRemovedEvent.

private void fireServiceRemovedEvent(final URI uri) {
    if (log.isInfoEnabled()) {
        log.info(String.format("Removed Service{uri=%s}", uri));
    }
    if (discoveryListener != null) {
        final DiscoveryListener discoveryListener = this.discoveryListener;
        // Have the listener process the event async so that
        // he does not block this thread since we are doing time sensitive
        // processing of events.
        executor.execute(new Runnable() {

            @Override
            public void run() {
                discoveryListener.serviceRemoved(uri);
            }
        });
    }
}
Also used : DiscoveryListener(org.apache.openejb.server.DiscoveryListener)

Example 3 with DiscoveryListener

use of org.apache.openejb.server.DiscoveryListener in project tomee by apache.

the class MulticastPulseAgentTest method testBroadcastBadUri.

@Test
public void testBroadcastBadUri() throws Exception {
    if ("true".equals(System.getProperty("skipMulticastTests"))) {
        Logger.getLogger(this.getClass().getName()).warning("Skipping MulticastTest " + this.getClass().getName());
        return;
    }
    final DiscoveryListener original = agent.getDiscoveryListener();
    final CountDownLatch latch = new CountDownLatch(1);
    final DiscoveryListener listener = new DiscoveryListener() {

        @Override
        public void serviceAdded(final URI service) {
            latch.countDown();
            System.out.println("added = " + service);
        }

        @Override
        public void serviceRemoved(final URI service) {
            latch.countDown();
            System.out.println("removed = " + service);
        }
    };
    agent.setDiscoveryListener(listener);
    final String[] hosts = agent.getHosts().split(",");
    final String host = hosts[hosts.length - 1];
    boolean removed = agent.removeFromIgnore(host);
    org.junit.Assert.assertTrue("Host is already ignored", !removed);
    final Future<?> future = executor.submit(new Runnable() {

        @Override
        public void run() {
            try {
                final InetAddress ia = getAddress(MulticastPulseAgentTest.host);
                final byte[] bytes = (MulticastPulseAgent.CLIENT + forGroup + MulticastPulseAgent.BADURI + host).getBytes(Charset.forName("UTF-8"));
                final DatagramPacket request = new DatagramPacket(bytes, bytes.length, new InetSocketAddress(ia, port));
                final MulticastSocket[] multicastSockets = MulticastPulseAgent.getSockets(MulticastPulseAgentTest.host, port);
                for (int i = 0; i < 5; i++) {
                    for (final MulticastSocket socket : multicastSockets) {
                        try {
                            socket.send(request);
                            Thread.sleep(100);
                        } catch (final Exception e) {
                            System.out.println("Failed to broadcast bad URI on: " + socket.getInterface().getHostAddress());
                            e.printStackTrace();
                        }
                    }
                }
            } catch (final Exception e) {
                System.out.println("Failed to broadcast bad URI");
                e.printStackTrace();
            }
        }
    });
    final boolean await = latch.await(20, TimeUnit.SECONDS);
    removed = agent.removeFromIgnore(host);
    agent.setDiscoveryListener(original);
    org.junit.Assert.assertTrue("Failed to remove host", removed);
    org.junit.Assert.assertTrue("Failed to unlatch", await);
}
Also used : MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) UnknownHostException(java.net.UnknownHostException) DatagramPacket(java.net.DatagramPacket) DiscoveryListener(org.apache.openejb.server.DiscoveryListener) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 4 with DiscoveryListener

use of org.apache.openejb.server.DiscoveryListener in project tomee by apache.

the class MultipointDiscoveryAgentTest method test.

public void test() throws Exception {
    final URI testService = new URI("green://localhost:0");
    final String[] names = { "red" };
    final int PEERS = names.length;
    final CountDownLatch[] latches = { new CountDownLatch(PEERS + 1), new CountDownLatch(PEERS + 1) };
    final DiscoveryListener listener = new DiscoveryListener() {

        public void serviceAdded(final URI service) {
            System.out.println("added = " + service);
            if (testService.equals(service)) {
                latches[0].countDown();
            }
        }

        public void serviceRemoved(final URI service) {
            System.out.println("removed = " + service);
            if (testService.equals(service)) {
                latches[1].countDown();
            }
        }
    };
    final List<Node> nodes = new ArrayList<Node>();
    final Node root = new Node(0, listener, "root");
    nodes.add(root);
    for (final String name : names) {
        final Node node = new Node(0, listener, name, root.getURI());
        nodes.add(node);
    }
    final Node owner = nodes.get(nodes.size() / 2);
    for (int i = 0; i < 3; i++) {
        latches[0] = new CountDownLatch(PEERS + 1);
        latches[1] = new CountDownLatch(PEERS + 1);
        // OK, do the broadcast
        owner.getRegistry().registerService(testService);
        // Notification should have reached all participants
        assertTrue("round=" + i + ". Add failed", latches[0].await(30, TimeUnit.SECONDS));
        owner.getRegistry().unregisterService(testService);
        assertTrue("round=" + i + ". Remove failed", latches[1].await(60, TimeUnit.SECONDS));
        for (final Node node : nodes) {
            final Set<URI> services = node.getRegistry().getServices();
            assertEquals("round=" + i + ". Service retained", 0, services.size());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) DiscoveryListener(org.apache.openejb.server.DiscoveryListener)

Example 5 with DiscoveryListener

use of org.apache.openejb.server.DiscoveryListener in project tomee by apache.

the class MulticastPulseAgent method fireEvent.

private void fireEvent(final URI uri, final boolean add) {
    if (null != this.listener) {
        final DiscoveryListener dl = this.listener;
        getExecutorService().execute(new Runnable() {

            @Override
            public void run() {
                if (add) {
                    dl.serviceAdded(uri);
                } else {
                    dl.serviceRemoved(uri);
                }
            }
        });
    }
}
Also used : DiscoveryListener(org.apache.openejb.server.DiscoveryListener)

Aggregations

DiscoveryListener (org.apache.openejb.server.DiscoveryListener)5 URI (java.net.URI)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 DatagramPacket (java.net.DatagramPacket)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 MulticastSocket (java.net.MulticastSocket)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1