1

我一直在查看 OpenNTF 上发布的示例 - http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=Threads%20and%20Jobs我的问题是我似乎无法参考到创建初始线程的主类之外的另一个类。

这是我尝试使用的代码,基于演示代码(顺便说一句,它工作得很好) - 我尝试了不同的变体,包括尝试从内部类中调用广播类,在这种情况下从外部类调用。在所有情况下,我都会得到一个 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");
    }
}
}
4

2 回答 2

1

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

于 2012-06-27T14:54:19.830 回答
1

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

你需要这些进口:

导入 com.ibm.domino.xsp.module.nsf.NSFComponentModule;

导入 com.ibm.domino.xsp.module.nsf.NotesContext;

导入 com.ibm.domino.xsp.module.nsf.SessionCloner;

该类需要两个字段

私人 SessionCloner sessionCloner;

私有 NSFComponentModule 模块;

在构造函数中:

// 初始化传递当前会话所需的对象

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

this.sessionCloner = SessionCloner.getSessionCloner();

在 run 方法中(将代码从匿名 ThreadSessionExecutor 类的 run 方法移动到 Thread 类中的 run 方法):

会话会话 = null;

尝试 {

NotesContext 上下文 = 新的 NotesContext(this.module);

NotesContext.initThread(上下文);

session = this.sessionCloner.getSession();

// 你的代码

} 捕捉(可抛出异常){

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

} 最后 {

NotesContext.termThread();

尝试 {

  this.sessionCloner.recycle();

} 捕捉(NotesException 异常){}

}

于 2012-06-28T05:46:13.813 回答