Package com.gmv.its.deepsy.hal.platform

Classes

interface  Platform
 Platform. More...
 
class  PlatformLowLevelException
 Platform low level exception. More...
 
interface  PlatformManager
 Platform Manager. More...
 
class  PlatformNotAvailableException
 Platform not available exception. More...
 
interface  Process
 Process. More...
 
interface  ProcessEvent
 Process event. More...
 
interface  ProcessInfo
 ProcessInfo. More...
 
interface  ProcessListEvent
 Process list event. More...
 
interface  ProcessListObserver
 Process list observer. More...
 
interface  ProcessObserver
 Process observer. More...
 
class  ProcessOptions
 ProcessOptions. More...
 
enum  ProcessStatus
 

Detailed Description

To work with Deepsy Platform service it is necessary to use DeepsyPlatformManager located in com.gmv.its.deepsy.platformmanager for getting the initial PlatformManager object.

PlatformManager platformManager = DeepsyPlatformManager.getInstance();


DeepsyPlatformManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:

PlatformManager platformManager = DeepsyPlatformManager.getInstance("192.168.1.10");


PlatformManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a platform object:

try (PlatformManager platformManager = DeepsyPlatformManager.getInstance()){
Platform platform = platformManager.getPlatform();
} catch (PlatformNotAvailableException e) {
e.printStackTrace();
} catch (PlatformLowLevelException e) {
e.printStackTrace();
}


If this object is not inside a try with resources block, it is recommended to call to platformManager.close() in order to finalize properly;

platformManager.close();

Service availability

To detect service availability the DeepsyPlatformManager implements Manager interface which provides methods for getting availability and subscribing to availability events.

class MyManagerObserver implements ManagerObserver{
public void notify(ManagerConnectionEvent event) {
System.out.println(event.getManager().getName() + " in "+ event.getManager().getIp() + " "+ event.getAction().toString());
}
}
public class App {
public static void main(String[] args) {
ManagerObserver observer = new MyManagerObserver();
try (PlatformManager platformManager = DeepsyPlatformManager.getInstance()){
platformManager.subscribe(observer);
} catch (CannotSubscribeException e1) {
e1.printStackTrace();
}
}
}


Examples

  • Getting list of processes
    For getting the list of processes to getProcesses method of Platform.

    package com.gmv.its.dpyjavaexamples.platform;
    import com.gmv.its.deepsy.platform.DeepsyPlatformManager;
    import java.util.List;
    public class GetListOfProcessesApp {
    public static void main(String[] args) {
    Platform platform;
    try (PlatformManager platformManager = DeepsyPlatformManager.getInstance("192.168.0.125")) {
    platform = platformManager.getPlatform();
    List<Process> processList = platform.getProcesses();
    for (Process p : processList) {
    System.out.println(p.getName());
    ProcessInfo info = p.getInfo();
    System.out.println("\t- Status: " + info.getStatus());
    System.out.println("\t- Seconds running: " + info.getSecondsRunning());
    System.out.println("\t- Retries: " + info.getNumberOfRetries());
    System.out.println("\t- MTBF: " + info.getMtbf());
    ProcessOptions opt = p.getOptions();
    System.out.println("\t- ProcessPath: " + opt.getProcessPath());
    System.out.println("\t- Executable: " + opt.getProcessExec());
    System.out.println("\t- Args: " + opt.getArgs());
    System.out.println("\t- Relaunch: " + opt.getRelaunch());
    System.out.println("\t- DelayOnLaunch (seconds): " + opt.getDelayOnLaunch());
    System.out.println("\t- Critical (seconds): " + opt.getCritical());
    System.out.println("\t- PreScriptPath: " + opt.getPreScriptPath());
    System.out.println("\t- PostScriptPath: " + opt.getPostScriptPath());
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }


  • Add/Start/Stop/Delete a process

    package com.gmv.its.dpyjavaexamples.platform;
    import com.gmv.its.deepsy.platform.DeepsyPlatformManager;
    import java.util.Arrays;
    public class ManageAprocessApp {
    public static void main(String[] args) {
    try (PlatformManager platformManager = DeepsyPlatformManager.getInstance("192.168.0.125")) {
    Platform platform = platformManager.getPlatform();
    System.out.println("Adding \"ping\" process with a preScript and post script");
    ProcessOptions options = new ProcessOptions("ping", "/bin/", Arrays.asList("127.0.0.1"), true, 0, false, "/mntDAT/myPreScriptPath.sh", "/mntDAT/myPostScript.sh");
    Process myProcess = platform.addProcess("processTest", options);
    System.out.println("Starting \"ping\" process ");
    myProcess.start();
    System.out.println("Stopping \"ping\" process ");
    myProcess.stop();
    System.out.println("Deleting \"ping\" process ");
    platform.delProcess(myProcess);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }


  • Subscribe to events
    There are 2 types of events in the platform system:

    • ProcessListEvent : This event happens when a process is added to or removed from the platform. To receive this kind of events, it is necessary to subscribe to the Platform object with an object implementing interface ProcessListObserver
    • ProcessEvent : This event happens when a process is started, stopped or relaunched. To receive this kind of events, it is necessary to subscribe to the Process object with an object extending abstract class ProcessObserver

    This example subscribes to every event.

    package com.gmv.its.dpyjavaexamples.platform;
    import java.util.List;
    import com.gmv.its.deepsy.platform.DeepsyPlatformManager;
    class MyProcessObserver implements ProcessObserver {
    public void notify(ProcessEvent event) {
    System.out.println(event.getProcess().getName() + " --> " + event.getStatus().name().toString());
    }
    }
    class MyPlatformObserver implements ProcessListObserver {
    MyProcessObserver myProcessObserver;
    MyPlatformObserver(MyProcessObserver processObserver) {
    myProcessObserver = processObserver;
    }
    public void notify(ProcessListEvent event) {
    System.out.println(event.getProcess().getName() + " --> " + event.getAction().name().toString());
    if (event.getAction() == ProcessListAction.ADDED) {
    try {
    event.getProcess().subscribe(myProcessObserver);
    } catch (CannotSubscribeException e) {
    e.printStackTrace();
    }
    } else {
    try {
    event.getProcess().unSubscribe(myProcessObserver);
    } catch (ObserverNotSubscribedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    public class PlatformEventsApp {
    public static void main(String[] args) {
    MyProcessObserver myProcessObserver = new MyProcessObserver();
    MyPlatformObserver myPlatformObserver = new MyPlatformObserver(myProcessObserver);
    try (PlatformManager platformManager = DeepsyPlatformManager.getInstance("192.168.0.125")){
    Platform platform = platformManager.getPlatform();
    platform.subscribe(myPlatformObserver);
    System.out.println("Subscribed to platform manager events");
    for (Process p : platform.getProcesses()) {
    p.subscribe(myProcessObserver);
    }
    System.out.println("Subscribed to existing processes events");
    Thread.sleep(50000);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }