private static DiscoveryGroup createDiscoveryGroup(String nodeID, DiscoveryGroupConfiguration config) throws Exception { DiscoveryGroup group = new DiscoveryGroup(nodeID, config.getName(), config.getRefreshTimeout(), config.getBroadcastEndpointFactory(), null); return group; }
public synchronized void registerListener(final DiscoveryListener listener) { listeners.add(listener); if (!connectors.isEmpty()) { listener.connectorsChanged(getDiscoveryEntries()); } }
@Override public synchronized void initialize() throws ActiveMQException { if (state == STATE.INITIALIZED) return; synchronized (stateGuard) { if (state == STATE.CLOSING) throw new ActiveMQIllegalStateException(); try { state = STATE.INITIALIZED; latch = new CountDownLatch(1); setThreadPools(); topology.setExecutor(new OrderedExecutor(threadPool)); instantiateLoadBalancingPolicy(); if (discoveryGroupConfiguration != null) { discoveryGroup = createDiscoveryGroup(nodeID, discoveryGroupConfiguration); discoveryGroup.registerListener(this); discoveryGroup.start(); } } catch (Exception e) { state = null; throw ActiveMQClientMessageBundle.BUNDLE.failedToInitialiseSessionFactory(e); } } }
dg1.start(); dg2.start(); dg3.start(); boolean ok = dg1.waitForBroadcast(1000); Assert.assertTrue(ok); List<DiscoveryEntry> entries = dg1.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); ok = dg2.waitForBroadcast(1000); Assert.assertTrue(ok); entries = dg2.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); ok = dg3.waitForBroadcast(1000); Assert.assertTrue(ok); entries = dg3.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); dg1.stop(); dg2.stop(); dg3.stop();
dg.registerListener(listener1); MyListener listener2 = new MyListener(); dg.registerListener(listener2); dg.start(); List<DiscoveryEntry> entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); Assert.assertTrue(listener1.called); entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1, live2), entries); Assert.assertTrue(listener1.called); entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries); Assert.assertTrue(listener1.called); entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries); Assert.assertFalse(listener1.called); entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries); Assert.assertFalse(listener1.called); entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries); Assert.assertFalse(listener1.called);
@Test public void testSimpleBroadcastWithStopStartDiscoveryGroup() throws Exception { final InetAddress groupAddress = InetAddress.getByName(address1); final int groupPort = getUDPDiscoveryPort(); final int timeout = 500; final String nodeID = RandomUtil.randomString(); bg = newBroadcast(nodeID, RandomUtil.randomString(), null, -1, groupAddress, groupPort); bg.start(); TransportConfiguration live1 = generateTC(); bg.addConnector(live1); dg = newDiscoveryGroup(RandomUtil.randomString(), RandomUtil.randomString(), null, groupAddress, groupPort, timeout); dg.start(); verifyBroadcast(bg, dg); List<DiscoveryEntry> entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); bg.stop(); dg.stop(); dg.start(); bg.start(); verifyBroadcast(bg, dg); entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); }
@Test public void testSimpleBroadcastJGropus() throws Exception { final String nodeID = RandomUtil.randomString(); bg = new BroadcastGroupImpl(new FakeNodeManager(nodeID), "broadcast", 100, null, new JGroupsFileBroadcastEndpointFactory().setChannelName("tst").setFile(TEST_JGROUPS_CONF_FILE)); bg.start(); TransportConfiguration live1 = generateTC(); bg.addConnector(live1); dg = new DiscoveryGroup(nodeID + "1", "broadcast", 5000L, new JGroupsFileBroadcastEndpointFactory().setChannelName("tst").setFile(TEST_JGROUPS_CONF_FILE), null); dg.start(); verifyBroadcast(bg, dg); List<DiscoveryEntry> entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); }
dg1.start(); dg2.start(); dg3.start(); boolean ok = dg1.waitForBroadcast(timeout); Assert.assertTrue(ok); List<DiscoveryEntry> entries = dg1.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); ok = dg2.waitForBroadcast(timeout); Assert.assertTrue(ok); entries = dg2.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live2), entries); ok = dg3.waitForBroadcast(timeout); Assert.assertTrue(ok); entries = dg3.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live3), entries);
@Test public void testSimpleBroadcastSpecificNIC() throws Exception { final InetAddress groupAddress = InetAddress.getByName(address1); final int groupPort = getUDPDiscoveryPort(); final int timeout = 500; final String nodeID = RandomUtil.randomString(); // We need to choose a real NIC on the local machine - note this will silently pass if the machine // has no usable NIC! Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress localAddress = InetAddress.getLoopbackAddress(); log.info("Local address is " + localAddress); bg = newBroadcast(nodeID, RandomUtil.randomString(), localAddress, -1, groupAddress, groupPort); bg.start(); TransportConfiguration live1 = generateTC(); bg.addConnector(live1); dg = newDiscoveryGroup(RandomUtil.randomString(), RandomUtil.randomString(), localAddress, groupAddress, groupPort, timeout); dg.start(); verifyBroadcast(bg, dg); List<DiscoveryEntry> entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); }
boolean ok = discoveryGroup.waitForBroadcast(timeout);
synchronized (this) { try { discoveryGroup.stop(); } catch (Exception e) { ActiveMQClientLogger.LOGGER.failedToStopDiscovery(e);
@Test public void testDiscoveryGroupNotifications() throws Exception { SimpleNotificationService notifService = new SimpleNotificationService(); SimpleNotificationService.Listener notifListener = new SimpleNotificationService.Listener(); notifService.addNotificationListener(notifListener); final InetAddress groupAddress = InetAddress.getByName(address1); final int groupPort = getUDPDiscoveryPort(); final int timeout = 500; dg = newDiscoveryGroup(RandomUtil.randomString(), RandomUtil.randomString(), null, groupAddress, groupPort, timeout, notifService); Assert.assertEquals(0, notifListener.getNotifications().size()); dg.start(); Assert.assertEquals(1, notifListener.getNotifications().size()); Notification notif = notifListener.getNotifications().get(0); Assert.assertEquals(CoreNotificationType.DISCOVERY_GROUP_STARTED, notif.getType()); Assert.assertEquals(dg.getName(), notif.getProperties().getSimpleStringProperty(new SimpleString("name")).toString()); dg.stop(); Assert.assertEquals(2, notifListener.getNotifications().size()); notif = notifListener.getNotifications().get(1); Assert.assertEquals(CoreNotificationType.DISCOVERY_GROUP_STOPPED, notif.getType()); Assert.assertEquals(dg.getName(), notif.getProperties().getSimpleStringProperty(new SimpleString("name")).toString()); }
@Test public void testSimpleBroadcastDifferentAddressAndPort() throws Exception { final InetAddress groupAddress = InetAddress.getByName(address1); final int groupPort = getUDPDiscoveryPort(); final int timeout = 500; bg = newBroadcast(RandomUtil.randomString(), RandomUtil.randomString(), null, -1, groupAddress, groupPort); bg.start(); TransportConfiguration live1 = generateTC(); bg.addConnector(live1); final InetAddress groupAddress2 = InetAddress.getByName(address2); final int port2 = getUDPDiscoveryPort(1); dg = newDiscoveryGroup(RandomUtil.randomString(), RandomUtil.randomString(), null, groupAddress2, port2, timeout); dg.start(); verifyNonBroadcast(bg, dg); }
@Test public void testIgnoreTrafficFromOwnNode() throws Exception { final InetAddress groupAddress = InetAddress.getByName(address1); final int groupPort = getUDPDiscoveryPort(); final int timeout = 500; String nodeID = RandomUtil.randomString(); bg = newBroadcast(nodeID, RandomUtil.randomString(), null, -1, groupAddress, groupPort); bg.start(); TransportConfiguration live1 = generateTC(); bg.addConnector(live1); dg = newDiscoveryGroup(nodeID, RandomUtil.randomString(), null, groupAddress, groupPort, timeout); dg.start(); verifyNonBroadcast(bg, dg); List<DiscoveryEntry> entries = dg.getDiscoveryEntries(); Assert.assertNotNull(entries); Assert.assertEquals(0, entries.size()); }
/** * @param discoveryGroup * @throws Exception */ protected static void verifyNonBroadcast(BroadcastGroup broadcastGroup, DiscoveryGroup discoveryGroup) throws Exception { broadcastGroup.broadcastConnectors(); Assert.assertFalse("NO broadcast received", discoveryGroup.waitForBroadcast(2000)); }
synchronized (this) { try { discoveryGroup.stop(); } catch (Exception e) { ActiveMQClientLogger.LOGGER.failedToStopDiscovery(e);
@Test public void testSimpleBroadcastDifferentPort() throws Exception { final InetAddress groupAddress = InetAddress.getByName(getUDPDiscoveryAddress()); final int groupPort = getUDPDiscoveryPort(); final int timeout = 500; bg = newBroadcast(RandomUtil.randomString(), RandomUtil.randomString(), null, -1, groupAddress, groupPort); bg.start(); TransportConfiguration live1 = generateTC(); bg.addConnector(live1); final int port2 = getUDPDiscoveryPort(1); dg = newDiscoveryGroup(RandomUtil.randomString(), RandomUtil.randomString(), null, groupAddress, port2, timeout); dg.start(); verifyNonBroadcast(bg, dg); }
@Override public synchronized void initialize() throws ActiveMQException { if (state == STATE.INITIALIZED) return; synchronized (stateGuard) { if (state == STATE.CLOSING) throw new ActiveMQIllegalStateException(); try { state = STATE.INITIALIZED; latch = new CountDownLatch(1); setThreadPools(); topology.setExecutor(new OrderedExecutor(threadPool)); instantiateLoadBalancingPolicy(); if (discoveryGroupConfiguration != null) { discoveryGroup = createDiscoveryGroup(nodeID, discoveryGroupConfiguration); discoveryGroup.registerListener(this); discoveryGroup.start(); } } catch (Exception e) { state = null; throw ActiveMQClientMessageBundle.BUNDLE.failedToInitialiseSessionFactory(e); } } }
private void callListeners() { for (DiscoveryListener listener : listeners) { try { listener.connectorsChanged(getDiscoveryEntries()); } catch (Throwable t) { // Catch it so exception doesn't prevent other listeners from running ActiveMQClientLogger.LOGGER.failedToCallListenerInDiscovery(t); } } }
@Test public void testSimpleBroadcast() throws Exception { final InetAddress groupAddress = InetAddress.getByName(address1); final int groupPort = getUDPDiscoveryPort(); final int timeout = 500; final String nodeID = RandomUtil.randomString(); bg = new BroadcastGroupImpl(new FakeNodeManager(nodeID), RandomUtil.randomString(), 0, null, new UDPBroadcastEndpointFactory().setGroupAddress(address1).setGroupPort(groupPort)); bg.start(); TransportConfiguration live1 = generateTC(); bg.addConnector(live1); dg = newDiscoveryGroup(RandomUtil.randomString(), RandomUtil.randomString(), null, groupAddress, groupPort, timeout); dg.start(); verifyBroadcast(bg, dg); List<DiscoveryEntry> entries = dg.getDiscoveryEntries(); assertEqualsDiscoveryEntries(Arrays.asList(live1), entries); }