use of won.bot.framework.bot.Bot in project webofneeds by researchstudio-sat.
the class SpringAwareBotManagerImpl method destroy.
@Override
public void destroy() throws Exception {
logger.info("shutting down bot manager");
synchronized (getMonitor()) {
List<Bot> bots = getBots();
Bot bot;
for (Iterator<Bot> it = bots.iterator(); it.hasNext(); ) {
bot = it.next();
if (bot.getLifecyclePhase().isActive()) {
try {
bot.shutdown();
} catch (Exception e) {
logger.warn("could not shut down bot {}", bot, e);
}
}
it.remove();
}
}
logger.info("bot manager shutdown complete");
}
use of won.bot.framework.bot.Bot in project webofneeds by researchstudio-sat.
the class BotTests method testIsShutdownMultiThreaded.
/**
* Makes sure that the shutdown cannot be entered by more than one thread.
*/
@Test
public void testIsShutdownMultiThreaded() throws BrokenBarrierException, InterruptedException {
// set of threads that managed to enter the shutdown method
final Set<Thread> threadsInShutdown = Collections.synchronizedSet(new HashSet<Thread>());
// bot impl that remembers which thread entered the shutdown method
final Bot bot = new DebugBot() {
@Override
protected void doShutdown() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadsInShutdown.add(Thread.currentThread());
}
};
// start 10 threads that shut down the bot
int numThreads = 10;
final Random rnd = new Random(System.currentTimeMillis());
final CyclicBarrier barrier = new CyclicBarrier(numThreads + 1);
for (int i = 0; i < numThreads; i++) {
Thread thread = new Thread() {
@Override
public void run() {
try {
Thread.sleep(rnd.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
bot.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
try {
barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
};
thread.start();
}
barrier.await();
// make sure the bot is shutdownd
Assert.assertTrue(bot.getLifecyclePhase().isDown());
// make sure it was shutdownd only once
Assert.assertTrue(threadsInShutdown.size() == 1);
}
use of won.bot.framework.bot.Bot in project webofneeds by researchstudio-sat.
the class BotRunnerApp method main.
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("arguments: [bot class name]");
System.exit(1);
}
String botClass = args[0];
SpringApplication app = new SpringApplication(new Object[] { "classpath:/spring/app/botRunner.xml" });
app.setWebEnvironment(false);
ConfigurableApplicationContext applicationContext = app.run(args);
Bot bot = null;
// create a bot instance and auto-wire it
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
bot = (Bot) beanFactory.autowire(Class.forName(botClass), AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
Object botBean = beanFactory.initializeBean(bot, "theBot");
bot = (Bot) botBean;
// (there is no trigger bean in the context)
if (bot instanceof TriggeredBot) {
PeriodicTrigger trigger = new PeriodicTrigger(5000, TimeUnit.MILLISECONDS);
trigger.setInitialDelay(1000);
((TriggeredBot) bot).setTrigger(trigger);
}
BotManager botManager = (BotManager) applicationContext.getBean("botManager");
// adding the bot to the bot manager will cause it to be initialized.
// at that point, the trigger starts.
botManager.addBot(bot);
}
use of won.bot.framework.bot.Bot in project webofneeds by researchstudio-sat.
the class BotManagerImpl method isWorkDone.
@Override
public boolean isWorkDone() {
logger.debug("checking if the bots' work is all done");
synchronized (getMonitor()) {
for (Bot bot : getBots()) {
if (!bot.isWorkDone()) {
logger.debug("bot {} is not done yet", bot);
return false;
}
}
}
logger.debug("all bots are done");
return true;
}
use of won.bot.framework.bot.Bot in project webofneeds by researchstudio-sat.
the class BotManagerImpl method getBotsForNodeURI.
@Override
public List<Bot> getBotsForNodeURI(final URI wonNodeUri) {
{
List<Bot> botList = botListByUri.get(wonNodeUri);
if (botList != null && botList.size() > 0)
return botList;
}
List<Bot> botList = new ArrayList<Bot>();
for (Bot mybot : bots) {
if (mybot.knowsNodeURI(wonNodeUri)) {
synchronized (getMonitor()) {
botList.add(mybot);
}
}
}
this.botListByUri.put(wonNodeUri, botList);
return botList;
}
Aggregations