Example Process
/*
* Copyright © Aricent Holdings Luxembourg S.a.r.l. 2020. All rights reserved
*/
package com.ibm.cc.event;
import java.io.File;
import com.ibm.tenx.core.exception.BaseRuntimeException;
import com.ibm.tenx.core.exception.FileException;
import com.ibm.tenx.core.util.FileUtil;
/**
* Example of a process that creates and posts events. See JavaDoc (index.html) for an overview of installation,
* configuration, and usage.
*
*/
public class ExampleProcess extends Object
{
private static final String NODE_ID = "cron/cron123";
public static void main(String[] args)
{
//EventRepository.init("admin", "admin");
new HeartbeatThread().start();
sleep(7);
doSomething();
sleep(21);
Event.flush();
System.out.println("Done!");
System.exit(0);
}
private static void doSomething()
{
ProcessStartEvent startEvt = Event.processStart(NODE_ID, "EVTEX0001D", "start", "doSomething").post();
try
{
System.out.println("Do something interesting...");
startEvt.status("EVTEX0001D", "status update in the middle of my process").post();
startEvt.status("EVTEX0001D", "a 2nd status update in the middle of my process").post();
for (int i=0; i < 5; i++)
{
ProcessStepStartEvent stepStartEvt = startEvt.stepStart("EVTEX0001D", "start", "doSomethingElse").post();
try
{
doSomethingElse();
}
finally
{
ProcessStepEndEvent stepEnd = stepStartEvt.stepEnd("EVTEX0002D", "end", 0);
// pretend that some steps failed
if (i == 4)
{
stepEnd.setReturnCode(8);
}
stepEnd.post();
}
}
}
finally
{
ProcessEndEvent endEvt = startEvt.end("EVTEX0002D", "end", 0);
// optionally attach a file to the event
endEvt.attach(getExampleFile());
// or just a link to a file (the URL will be recorded but the file will not be fetched unless
// and until an end user wants to see it)
//endEvt.attach("robots.txt", "http://www.google.com/robots.txt");
// and post the event
endEvt.post();
}
}
private static void doSomethingElse()
{
System.out.println("And now we're doing something else...");
}
private static void sleep(int seconds)
{
System.out.println("Sleeping for " + seconds + " seconds...");
try
{
Thread.sleep(seconds * 1000);
}
catch (InterruptedException e)
{
throw new BaseRuntimeException(e);
}
}
private static File getExampleFile()
{
File tempDir = new File(System.getProperty("user.dir"));
File file = new File(tempDir, "example.txt");
if (! file.exists())
{
try
{
FileUtil.write(file, "This SDK makes life so much easier.", true);
}
catch (FileException e)
{
throw new BaseRuntimeException(e);
}
}
return file;
}
private static final class HeartbeatThread extends Thread
{
private HeartbeatThread()
{
super();
setName("Heartbeat");
setDaemon(true);
}
@Override
public void run()
{
while (true)
{
HeartbeatEvent hb = Event.heartbeat(NODE_ID, "GOR999999", "I'm alive!", 30000);
hb.setHeartbeatInterval(15000);
hb.post();
try
{
Thread.sleep(5 * 1000);
}
catch (InterruptedException e)
{
throw new BaseRuntimeException(e);
}
}
}
}
}
Example File Transfer
/*
* Copyright © Aricent Holdings Luxembourg S.a.r.l. 2020. All rights reserved
*/
package com.ibm.cc.event;
import com.ibm.cc.event.FileTransferEvent.TransferDirection;
import com.ibm.tenx.core.exception.BaseRuntimeException;
/**
* Example of a file transfer that creates and posts events. See JavaDoc (index.html) for an overview of installation,
* configuration, and usage.
*
*/
public class ExampleFileTransfer extends Object
{
private static final String NODE_ID = "cron/cron123";
public static void main(String[] args)
{
//EventRepository.init("admin", "admin");
doTransfer();
Event.flush();
System.out.println("Done!");
System.exit(0);
}
private static void doTransfer()
{
System.out.println("Pretending to transfer a file...");
String fileName = "file-" + System.currentTimeMillis() + ".txt";
FileTransferStartEvent startEvt = Event.transferStart(NODE_ID, "EVTEX0008D", "start", fileName, fileName,
TransferDirection.INBOUND).post();
long totalBytesTransferred = 0;
long totalFileSize = 5 * 1024;
try
{
// later, we find out more information about the transfer
FileTransferStatusEvent statusEvt = startEvt.status("EVTEX0009D", "info");
statusEvt.setFileSize(totalFileSize);
statusEvt.setBytesTransferred(0);
statusEvt.post();
for (int i=0; i < 5; i++)
{
sleep(5);
// provide incremental updates about the status of the transfer
long bytesTransferred = 1024 * (i + 1);
int pctComplete = (int) ((bytesTransferred * 100) / totalFileSize);
System.out.println("Pretending to have transferred " + bytesTransferred + " bytes " +
"(" + pctComplete + "%)...");
startEvt.status("EVTEX0009D", "info", bytesTransferred, pctComplete).post();
totalBytesTransferred += bytesTransferred;
}
}
finally
{
// see ExampleProcess for examples of attaching actual files or links to files
// notify that the transfer has completed
startEvt.end("EVTEX0015D", "end", totalBytesTransferred, 0).post();
}
}
private static void sleep(int seconds)
{
System.out.println("Sleeping for " + seconds + " seconds...");
try
{
Thread.sleep(seconds * 1000);
}
catch (InterruptedException e)
{
throw new BaseRuntimeException(e);
}
}
}