Search in sources :

Example 1 with Bot

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");
}
Also used : Bot(won.bot.framework.bot.Bot) BeansException(org.springframework.beans.BeansException)

Example 2 with Bot

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);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) DebugBot(won.bot.impl.DebugBot) DebugBot(won.bot.impl.DebugBot) Bot(won.bot.framework.bot.Bot) BaseBot(won.bot.framework.bot.base.BaseBot) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 3 with Bot

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);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BotManager(won.bot.framework.manager.BotManager) SpringApplication(org.springframework.boot.SpringApplication) TriggeredBot(won.bot.framework.bot.base.TriggeredBot) Bot(won.bot.framework.bot.Bot) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) TriggeredBot(won.bot.framework.bot.base.TriggeredBot) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger)

Example 4 with 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;
}
Also used : Bot(won.bot.framework.bot.Bot)

Example 5 with Bot

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;
}
Also used : Bot(won.bot.framework.bot.Bot)

Aggregations

Bot (won.bot.framework.bot.Bot)8 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)4 Test (org.junit.Test)4 BaseBot (won.bot.framework.bot.base.BaseBot)4 DebugBot (won.bot.impl.DebugBot)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 BeansException (org.springframework.beans.BeansException)1 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)1 SpringApplication (org.springframework.boot.SpringApplication)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)1 TriggeredBot (won.bot.framework.bot.base.TriggeredBot)1 BotManager (won.bot.framework.manager.BotManager)1