astra.lang.Messaging Maven / Gradle / Ivy
Show all versions of astra-apis Show documentation
package astra.lang;
import astra.core.Module;
import astra.messaging.MessageService;
/**
* This class implements support for the management of ASTRA's lightweight messaging infrastructure.
*
*
* The methods provided in this class deliver basic functionality to manage ASTRAs messaging infrastructure.
* This infrastructure is lightweight in that, it does not require extensive configuration.
*
*
* When deploying a message service, the standard usage of the methods in this class is as follows:
*
*
*
* - Install the message service
* - Configure the service by setting properties
* - Start the service
*
*
*
* This deployment is typically done as one of the first tasks by a "main" agent so that other agents
* can communicate using the infrastructure.
*
*
*
* The ASTRA equivalent of this is:
*
*
* rule +!main(list args) {
* messaging.installService("local", "astra.messaging.LocalMQService");
* messaging.startService();
* }
*
*
* @author Rem Collier
*
*/
public class Messaging extends Module {
/**
* Action that installs a Message Service.
*
*
* This method installs a message service in to the local JVM. Message services route
* messages to agents on the local platform or via a remote connection to another platform.
*
*
* All message services must implement the astra.messaging.MessageService interface.
*
*
* @see MessageService
* @param id a unique identifier by which the service can be referenced
* @param clazz the canonical name of the class that implements the service
* @return true if the action succeeds, false otherwise
*/
@ACTION
public boolean installService(String id, String clazz) {
try {
if (!MessageService.hasService(id)) {
MessageService.installService(id, (MessageService) Class.forName(clazz).getDeclaredConstructor(new Class[0]).newInstance(new Object[0]));
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* Action that sets a property for a message service.
*
*
* This method allows you to configure a message service by setting a property of that
* service
*
*
* @param id the id of the service
* @param key the key of the property
* @param value the value of the property
* @return true if the action succeeds, false otherwise
*/
@ACTION
public boolean setProperty(String id, String key, String value) {
MessageService.getService(id).configure(key, value);
return true;
}
/**
* Action that starts the message service.
*
* @param id the id of the service.
* @return true if the action succeeds, false otherwise
*/
@ACTION
public boolean startService(String id) {
MessageService.getService(id).start();
return true;
}
}