How to create and run an agent from Java, using Lotus Notes API
I am trying to create an agent and run it. I have created two classes, one extends AgentBase
and the other one is a normal main class.
I have written the code for agent in the 1st class and trying to run it from the second class. But I am not able to access it.
I am a complete novice here, any guidance would be appreciated.
Agent
Class:
import lotus.domino.*;
import java.util.Vector;
import sun.management.Agent;
public class anagent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext =
session.getAgentContext();
// (Your code goes here)
System.out.println("I am an agent");
} catch(Exception e) {
e.printStackTrace();
}
}
Main
Class:
public static void main(String [] args) throws NotesException {
Session session = null;
Database db = null;
try {
session = NotesFactory.createSession(hostname开发者_开发知识库,UserName, password);
} catch (NotesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean x = session.isValid();
System.out.println("success- "+x);
try {
db = session.getDatabase(null,"LotusDB2.nsf");
} catch (NotesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(db.isOpen())
System.out.println("database open");
//Agent agnt = (Agent) a.firstElement();
//agnt.toString();}
//AgentContext agentContext = session.getAgentContext();
// db = agentContext.getCurrentDatabase();
Vector agents = db.getAgents();
//lotus.domino.Agent agent = new lotus.domino.Agent();
System.out.println("Agents in database:");
if(agents.size()>0) System.out.println("some agents found");
for (int i=0; i<agents.size(); i++)
{
lotus.domino.Agent agent = (lotus.domino.Agent)agents.elementAt(i);
When you say you cannot access the agent, are you getting an error? You do not need to loop through the agent collection looking for the first agent - you can use GetAgent("agentname") and then Agent.run(). If your Java code seems to be finding the agent and running it, but nothing happens, check the log.nsf database on your server for possible errors
These 2 links are a good guide for you to go through. It should help you design java agents using eclipse.
ibm
LekkimWorld
You have defined two main entry points in your notes agent, however in the context of a notes agent, only NotesMain will execute. The static main method would only fire outside of the context of a notes agent, such as when running this in a 3rd party IDE such as Netbeans or Eclipse.
For your code to run from the context of a Notes Agent, simply modify your NotesMain entry point to do all the work you need.
also.. what is that reference to sun.management.Agent for??
import lotus.domino.*;
import java.util.Vector;
public class AnAgent extends AgentBase {
public void NotesMain() {
private Session m_session;
private AgentContext m_agentContext;
private Database m_db;
try {
m_session = getSession();
m_agentContext = m_session.getAgentContext();
// (Your code goes here)
System.out.println("I am an agent");
m_db = m_session.getDatabase("","LotusDB2.nsf");
if(m_db.isOpen())
System.out.println("database open");
Vector agents = m_db.getAgents();
if(agents != null && agents.size()>0) {
System.out.println("some agents found");
for (int i=0; i<agents.size(); i++) {
lotus.domino.Agent agent = (lotus.domino.Agent)agents.elementAt(i);
// whatever it is you are trying to do here...
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
精彩评论