Notification examples
C# and Java implement the notification API slightly differently. This section contains examples for each.
You can create a notification listener that provides information about the job’s current status. The status returned via the notification listener can be one of the following:
- Running
- Done
- Done with Error
- Failed
You can create a single generic notification listener that provides status messages for all jobs, or you can create job-specific notification listeners. If you create a job-specific notification listener, be sure to release it when your job completes. Otherwise, it can become a drain on your memory resources. The following examples show both generic and job-specific listeners.
Note: These examples use the default
service_name SplusServer. The
service_name for your
Spotfire Statistics Services installation is set by the server administrator and might be different. See your server administrator for more information.
C# notification listener example
public class NotificationExample
{
public NotificationExample()
{
IExpressionClient Client = ClientFactory.GetExpressionClient("http://localhost:8080/SplusServer");
// execute command asynchronously (return
// immediately)
SplusDataResult job = Client.Eval("sleep(10)",
new SynchronousJobStartup(2000));
// register notification listener to be called when
// any updates to jobs submitted by this client occur.
Client.SetNotificationListener(HandleNotification);
// Alternatively, you could set up a job-specific
// notification listener that would only handle
// notifications for a specific jobId using:
// Client.SetNotificationListener(job.JobId,
// HandleNotification);
// NOTE: if setting a job-specific listener, be sure
// to remove it using
// RemoveNotificationListener(jobId) when it is no
// longer needed, to prevent excess memory buildup.
}
// method to be called when notification received
private void HandleNotification(NotificationMessage
message)
{
// if job status has changed, write message to
// console.
if (message.Type == eNotificationMessageType.eJobStatusChanged)
{
Console.WriteLine("Job " + message.JobId + " is now " + message.JobStatus);
}
}
}
Java notification listener example
public class NotificationExample implements NotificationListener
{
public NotificationExample() throws NotAuthenticatedException, ApiException, ServerCreationException
{
ExpressionClient client = ClientFactory.getExpressionClient("http://localhost:8080/SplusServer");
// execute command asynchronously (return
// immediately)
SplusDataResult job = client.eval("sleep(10)",
new SynchronousJobStartup(2000));
// register notification listener to be called when
// any updates to jobs submitted by this client
// occur.
client.setNotificationListener(this);
// Alternately, you could set up a job-specific
// notification listener that would only handle
// notifications for a specific jobId using:
// client.setNotificationListener(this,
// job.getJobId());
// NOTE: if setting a job-specific listener, be
// sure to remove it using
// removeNotificationListener(jobId) once it is no
// longer needed, to prevent excess memory
// buildup.
}
// method to be called when notification received
public void handleNotification(NotificationMessage message)
{
// if job status has changed, write message to
// console.
if (message.getType() ==
eNotificationMessageType.eJobStatusChanged)
{
System.out.println("Job " +
message.getJobId() + " is now " +
message.getJobStatus().name());
}
}
}
}
Parent topic: Use Notifications