Search in sources :

Example 6 with ManagedThreadFactory

use of com.adaptris.core.util.ManagedThreadFactory in project interlok by adaptris.

the class JmxNotificationConsumerTest method testNotFound_Retry.

@Test
public void testNotFound_Retry() throws Exception {
    final MBeanServer mbeanServer = JmxHelper.findMBeanServer();
    final String myObjectName = "com.adaptris:type=Junit,id=" + getName();
    final StubNotificationBroadcaster broadcast = new StubNotificationBroadcaster();
    MockMessageListener listener = new MockMessageListener();
    JmxNotificationConsumer consumer = new JmxNotificationConsumer();
    consumer.setFailIfNotFound(false);
    consumer.setRetryInterval(new TimeInterval(1L, TimeUnit.SECONDS));
    consumer.setObjectName(myObjectName);
    StandaloneConsumer sc = wrap(new JmxConnection(), consumer, listener);
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ManagedThreadFactory(getClass().getSimpleName()));
    try {
        start(sc);
        scheduler.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                try {
                    if (!mbeanServer.isRegistered(ObjectName.getInstance(myObjectName))) {
                        mbeanServer.registerMBean(broadcast, ObjectName.getInstance(myObjectName));
                    }
                    broadcast.sendNotification(getName(), new Object());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
        }, 3, 1, TimeUnit.SECONDS);
        waitForMessages(listener, 1);
        assertTrue(listener.messageCount() >= 1);
        assertNotNull(listener.getMessages().get(0).getObjectHeaders().get(NotificationSerializer.OBJ_METADATA_USERDATA));
    } catch (CoreException e) {
    } finally {
        stop(sc);
        ManagedThreadFactory.shutdownQuietly(scheduler, 100L);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TimeInterval(com.adaptris.util.TimeInterval) MockMessageListener(com.adaptris.core.stubs.MockMessageListener) CoreException(com.adaptris.core.CoreException) ManagedThreadFactory(com.adaptris.core.util.ManagedThreadFactory) CoreException(com.adaptris.core.CoreException) StandaloneConsumer(com.adaptris.core.StandaloneConsumer) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 7 with ManagedThreadFactory

use of com.adaptris.core.util.ManagedThreadFactory in project interlok by adaptris.

the class CmdLineBootstrap method launchAdapter.

// Jira 154 : If the adapter is configured with a shared connection that has a fixed-number of retries
// which fails, then it throws an exception back to "main" which can terminate the JVM depending
// on how you're starting it.
// Not so much if you're using java -jar, but definitely if you're using the wrapper script.
private void launchAdapter(final UnifiedBootstrap bootstrap, boolean quietly) throws Exception {
    final String threadName = this.getClass().getSimpleName();
    if (quietly) {
        Thread launcher = new ManagedThreadFactory(getClass().getSimpleName()).newThread(new Runnable() {

            @Override
            public void run() {
                Thread.currentThread().setName(threadName);
                bootStart(bootstrap);
            }
        });
        launcher.setDaemon(false);
        launcher.start();
    } else {
        bootStart(bootstrap);
    }
}
Also used : ManagedThreadFactory(com.adaptris.core.util.ManagedThreadFactory)

Example 8 with ManagedThreadFactory

use of com.adaptris.core.util.ManagedThreadFactory in project interlok by adaptris.

the class AdapterManagerTest method testForceClose_ErrorOnInit_RequestStart.

@Test
public void testForceClose_ErrorOnInit_RequestStart() throws Exception {
    final TimeInterval waitTime = new TimeInterval(5L, TimeUnit.SECONDS);
    String adapterName = this.getClass().getSimpleName() + "." + getName();
    Adapter adapter = createAdapter(adapterName);
    MockFailingConnection conn = new MockFailingConnection(getName(), "Init");
    conn.setConnectionAttempts(-1);
    conn.setConnectionRetryInterval(new TimeInterval(100L, TimeUnit.MILLISECONDS));
    adapter.getSharedComponents().addConnection(conn);
    List<BaseComponentMBean> mBeans = createJmxManagers(adapter);
    try {
        register(mBeans);
        ObjectName adapterObj = createAdapterObjectName(adapterName);
        final AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class);
        // Create a cyclic barrier to wait for init/start operation to finish
        final CyclicBarrier gate = new CyclicBarrier(3);
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        ManagedThreadFactory threadFactory = new ManagedThreadFactory();
        Thread initThread = threadFactory.newThread(new Runnable() {

            @Override
            public void run() {
                try {
                    manager.requestInit();
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
                // This code is likely to never trigger, because of the throw above...
                try {
                    gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
                } catch (Exception gateException) {
                }
            }
        });
        initThread.setUncaughtExceptionHandler(exceptionHandler);
        initThread.start();
        Thread startThread = threadFactory.newThread(new Runnable() {

            @Override
            public void run() {
                try {
                    manager.requestStart();
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
                // This code is likely to never trigger, because of the throw above...
                try {
                    gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
                } catch (Exception gateException) {
                }
            }
        });
        startThread.setUncaughtExceptionHandler(exceptionHandler);
        startThread.start();
        try {
            gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
            fail("Adapter init success, when not expected");
        } catch (Exception gateException) {
            // Expected now force close it, because it took too long.
            manager.forceClose();
        }
        assertEquals(ClosedState.getInstance(), manager.getComponentState());
        assertEquals(0, conn.getInitCount());
        assertEquals(0, conn.getCloseCount());
    } finally {
        adapter.requestClose();
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) MockFailingConnection(com.adaptris.core.stubs.MockFailingConnection) Adapter(com.adaptris.core.Adapter) TimeoutException(java.util.concurrent.TimeoutException) MalformedObjectNameException(javax.management.MalformedObjectNameException) CoreException(com.adaptris.core.CoreException) ObjectName(javax.management.ObjectName) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedThreadFactory(com.adaptris.core.util.ManagedThreadFactory) CoreException(com.adaptris.core.CoreException) Test(org.junit.Test)

Example 9 with ManagedThreadFactory

use of com.adaptris.core.util.ManagedThreadFactory in project interlok by adaptris.

the class AdapterManagerTest method testForceClose_ErrorOnStart_RequestStart.

@Test
public void testForceClose_ErrorOnStart_RequestStart() throws Exception {
    final TimeInterval waitTime = new TimeInterval(5L, TimeUnit.SECONDS);
    String adapterName = this.getClass().getSimpleName() + "." + getName();
    Adapter adapter = createAdapter(adapterName);
    MockFailingConnection conn = new MockFailingConnection(getName(), "Start");
    conn.setConnectionAttempts(-1);
    conn.setConnectionRetryInterval(new TimeInterval(100L, TimeUnit.MILLISECONDS));
    adapter.getSharedComponents().addConnection(conn);
    List<BaseComponentMBean> mBeans = createJmxManagers(adapter);
    try {
        register(mBeans);
        ObjectName adapterObj = createAdapterObjectName(adapterName);
        final AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class);
        // Create a cyclic barrier to wait for init operation to finish
        final CyclicBarrier gate = new CyclicBarrier(2);
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        Thread startThread = new ManagedThreadFactory().newThread(new Runnable() {

            @Override
            public void run() {
                try {
                    manager.requestStart();
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
                // This code is likely to never trigger, because of the throw above...
                try {
                    gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
                } catch (Exception gateException) {
                }
            }
        });
        startThread.setUncaughtExceptionHandler(exceptionHandler);
        startThread.start();
        try {
            gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
            fail("Adapter Start success, when not expected");
        } catch (Exception gateException) {
            // Expected now force close it, because it took too long.
            manager.forceClose();
        }
        assertEquals(ClosedState.getInstance(), manager.getComponentState());
        assertEquals(1, conn.getInitCount());
        assertEquals(0, conn.getStartCount());
        assertEquals(1, conn.getCloseCount());
    } finally {
        adapter.requestClose();
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) MockFailingConnection(com.adaptris.core.stubs.MockFailingConnection) Adapter(com.adaptris.core.Adapter) TimeoutException(java.util.concurrent.TimeoutException) MalformedObjectNameException(javax.management.MalformedObjectNameException) CoreException(com.adaptris.core.CoreException) ObjectName(javax.management.ObjectName) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedThreadFactory(com.adaptris.core.util.ManagedThreadFactory) CoreException(com.adaptris.core.CoreException) Test(org.junit.Test)

Example 10 with ManagedThreadFactory

use of com.adaptris.core.util.ManagedThreadFactory in project interlok by adaptris.

the class AdapterManagerTest method testForceClose_ErrorOnInit_RequestInit.

@Test
public void testForceClose_ErrorOnInit_RequestInit() throws Exception {
    final TimeInterval waitTime = new TimeInterval(5L, TimeUnit.SECONDS);
    String adapterName = this.getClass().getSimpleName() + "." + getName();
    Adapter adapter = createAdapter(adapterName);
    MockFailingConnection conn = new MockFailingConnection(getName(), "Init");
    conn.setConnectionAttempts(-1);
    conn.setConnectionRetryInterval(new TimeInterval(100L, TimeUnit.MILLISECONDS));
    adapter.getSharedComponents().addConnection(conn);
    List<BaseComponentMBean> mBeans = createJmxManagers(adapter);
    try {
        register(mBeans);
        ObjectName adapterObj = createAdapterObjectName(adapterName);
        final AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class);
        // Create a cyclic barrier to wait for init operation to finish
        final CyclicBarrier gate = new CyclicBarrier(2);
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        Thread initThread = new ManagedThreadFactory().newThread(new Runnable() {

            @Override
            public void run() {
                try {
                    manager.requestInit();
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
                // This code is likely to never trigger, because of the throw above...
                try {
                    gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
                } catch (Exception gateException) {
                }
            }
        });
        initThread.setUncaughtExceptionHandler(exceptionHandler);
        initThread.start();
        try {
            gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
            fail("Adapter init success, when not expected");
        } catch (Exception gateException) {
            // Expected now force close it, because it took too long.
            manager.forceClose();
        }
        assertEquals(ClosedState.getInstance(), manager.getComponentState());
        assertEquals(0, conn.getInitCount());
        assertEquals(0, conn.getCloseCount());
    } finally {
        adapter.requestClose();
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) MockFailingConnection(com.adaptris.core.stubs.MockFailingConnection) Adapter(com.adaptris.core.Adapter) TimeoutException(java.util.concurrent.TimeoutException) MalformedObjectNameException(javax.management.MalformedObjectNameException) CoreException(com.adaptris.core.CoreException) ObjectName(javax.management.ObjectName) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedThreadFactory(com.adaptris.core.util.ManagedThreadFactory) CoreException(com.adaptris.core.CoreException) Test(org.junit.Test)

Aggregations

ManagedThreadFactory (com.adaptris.core.util.ManagedThreadFactory)12 CoreException (com.adaptris.core.CoreException)7 TimeInterval (com.adaptris.util.TimeInterval)4 Test (org.junit.Test)4 Adapter (com.adaptris.core.Adapter)3 MockFailingConnection (com.adaptris.core.stubs.MockFailingConnection)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 ExecutorService (java.util.concurrent.ExecutorService)3 TimeoutException (java.util.concurrent.TimeoutException)3 MalformedObjectNameException (javax.management.MalformedObjectNameException)3 ObjectName (javax.management.ObjectName)3 Channel (com.adaptris.core.Channel)1 StandaloneConsumer (com.adaptris.core.StandaloneConsumer)1 MockMessageListener (com.adaptris.core.stubs.MockMessageListener)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 JMSException (javax.jms.JMSException)1