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);
}
}
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);
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations