OSGi后台线程失败 [英] OSGi background thread failure

查看:137
本文介绍了OSGi后台线程失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BundleActivator 运行后台线程并且后台线程有不可恢复的错误时应该怎么做?

What should be done when a BundleActivator runs a background thread, and that background thread has an unrecoverable error?

public class Activator implements BundleActivator
{
   private Thread t;
   @Override
   public void start(BundleContext context) throws Exception
   {
      t = new Thread(new Runnable(){
            @Override
             public void run(){
                while (!Thread.interrupted()){
                  // do something which may throw a runtime exception
               }
            }
         });
      t.start();
   }
   @Override void stop(BundleContext context) throws Exception
   {
      t.interrupt();
      t.join();
   }
}

通过这个例子,我该如何通知OSGi框架线程已经死了,捆绑包是否有效停止而且没有运行?

With this example, how can I notify the OSGi framework that the thread is dead and the bundle is effectively stopped and not running?

推荐答案

看看Peter Kriens如何在< a href =http://www.aqute.biz/Snippets/Stop =nofollow>这篇文章。你需要做的就是在他的catch块中调用激活器上的stop,而不是执行printStackTrace。

Look at how Peter Kriens performs similar actions in this article. All you would need to do with his example is invoke the stop on the activator in his catch block, instead of doing the printStackTrace.

这篇关于OSGi后台线程失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆