2012-06-26 62 views
1

我一直在寻找在OpenNTF上发布的例子 - http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=Threads%20and%20Jobs我的问题是我似乎无法引用另一个类,它是创建初始线程的主类之外。Xpage Threads - 无法访问其他类

我试图使用基于演示代码(它工作正常顺便说一句)代码 - 我尝试过不同的变化,包括尝试从内部类中调用广播类,并在这种情况下从外部类。在任何情况下,我都会得到一个ClassNotFoundException - 注意Broadcast类与此ThreadSample在同一个包中。

public class ThreadSample { 

private MyThread myThread; 

public boolean isRunning() { 
    return myThread != null; 
} 

public void startThread() 
throws NotesException { 
    if (myThread != null) { 
     stopThread(); 
    } 

    try { 
     { 
      if (myThread == null) { 
       myThread = new MyThread(); 
       myThread.start(); 
      } 
      System.out.println("Thread started"); 
     } 
    } catch (Throwable t) { 
     t.printStackTrace(); 
    } 
} 

public void stopThread() { 
    if (myThread != null) { 
     synchronized (ThreadSample.class) { 
      if (myThread != null) { 
       myThread.stopRequest = true; 
       myThread = null; 
       System.out.println(" >> Thread stopping"); 
      } 
     } 
    } 
} 
public void test(){ 
    System.out.println("HERE in Test"); 
    Broadcast.test_subscribe(); 
} 

class MyThread extends Thread { 
    boolean stopRequest; 
    private ThreadSessionExecutor<IStatus> executor; 

    MyThread() throws NotesException { 

     this.executor = new ThreadSessionExecutor<IStatus>() { 
      @Override 
      protected IStatus run(Session session) throws NotesException { 
       try { 
        System.out.println(" >> Thread running here"); 
        ThreadSample.this.test_subscribe(); 
        System.out.println(" >> After test call"); 
       } catch (Throwable ex) { 
        ex.printStackTrace(); 
       } 
       return Status.OK_STATUS; 
      } 
     }; 
    } 

    public void run() { 
     while (!stopRequest) { 
      try { 
       executor.run(); 
      } catch (Exception ex) { 
      } 
     } 
     System.out.println("Thread left"); 
    } 
} 
} 

回答

1

如果作业在NSF中,那么出于安全原因它不能访问一些核心的eclipse类。例如,IStatus就是这种情况。另一方面,如果您将该类作为插件的一部分进行部署,那么它可能对核心eclipse运行时具有依赖性。 ThreadSessionExecutor中的上下文类加载器也存在一个问题,它已在下一个N/D版本中得到修复。 对于8.5.3,最好将您的作业作为插件进行部署。

+0

感谢菲利普我会尝试,并将其部署为一个插件,看看它是如何去。 – markbarton

+0

您是否有创建和部署不适用于UIComponent的插件的链接?我在这里查看了这些教程http://www-10.lotus.com/ldd/ddwiki.nsf/dx/Creating_an_XPages_Library,但它们适用于一个UI组件。 基于纯代码的插件是否一样? – markbarton

+0

查看XSP入门套件:http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=XSP%20Starter%20Kit –

1

如果您需要在线程中访问会话,则可以使用SessionCloner。然后,您不需要将其部署为插件,因为它不会加载它自己的类加载器。您可能需要更改服务器上的java.policy文件(我在测试之前修改了java.policy,所以我不确定)。

您需要这些进口:

进口com.ibm.domino.xsp.module.nsf.NSFComponentModule;

import com.ibm.domino.xsp.module.nsf.NotesContext;

import com.ibm.domino.xsp.module.nsf.SessionCloner;

类需要两个字段

私人SessionCloner sessionCloner;

private NSFComponentModule module;

在构造:

//初始化到当前会话传递到该真实需要的对象

this.module = NotesContext.getCurrent()getModule();

this.sessionCloner = SessionCloner.getSessionCloner();

在(在Thread类的run方法的运行方法的代码从匿名ThreadSessionExecutor类移动)的run方法:

会话的会话= NULL;

尝试{

NotesContext context = new NotesContext(this。模块);

NotesContext.initThread(context);

session = this.sessionCloner.getSession();

//您的代码

}赶上(Throwable异常){

//错误日志记录,打印堆栈跟踪/等

}最后{

NotesContext.termThread();

尝试{

this.sessionCloner.recycle(); 

}赶上(NotesException除外){}

}

+0

谢谢汤米 - 我会试试看。 – markbarton

+0

@Tommy你对SessionAsSigner对象有什么想法是SessionCloner存在吗?谢谢! –

+0

...好吧,我想我找到了我上面评论的答案。这是:SessionCloner.getDesignerSessionCloner(false | true).getSession() –