File: //opt/OV/OpC/examples/jopcagtapi/JOpcAgtMsgTest.java
import java.io.*;
import java.util.*;
import com.hp.openview.ib.api.jopc.*;
class JOpcAgtMsgTest
{
private static BufferedReader stdin = null;
private static String lastID = null;
private static JOpcAgentMessage aMsg = null;
public static void main(String args[]) throws JOpcException, IOException,
ClassNotFoundException
{
System.out.println("JOpcAgtMsgTest Startup");
stdin = new BufferedReader(new InputStreamReader(System.in));
try {
createAgentMsg();
sendAgentMsg();
System.out.print("Acknowledge Message? [y|n] ");
System.out.flush();
String resp = stdin.readLine();
if (resp.equals("y")) ackMsgFromManagedNode();
}
catch(JOpcException jopce){
System.out.println("JOpcAgtMsgTest Exception occured!");
System.out.println("Message: " + jopce.getMessage());
System.out.println("Stack Trace: " + jopce.getStack());
}
} // END main METHOD
private static void createAgentMsg() throws JOpcException, IOException
{
aMsg = new JOpcAgentMessage();
System.out.print("Enter message text: ");
System.out.flush();
String txt = stdin.readLine();
aMsg.setMsgtext(txt);
System.out.print("Enter object: ");
System.out.flush();
String obj = stdin.readLine();
aMsg.setObject(obj);
System.out.print("Enter application: ");
System.out.flush();
String appl = stdin.readLine();
aMsg.setApplication(appl);
System.out.print("Enter severity {4,8,16,32,64,128}: ");
System.out.flush();
String sev = stdin.readLine();
long sevVal = aMsg.getSeverity();
if((sev != null) && (sev.length() > 0))
{
try
{
Long l = new Long(sev);
sevVal = l.longValue();
}
catch(NumberFormatException e) {}
}
aMsg.setSeverity(sevVal);
} // END createAgentMsg METHOD
private static void sendAgentMsg() throws JOpcException, IOException
{
/*
* NOTE: Some templates must be distributed to the agent for an opcmsg to be
* forwarded to the management server's message browser. This is also the case
* when using opcmsg() on the command line. Minimally, you can distribute the
* opcmsg(1|3) message template to the agent.
*/
aMsg.setDataInfo(aMsg.OPC_REMARK_FOR_ACK); // this step is necessary to acknowledge
// the message later.
aMsg.send();
lastID = aMsg.getMsgid();
System.out.println("Sent message with ID: " + lastID);
} // END sendAgentMsg METHOD
private static void ackMsgFromManagedNode() throws JOpcException
{
lastID = aMsg.getMsgid();
aMsg.acknowledge();
System.out.println("Acknowledged message ID " + lastID + " from Managed Node");
} // END ackMsgFromManagedNode METHOD
} // END JOpcAgtTest class