public class MicroService extends java.lang.Object implements Process.EventListener
Modifier and Type | Class and Description |
---|---|
static interface |
MicroService.EventListener
Listens for a specific event emitted by the MicroService
|
class |
MicroService.ServiceAlreadyStartedError
This runtime exception is thrown if an attempt is made to re-start a MicroService if it
has already been started.
|
static interface |
MicroService.ServiceErrorListener
Listens for any errors that may cause the MicroService to shut down unexpectedly.
|
static interface |
MicroService.ServiceExitListener
Listens for when the MicroService has exited gracefully.
|
static interface |
MicroService.ServiceStartListener
Listens for when the MicroService has been inititialized and the environment is ready
to receive event listeners.
|
class |
MicroService.SynchronizerImpl |
Constructor and Description |
---|
MicroService(android.content.Context ctx,
java.net.URI serviceURI)
Creates a new instance of the MicroService referenced by serviceURI
|
MicroService(android.content.Context ctx,
java.net.URI serviceURI,
MicroService.ServiceStartListener start)
Creates a new instance of the MicroService referenced by serviceURI
|
MicroService(android.content.Context ctx,
java.net.URI serviceURI,
MicroService.ServiceStartListener start,
MicroService.ServiceErrorListener error)
Creates a new instance of the MicroService referenced by serviceURI
|
MicroService(android.content.Context ctx,
java.net.URI serviceURI,
MicroService.ServiceStartListener start,
MicroService.ServiceErrorListener error,
MicroService.ServiceExitListener exit)
Creates a new instance of the MicroService referenced by serviceURI
|
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(java.lang.String event,
MicroService.EventListener listener)
Adds an event listener for an event triggered by 'LiquidCore.emit(event, payload)' in
JavaScript.
|
void |
emit(java.lang.String event)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
void |
emit(java.lang.String event,
java.lang.Boolean v)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
void |
emit(java.lang.String event,
java.lang.Double v)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
void |
emit(java.lang.String event,
java.lang.Float v)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
void |
emit(java.lang.String event,
java.lang.Integer v)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
void |
emit(java.lang.String event,
org.json.JSONArray v)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
void |
emit(java.lang.String event,
org.json.JSONObject payload)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
void |
emit(java.lang.String event,
java.lang.Long v)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
void |
emit(java.lang.String event,
java.lang.String v)
Emits an event that can be received by the JavaScript code, if the MicroService has
registered a listener.
|
java.lang.String |
getId()
Each MicroService instance is mapped to a unique String id.
|
Process |
getProcess()
Returns the Node.js Process object.
|
static MicroService |
getService(java.lang.String id)
Each MicroService instance is mapped to a unique String id.
|
void |
onProcessAboutToExit(Process process,
int exitCode)
Called when a node.js process has completed all of its callbacks and has nothing left
to do.
|
void |
onProcessExit(Process process,
int exitCode)
Called after a process has exited.
|
void |
onProcessFailed(Process process,
java.lang.Exception error)
Called in the event of a Process failure
|
void |
onProcessStart(Process process,
JSContext context)
Called when a node.js process is actively running.
|
void |
removeEventListener(java.lang.String event,
MicroService.EventListener listener)
Removes an EventListener previously added with addEventListener.
|
void |
start(java.lang.String... argv)
Starts the MicroService.
|
static void |
uninstall(android.content.Context ctx,
java.net.URI serviceURI)
Uninstalls the MicroService from this host, and removes any global data associated with the
service
|
public MicroService(android.content.Context ctx, java.net.URI serviceURI, MicroService.ServiceStartListener start, MicroService.ServiceErrorListener error, MicroService.ServiceExitListener exit)
ctx
- The android context of this appserviceURI
- The URI (can be a network URL or local file/resource) of the MicroService
codestart
- The ServiceStartListener (optional)error
- The ServiceErrorListener (optional)exit
- The ServiceExitListener (optional)public MicroService(android.content.Context ctx, java.net.URI serviceURI, MicroService.ServiceStartListener start, MicroService.ServiceErrorListener error)
ctx
- The android context of this appserviceURI
- The URI (can be a network URL or local file/resource) of the MicroService
codestart
- The ServiceStartListener (optional)error
- The ServiceErrorListener (optional)public MicroService(android.content.Context ctx, java.net.URI serviceURI, MicroService.ServiceStartListener start)
ctx
- The android context of this appserviceURI
- The URI (can be a network URL or local file/resource) of the MicroService
codestart
- The ServiceStartListener (optional)public MicroService(android.content.Context ctx, java.net.URI serviceURI)
ctx
- The android context of this appserviceURI
- The URI (can be a network URL or local file/resource) of the MicroService
codepublic void addEventListener(java.lang.String event, MicroService.EventListener listener)
LiquidCore.emit('my_event', { stringData: 'foo', bar : 6 });
This will trigger the 'listener' added here, with the JavaScript object represented as a
JSONObject payload.event
- The String event idlistener
- The event listenerpublic java.lang.String getId()
public Process getProcess()
public void removeEventListener(java.lang.String event, MicroService.EventListener listener)
event
- The event for which to unregister the listenerlistener
- The listener to unregisterpublic void emit(java.lang.String event, org.json.JSONObject payload)
LiquidCore.on('my_event', function(payload) {
// Do something with the payload data
console.log(payload.hello);
});
JSONObject foo = new JSONObject();
foo.putString("hello", "world");
myService.emit("my_event", foo);
event
- The event to triggerpayload
- The JSONObject data structure to emitpublic void emit(java.lang.String event)
LiquidCore.on('my_event', function() {
console.log('Received my_event');
});
myService.emit("my_event");
event
- The event to triggerpublic void emit(java.lang.String event, java.lang.Integer v)
LiquidCore.on('my_event', function(val) {
console.log('Received: ' + val);
});
myService.emit("my_event", 5);
event
- The event to triggerv
- value to sendpublic void emit(java.lang.String event, java.lang.Long v)
LiquidCore.on('my_event', function(val) {
console.log('Received: ' + val);
});
myService.emit("my_event", 5L);
event
- The event to triggerv
- value to sendpublic void emit(java.lang.String event, java.lang.Float v)
LiquidCore.on('my_event', function(val) {
console.log('Received: ' + val);
});
myService.emit("my_event", 5.2f);
event
- The event to triggerv
- value to sendpublic void emit(java.lang.String event, java.lang.Double v)
LiquidCore.on('my_event', function(val) {
console.log('Received: ' + val);
});
myService.emit("my_event", 15.6);
event
- The event to triggerv
- value to sendpublic void emit(java.lang.String event, java.lang.String v)
LiquidCore.on('my_event', function(val) {
console.log('Received: ' + val);
});
myService.emit("my_event", "foo");
event
- The event to triggerv
- value to sendpublic void emit(java.lang.String event, java.lang.Boolean v)
LiquidCore.on('my_event', function(val) {
console.log('Received: ' + val);
});
myService.emit("my_event", true);
event
- The event to triggerv
- value to sendpublic void emit(java.lang.String event, org.json.JSONArray v)
LiquidCore.on('my_event', function(val) {
console.log('Received: ' + val);
});
JSONArray a = new JSONArray();
a.put(0);
a.put("two");
myService.emit("my_event", a);
event
- The event to triggerv
- value to sendpublic void start(java.lang.String... argv)
argv
- The list of arguments to sent to the MicroService. This is similar to running
node from a command line. The first two arguments will be the application (node)
followed by the local module code (/home/module/[service.js]. 'argv' arguments
will then be appended in process.argv[2:]public static void uninstall(android.content.Context ctx, java.net.URI serviceURI)
ctx
- The Android contextserviceURI
- The URI of the service (should be the same URI that the service was started
with)public void onProcessStart(Process process, JSContext context)
Process.EventListener
onProcessStart
in interface Process.EventListener
process
- The node.js Process objectcontext
- The JavaScript JSContext for this processpublic void onProcessAboutToExit(Process process, int exitCode)
Process.EventListener
onProcessAboutToExit
in interface Process.EventListener
process
- The node.js Process objectexitCode
- The node.js exit codepublic void onProcessExit(Process process, int exitCode)
Process.EventListener
onProcessExit
in interface Process.EventListener
process
- The defunct node.js Process objectexitCode
- The node.js exit codepublic void onProcessFailed(Process process, java.lang.Exception error)
Process.EventListener
onProcessFailed
in interface Process.EventListener
process
- The node.js process objecterror
- The thrown exceptionpublic static MicroService getService(java.lang.String id)
id
- An id returned by MicroService.getId()