Threads and Jobs on release 9

On OpenNTF you can find a project called Thread and Jobs:

This project contains samples showing how to create threads and Eclipse jobs from XPages to run longer taking operations asynchronously without blocking the XPages user interface or to run scheduled tasks.

It turns out that there have been updates in Release 9 in the ThreadSessionExecutor class and no longer a Status object is returned.

So if you want to have the example application working on your Domino 9 server you should remove the Status/iStatus library import and change the return type from Status to Object as followed:

/*
* © Copyright IBM Corp. 2012
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.openntf.samples.thread;

import java.util.Date;
import com.ibm.domino.xsp.module.nsf.ThreadSessionExecutor;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.Session;

/**
* Thread sample
*
* @author priand
* @author Niklas Heidloff (simplified sample)
*
* Updated 2016-10-18 Patrick Kwinten
*
*/

public class ThreadSample {

private static MyThread myThread;

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

public static void startThread() throws NotesException {
if (myThread != null) {
stopThread();
}
try {
synchronized(ThreadSample.class) {
if (myThread == null) {
myThread = new MyThread();
myThread.start();
}
System.out.println(“Thread started”);
}
} catch (Throwable t) {
t.printStackTrace();
}
}

public static void stopThread() {
if (myThread != null) {
synchronized(ThreadSample.class) {
if (myThread != null) {
myThread.stopRequest = true;
myThread = null;
System.out.println(” >> Thread stopping”);
}
}
}
}

private static class MyThread extends Thread {
boolean stopRequest;
private ThreadSessionExecutor < Object > executor;
MyThread() throws NotesException {
this.executor = new ThreadSessionExecutor < Object > () {
@Override
protected Object run(Session session) throws NotesException {
try {
System.out.println(” >> Thread running”);
Database db = session.getDatabase(null, “ThreadsJobs.nsf”);
if (db != null) {
if (!db.isOpen())
db.open();
if (db.isOpen()) {
System.out.println(” >> Database opened: ” + db.getTitle());
Document doc = db.createDocument();
try {
doc.replaceItemValue(“Form”, “ThreadTest”);
doc.replaceItemValue(“DateTime”, session.createDateTime(new Date()));
doc.save();
} finally {
doc.recycle();
}
}
}
} catch (Throwable ex) {
ex.printStackTrace();
}
if (!stopRequest) {
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
//return Status.OK_STATUS;
return “probably OK”;
}
};
}

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

I have not yet managed how to fix the JobScheduler class…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s