一、Java访问Domino Objects:(该部分可以IBM的介绍)
Java 对 Domino Objects 的访问是通过高级包 lotus.domino 进行的。
对于本地访问,Java 程序运行在安装有 Notes 客户机的计算机上。本地类由 JNI(Java Native Interface)构建来,用于访问与 JVM(Java Virtual Machine)相同的进程中的 Notes/Domino 二进制文件。
在 lotus.domino 中,NotesFactory 类提供了 createSession 和其他方法,以便在 Java 应用程序和 servlet 中启用对 Domino Objects 的访问。
createSession 的调用没有参数,第一个参数为 null 或者第一个参数为空字符串都表示本地调用。下列代码是等价的:
|
Session s = NotesFactory.createSession()
Session s = NotesFactory.createSession((String)null)
Session s = NotesFactory.createSession("")
|
将 null 转换成 String,以避免过载冲突。
要从应用程序和 servlet 中实现本地调用,路径中必须包含 Notes/Domino 的程序目录,类路径中必须包含 Notes/Domino 程序目录中的 Notes.jar。
例如:我的(版本:8.5.2)在安装目录Notes\framework\shared\eclipse\plugins\com.ibm.notes.java.api.win32.linux_1.5.2.20100811-1131下找到Notes.jar。
本地调用需要用 NotesThread 类管理线程。NotesThread 类扩展了 java.lang.Thread,包含专门针对 Domino 的初始化和终止代码。您有以下三种选择:
Ø 通过继承来执行线程。
Ø 通过 Runnable 接口来执行线程。
Ø 通过静态方法来执行线程。
通过继承来执行线程
要通过继承来执行线程,需要扩展 NotesThread,而不是 Thread,并且需要包含 runNotes 方法,而不是 run 方法。NotesThread 线程可以和任何其他线程一样通过 start 方法来启动。这种方式比静态方法(稍后讨论)容易使用,且不易出错。
|
import lotus.domino.*;
public class myClass extends NotesThread
{
public static void main(String argv[])
{
myClass t = new myClass();
t.start();
}
public void runNotes() // entry point for Notes thread
{
try
{
Session s = NotesFactory.createSession();
// Operational code goes here
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
|
通过 Runnable 接口来执行线程
要通过 Runnable 接口来执行线程,需要实现 Runnable 并包含 run 方法,这与使用线程的任何类相同。当您因为正在扩展其他类而不能扩展 NotesThread 时,可以使用这种方式。
|
import lotus.domino.*;
public class myClass implements Runnable
{
public static void main(String argv[])
{
myClass t = new myClass();
NotesThread nt = new NotesThread((Runnable)t);
nt.start();
}
public void run() // entry point for thread
{
try
{
Session s = NotesFactory.createSession();
// Operational code goes here
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
|
通过静态方法来执行线程
要通过静态方法来执行线程,需要调用 sinitThread() 来初始化线程,调用 stermThread() 来终止线程。stermThread() 的调用必须与 sinitThread() 的调用严格一一对应;推荐将 stermThread 放在“finally”程序块里。静态方法适用于不可能进行继承的线程,或者适用于需要更好地控制基于事件的线程。
|
import lotus.domino.*;
public class myClass
{
public static void main(String argv[])
{
try
{
NotesThread.sinitThread(); // start thread
Session s = NotesFactory.createSession();
// Operational code goes here
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
NotesThread.stermThread(); // must terminate every thread
}
}
}
|
进行本地调用的每个应用程序的线程都必须初始化一个 NotesThread 对象。它包含了访问 Domino Objects 的 AWT 线程。监听器线程必须使用静态方法,因为无法从 NotesThread 中继承它们。
二、Java调用Notes客户端发邮件示例:
|
Session session = null;
Database db = null;
Document doc = null;
try {
NotesThread.sinitThread();
session = NotesFactory.createSession((String)null, (String)null, "password");
db = session.getDatabase(getDominoServer(), getDatabase());
if (!db.isOpen()) {
db.open();
}
doc = db.createDocument();
doc.replaceItemValue("Subject", "这里是标题");
doc.replaceItemValue("Body", "这里是内容");
doc.save();
doc.send(email);
//System.out.println("send email successfully");
} catch (NotesException e1) {
e1.printStackTrace();
} finally {
NotesThread.stermThread();
try {
if (doc != null) {
doc.recycle();
}
if (db != null) {
db.recycle();
}
if (session != null) {
session.recycle();
}
} catch (NotesException e2) {
e2.printStackTrace();
}
}
|
结束!
加载中,请稍候......