wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Mach Dich auf die Socken!


A common requirement in corporate systems is "let me know when something is going on". In Notes we use "On document creation or update" triggered agents to process such events. To let external systems know about such a change R8 introduced the web service client. This works well in distributed system, but requires quite some work on both ends. In a recent case I had to optimize the communication between Domino and a task running on the same machine. The existing solution was polling the Domino API in short intervals for updates. Something I would call donkey mode. Sockets to the rescue. A few lines of Java in a triggered agent puts an end to donkey mode and provides the receiving end with all it needs in time:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import lotus.domino.AgentBase;
import lotus.domino.AgentContext;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.DocumentCollection;
import lotus.domino.NotesException;
import lotus.domino.Session;

import com.issc.castle.domino.Utils;

public class JavaAgent extends AgentBase {

 public static String sockethost = "127.0.0.1";
 public static int  socketport = 1234;

 public void NotesMain() {
  Session session = null;
  AgentContext agentContext = null;
  Database db = null;
  DocumentCollection dc = null;
  Document doc = null;

  // The socket elements
  DataOutputStream out = null;
  BufferedReader in = null;
  Socket socketClient = null;
  try {
   // Get the Notes parts
   session = getSession();
   agentContext = session.getAgentContext();
   db = agentContext.getCurrentDatabase();
   dc = agentContext.getUnprocessedDocuments();

   // Get the socket
   socketClient = new Socket(sockethost, socketport);
   in = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
   out = new DataOutputStream(socketClient.getOutputStream());

   doc = dc.getFirstDocument();
   while (doc != null) {
    Document nextDoc = dc.getNextDocument(doc);
    this.signalOneDocument(doc, in, out);
    Utils.shred(doc);
    doc = nextDoc;
   }

   // Mark them done
   dc.updateAll();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   Utils.shred(doc, dc, db, agentContext, session);
   // Close them
   try {
    if (out != null) {
     out.close();
    }
    if (in != null) {
     in.close();
    }
    if (socketClient != null) {
     socketClient.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 private void signalOneDocument(final Document doc, final BufferedReader in, final DataOutputStream out) {
  try {
   String notesURL = doc.getNotesURL();
   out.writeBytes(notesURL);
   out.writeBytes("|");
  } catch (NotesException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }
}

No libraries to load, the only utility function used is Utils.shred() which is a error wrapped recycle call.
As usual YMMV
(Bad German pun in the title)

Posted by on 09 May 2016 | Comments (0) | categories: IBM Notes Java

Comments

  1. No comments yet, be the first to comment