Search in sources :

Example 6 with SocketProxy

use of org.apache.activemq.util.SocketProxy in project activemq-artemis by apache.

the class DiscoveryNetworkReconnectTest method setUp.

@Before
public void setUp() throws Exception {
    context = new JUnit4Mockery() {

        {
            setImposteriser(ClassImposteriser.INSTANCE);
        }
    };
    brokerA = new BrokerService();
    brokerA.setBrokerName("BrokerA");
    configure(brokerA);
    brokerA.addConnector("tcp://localhost:0");
    brokerA.start();
    brokerA.waitUntilStarted();
    proxy = new SocketProxy(brokerA.getTransportConnectors().get(0).getConnectUri());
    managementContext = context.mock(ManagementContext.class);
    context.checking(new Expectations() {

        {
            allowing(managementContext).getJmxDomainName();
            will(returnValue("Test"));
            allowing(managementContext).setBrokerName("BrokerNC");
            allowing(managementContext).start();
            allowing(managementContext).isCreateConnector();
            allowing(managementContext).stop();
            allowing(managementContext).isConnectorStarted();
            // expected MBeans
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=Health"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,connector=networkConnectors,networkConnectorName=NC"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=Log4JConfiguration"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.Connection"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.NetworkBridge"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.MasterBroker"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=jobScheduler,jobSchedulerName=JMS"))));
            atLeast(maxReconnects - 1).of(managementContext).registerMBean(with(any(Object.class)), with(new NetworkBridgeObjectNameMatcher<>(new ObjectName("Test:type=Broker,brokerName=BrokerNC,connector=networkConnectors,networkConnectorName=NC,networkBridge=localhost/127.0.0.1_" + proxy.getUrl().getPort()))));
            will(new CustomAction("signal register network mbean") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    LOG.info("Mbean Registered: " + invocation.getParameter(0));
                    mbeanRegistered.release();
                    return new ObjectInstance((ObjectName) invocation.getParameter(1), "discription");
                }
            });
            atLeast(maxReconnects - 1).of(managementContext).unregisterMBean(with(new NetworkBridgeObjectNameMatcher<>(new ObjectName("Test:type=Broker,brokerName=BrokerNC,connector=networkConnectors,networkConnectorName=NC,networkBridge=localhost/127.0.0.1_" + proxy.getUrl().getPort()))));
            will(new CustomAction("signal unregister network mbean") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    LOG.info("Mbean Unregistered: " + invocation.getParameter(0));
                    mbeanUnregistered.release();
                    return null;
                }
            });
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=Health"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,connector=networkConnectors,networkConnectorName=NC"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=Log4JConfiguration"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.Connection"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.NetworkBridge"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.MasterBroker"))));
        }
    });
    brokerB = new BrokerService();
    brokerB.setManagementContext(managementContext);
    brokerB.setBrokerName("BrokerNC");
    configure(brokerB);
}
Also used : Expectations(org.jmock.Expectations) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) ObjectInstance(javax.management.ObjectInstance) ManagementContext(org.apache.activemq.broker.jmx.ManagementContext) SocketProxy(org.apache.activemq.util.SocketProxy) BrokerService(org.apache.activemq.broker.BrokerService) ObjectName(javax.management.ObjectName) Before(org.junit.Before)

Example 7 with SocketProxy

use of org.apache.activemq.util.SocketProxy in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testCallbackOnDisconnectCanClose.

@Test
public void testCallbackOnDisconnectCanClose() throws Exception {
    curator.close();
    LOG.info("....");
    SocketProxy socketProxy = new SocketProxy(new URI("tcp://localhost:" + zkPort));
    final CuratorFramework proxyCurator = CuratorFrameworkFactory.builder().connectString("localhost:" + socketProxy.getUrl().getPort()).sessionTimeoutMs(5000).connectionTimeoutMs(3000).retryPolicy(new RetryNTimes(10, 1000)).build();
    proxyCurator.start();
    proxyCurator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    LOG.info("curator is go: " + proxyCurator);
    final String path = "/singletons/test/threads" + System.currentTimeMillis() + "**";
    final ArrayList<Runnable> members = new ArrayList<Runnable>();
    final int nThreads = 1;
    final CountDownLatch gotDisconnectEvent = new CountDownLatch(1);
    class GroupRunnable implements Runnable, GroupListener<NodeState> {

        final int id;

        private final BlockingQueue<Integer> jobQueue = new LinkedBlockingDeque<Integer>();

        ZooKeeperGroup<NodeState> group;

        NodeState nodeState;

        public GroupRunnable(int id) {
            this.id = id;
            members.add(this);
            nodeState = new NodeState("foo" + id);
        }

        @Override
        public void run() {
            group = new ZooKeeperGroup<NodeState>(proxyCurator, path, NodeState.class);
            group.add(this);
            LOG.info("run: Added: " + this);
            try {
                while (true) {
                    switch(jobQueue.take()) {
                        case 0:
                            LOG.info("run: close: " + this);
                            try {
                                group.close();
                            } catch (IOException ignored) {
                            }
                            return;
                        case 1:
                            LOG.info("run: start: " + this);
                            group.start();
                            group.update(nodeState);
                            break;
                        case 2:
                            LOG.info("run: update: " + this);
                            nodeState.setId(nodeState.getId() + id);
                            group.update(nodeState);
                            break;
                    }
                }
            } catch (InterruptedException exit) {
            }
        }

        @Override
        public void groupEvent(Group<NodeState> group, GroupEvent event) {
            LOG.info("Got: event: " + event);
            if (event.equals(GroupEvent.DISCONNECTED)) {
                gotDisconnectEvent.countDown();
            }
        }
    }
    ;
    ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
    for (int i = 0; i < nThreads; i++) {
        executorService.execute(new GroupRunnable(i));
    }
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(1);
        // wait for registration
        while (groupRunnable.group == null || groupRunnable.group.getId() == null) {
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
    boolean firsStartedIsMaster = ((GroupRunnable) members.get(0)).group.isMaster();
    assertTrue("first started is master", firsStartedIsMaster);
    LOG.info("got master...");
    // lets see how long they take to notice a no responses to heart beats
    socketProxy.pause();
    // splash in an update
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(2);
    }
    boolean hasMaster = true;
    while (hasMaster) {
        for (Runnable r : members) {
            GroupRunnable groupRunnable = (GroupRunnable) r;
            hasMaster &= groupRunnable.group.isMaster();
        }
        if (hasMaster) {
            LOG.info("Waiting for no master state on proxy pause");
            TimeUnit.SECONDS.sleep(1);
        }
    }
    try {
        boolean gotDisconnect = gotDisconnectEvent.await(15, TimeUnit.SECONDS);
        assertTrue("got disconnect event", gotDisconnect);
        LOG.info("do close");
        for (Runnable r : members) {
            GroupRunnable groupRunnable = (GroupRunnable) r;
            groupRunnable.jobQueue.offer(0);
        }
        executorService.shutdown();
        // at a min when the session has expired
        boolean allThreadComplete = executorService.awaitTermination(6, TimeUnit.SECONDS);
        assertTrue("all threads complete", allThreadComplete);
    } finally {
        proxyCurator.close();
        socketProxy.close();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Group(io.fabric8.groups.Group) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) NodeState(io.fabric8.groups.NodeState) GroupListener(io.fabric8.groups.GroupListener) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SocketProxy(org.apache.activemq.util.SocketProxy) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecutorService(java.util.concurrent.ExecutorService) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) Test(org.junit.Test)

Example 8 with SocketProxy

use of org.apache.activemq.util.SocketProxy in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testThreadsOnTimeout.

@Test
public void testThreadsOnTimeout() throws Exception {
    curator.close();
    SocketProxy socketProxy = new SocketProxy(new URI("tcp://localhost:" + zkPort));
    final CuratorFramework proxyCurator = CuratorFrameworkFactory.builder().connectString("localhost:" + socketProxy.getUrl().getPort()).sessionTimeoutMs(5000).connectionTimeoutMs(3000).retryPolicy(new RetryNTimes(10, 1000)).build();
    proxyCurator.start();
    proxyCurator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    LOG.info("curator is go: " + proxyCurator);
    final String path = "/singletons/test/threads" + System.currentTimeMillis() + "**";
    final ArrayList<Runnable> members = new ArrayList<Runnable>();
    final int nThreads = 1;
    class GroupRunnable implements Runnable, GroupListener<NodeState> {

        final int id;

        private final BlockingQueue<Integer> jobQueue = new LinkedBlockingDeque<Integer>();

        ZooKeeperGroup<NodeState> group;

        NodeState nodeState;

        public GroupRunnable(int id) {
            this.id = id;
            members.add(this);
            nodeState = new NodeState("foo" + id);
        }

        @Override
        public void run() {
            group = new ZooKeeperGroup<NodeState>(proxyCurator, path, NodeState.class);
            group.add(this);
            LOG.info("run: Added: " + this);
            try {
                while (true) {
                    switch(jobQueue.take()) {
                        case 0:
                            LOG.info("run: close: " + this);
                            try {
                                group.close();
                            } catch (IOException ignored) {
                            }
                            return;
                        case 1:
                            LOG.info("run: start: " + this);
                            group.start();
                            group.update(nodeState);
                            break;
                        case 2:
                            LOG.info("run: update: " + this);
                            nodeState.setId(nodeState.getId() + id);
                            group.update(nodeState);
                            break;
                    }
                }
            } catch (InterruptedException exit) {
            }
        }

        @Override
        public void groupEvent(Group<NodeState> group, GroupEvent event) {
        }
    }
    ;
    ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
    for (int i = 0; i < nThreads; i++) {
        executorService.execute(new GroupRunnable(i));
    }
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(1);
        // wait for registration
        while (groupRunnable.group == null || groupRunnable.group.getId() == null) {
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
    boolean firsStartedIsMaster = ((GroupRunnable) members.get(0)).group.isMaster();
    assertTrue("first started is master", firsStartedIsMaster);
    LOG.info("got master...");
    // lets see how long they take to notice a no responses to heart beats
    socketProxy.pause();
    // splash in an update
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(2);
    }
    boolean hasMaster = true;
    while (hasMaster) {
        for (Runnable r : members) {
            GroupRunnable groupRunnable = (GroupRunnable) r;
            hasMaster &= groupRunnable.group.isMaster();
        }
        if (hasMaster) {
            LOG.info("Waiting for no master state on proxy pause");
            TimeUnit.SECONDS.sleep(1);
        }
    }
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(0);
    }
    executorService.shutdown();
    // at a min when the session has expired
    boolean allThreadComplete = executorService.awaitTermination(6, TimeUnit.SECONDS);
    proxyCurator.close();
    socketProxy.close();
    assertTrue("all threads complete", allThreadComplete);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Group(io.fabric8.groups.Group) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) NodeState(io.fabric8.groups.NodeState) GroupListener(io.fabric8.groups.GroupListener) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SocketProxy(org.apache.activemq.util.SocketProxy) URI(java.net.URI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecutorService(java.util.concurrent.ExecutorService) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) Test(org.junit.Test)

Aggregations

SocketProxy (org.apache.activemq.util.SocketProxy)8 URI (java.net.URI)6 Test (org.junit.Test)4 TransportConnector (org.apache.activemq.broker.TransportConnector)3 Group (io.fabric8.groups.Group)2 GroupListener (io.fabric8.groups.GroupListener)2 NodeState (io.fabric8.groups.NodeState)2 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Connection (javax.jms.Connection)2 JMSException (javax.jms.JMSException)2 Session (javax.jms.Session)2 ObjectName (javax.management.ObjectName)2 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)2