use of net.dempsy.threading.ThreadingModel in project Dempsy by Dempsy.
the class DempsyBaseTest method makeNode.
@SuppressWarnings("resource")
protected NodeManagerWithContext makeNode(final String[] ctxArr) {
final List<String> fullCtx = new ArrayList<>(Arrays.asList(ctxArr));
fullCtx.addAll(Arrays.asList(frameworkCtx));
fullCtx.add(TRANSPORT_CTX_PREFIX + transportType + TRANSPORT_CTX_SUFFIX);
if (!"none".equals(serializerType))
fullCtx.add(SERIALIZER_CTX_PREFIX + serializerType + SERIALIZER_CTX_SUFFIX);
LOGGER.debug("Starting node with {}", fullCtx);
final ClassPathXmlApplicationContext ctx = currentlyTracking.track(new ClassPathXmlApplicationContext(fullCtx.toArray(new String[fullCtx.size()])));
final ThreadingModel threading;
currentlyTracking.track(threading = threadingModelSource.apply(currentAppName)).start();
return currentlyTracking.track(new NodeManagerWithContext(new NodeManager().node(ctx.getBean(Node.class)).threadingModel(threading).collaborator(uncheck(() -> currentSessionFactory.createSession())).start(), ctx));
}
use of net.dempsy.threading.ThreadingModel in project Dempsy by Dempsy.
the class TcpTransportTest method testMessage.
@Test
public void testMessage() throws Exception {
try (ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).setNumHandlers(2).setUseLocalHost(true);
final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testMessage"));
final Infrastructure infra = tr.track(new TestInfrastructure(tm));
final TcpAddress addr = r.getAddress(infra);
LOGGER.debug(addr.toString());
final AtomicReference<RoutedMessage> rm = new AtomicReference<>(null);
r.start((Listener<RoutedMessage>) msg -> {
rm.set(msg);
return true;
}, infra);
try (final SenderFactory sf = senderFactory.get()) {
sf.start(new TestInfrastructure(tm) {
@Override
public String getNodeId() {
return "test";
}
});
final Sender sender = sf.getSender(addr);
sender.send(new RoutedMessage(new int[] { 0 }, "Hello", "Hello"));
assertTrue(poll(o -> rm.get() != null));
assertEquals("Hello", rm.get().message);
}
}
}
use of net.dempsy.threading.ThreadingModel in project Dempsy by Dempsy.
the class TcpTransportTest method testConnectionRecovery.
@Test
public void testConnectionRecovery() throws Exception {
try (final ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).setNumHandlers(2).setUseLocalHost(true);
// can't test connection recovery here.
if (!(r instanceof DisruptableRecevier))
return;
final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testConnectionRecovery"));
final Infrastructure infra = tr.track(new TestInfrastructure(tm));
final TcpAddress addr = r.getAddress(infra);
LOGGER.debug(addr.toString());
final AtomicReference<RoutedMessage> rm = new AtomicReference<>(null);
r.start((Listener<RoutedMessage>) msg -> {
rm.set(msg);
return true;
}, infra);
try (final SenderFactory sf = senderFactory.get()) {
sf.start(new TestInfrastructure(tm) {
@Override
public String getNodeId() {
return "test";
}
});
final Sender sender = sf.getSender(addr);
sender.send(new RoutedMessage(new int[] { 0 }, "Hello", "Hello"));
assertTrue(poll(o -> rm.get() != null));
assertEquals("Hello", rm.get().message);
assertTrue(((DisruptableRecevier) r).disrupt(addr));
final AtomicBoolean stop = new AtomicBoolean(false);
final RoutedMessage resetMessage = new RoutedMessage(new int[] { 0 }, "RESET", "RESET");
final Thread senderThread = new Thread(() -> {
while (!stop.get()) {
sender.send(resetMessage);
dontInterrupt(() -> Thread.sleep(100));
}
}, "testConnectionRecovery-sender");
senderThread.start();
try {
assertTrue(poll(o -> "RESET".equals(rm.get().message)));
} finally {
stop.set(true);
if (!poll(senderThread, t -> {
dontInterrupt(() -> t.join(10000));
return !t.isAlive();
}))
LOGGER.error("FAILED TO SHUT DOWN TEST SENDING THREAD. THREAD LEAK!");
}
}
}
}
use of net.dempsy.threading.ThreadingModel in project Dempsy by Dempsy.
the class NioReceiver method start.
@SuppressWarnings("unchecked")
@Override
public void start(final Listener<?> listener, final Infrastructure infra) throws MessageTransportException {
if (!isRunning.get())
throw new IllegalStateException("Cannot restart an " + NioReceiver.class.getSimpleName());
if (binding == null)
// sets binding via side affect.
getAddress(infra);
// before starting the acceptor, make sure we have Readers created.
try {
for (int i = 0; i < readers.length; i++) readers[i] = new Reader<T>(isRunning, address, (Listener<T>) listener, serializer, maxMessageSize);
} catch (final IOException ioe) {
LOGGER.error(address.toString() + " failed to start up readers", ioe);
throw new MessageTransportException(address.toString() + " failed to start up readers", ioe);
}
final ThreadingModel threadingModel = infra.getThreadingModel();
// now start the readers.
for (int i = 0; i < readers.length; i++) threadingModel.runDaemon(readers[i], "nio-reader-" + i + "-" + address);
// start the acceptor
threadingModel.runDaemon(acceptor = new Acceptor(binding, isRunning, readers, address), "nio-acceptor-" + address);
}
use of net.dempsy.threading.ThreadingModel in project Dempsy by Dempsy.
the class TcpTransportTest method runMultiMessage.
private void runMultiMessage(final String testName, final int numThreads, final int numMessagePerThread, final String message, final Serializer serializer) throws Exception {
try (final ServiceTracker tr = new ServiceTracker()) {
final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).setNumHandlers(2).setUseLocalHost(true).setMaxMessageSize(1024 * 1024 * 1024);
final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + "." + testName));
final Infrastructure infra = tr.track(new TestInfrastructure(tm));
final TcpAddress addr = r.getAddress(infra);
LOGGER.debug(addr.toString());
final AtomicLong msgCount = new AtomicLong();
r.start((Listener<RoutedMessage>) msg -> {
msgCount.incrementAndGet();
return true;
}, infra);
final AtomicBoolean letMeGo = new AtomicBoolean(false);
final CountDownLatch waitToExit = new CountDownLatch(1);
final List<Thread> threads = IntStream.range(0, numThreads).mapToObj(threadNum -> new Thread(() -> {
try (final SenderFactory sf = senderFactory.get()) {
sf.start(new TestInfrastructure(null, null) {
@Override
public Map<String, String> getConfiguration() {
final Map<String, String> ret = new HashMap<>();
ret.put(sf.getClass().getPackage().getName() + "." + NioSenderFactory.CONFIG_KEY_SENDER_THREADS, NUM_SENDER_THREADS);
return ret;
}
});
final Sender sender = sf.getSender(addr);
while (!letMeGo.get()) Thread.yield();
for (int i = 0; i < numMessagePerThread; i++) sender.send(new RoutedMessage(new int[] { 0 }, "Hello", message));
try {
waitToExit.await();
} catch (final InterruptedException ie) {
}
}
}, "testMultiMessage-Sender-" + threadNum)).map(th -> chain(th, t -> t.start())).collect(Collectors.toList());
Thread.sleep(10);
// here's we go.
letMeGo.set(true);
// the total number of messages sent should be this count.
assertTrue(poll(new Long((long) numThreads * (long) numMessagePerThread), v -> v.longValue() == msgCount.get()));
// let the threads exit
waitToExit.countDown();
// all threads should eventually exit.
assertTrue(poll(threads, o -> o.stream().filter(t -> t.isAlive()).count() == 0));
}
}
Aggregations