Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Html-bobswift
<meta http-equiv="refresh" content="5; URL=https://docs.perspectium.com/display/helium/Sharer+Code+Example" />
<body>
  <p>Redirecting... If you are not redirected in five seconds, <a href="https://docs.perspectium.com/display/helium/Sharer+Code+Example/">click here</a>.</p>
</body>

The following is a simple example of a Sharer Handler. We'll use this example as a means to outline how the framework is used to create a handler.

Code Block
languagejava
import com.perspectium.api.Message;
import com.perspectium.logging.PerspectiumLogger;
import com.perspectium.replicator.ASharer;
import com.perspectium.replicator.AgentConfig;
 
public class EchoSharer extends ASharer {
    final static PerspectiumLogger Log = PerspectiumLogger.create(EchoSharer.class);
 
    private static final String DEFAULT_ECHO_TEXT = "Hi there - this is an echoed message ";
    private static final String ECHO_MESSAGE = "echo_message";
    private static final String ECHO_ITERATIONS = "echo_iterations";
 
    private Message fMessage;
    private String fEchoText;
    private int fEchoIterations = 1;
 
    public void processMessages() {
        try {
            for (int iteration = 1; iteration <= fEchoIterations; iteration++) {
                String text = fEchoText + " " + String.valueOf(iteration);
                Log.info("publishing text: " + text);
 
                fMessage = new Message.Builder()
                        .topic(AgentConfig.getTopic(fTaskName))
                        .type(AgentConfig.getType(fTaskName))
                        .key(AgentConfig.getKey(fTaskName))
                        .name(AgentConfig.getName(fTaskName))
                        .value(text)
                        .build();
 
                publish(fMessage);
            }
 
            Log.info(String.format("task: %s echoed %s message(s) to the queue: %s",  fTaskName, fEchoIterations, fQueueName));
 
        } catch (Exception e) {
            Log.error("Error:" + e.getMessage());
            e.printStackTrace();
	} 
    }
 
    @Override
    public void initialize(final String taskName) {
        super.initialize(taskName);
 
        fEchoText = AgentConfig.getStringValue(fTaskName, ECHO_MESSAGE);
 
        if (fEchoText == null)
            fEchoText = DEFAULT_ECHO_TEXT;
 
        if (AgentConfig.taskHasElement(fTaskName, ECHO_ITERATIONS))
            fEchoIterations = AgentConfig.getIntValue(fTaskName, ECHO_ITERATIONS);
 
    }
}

The EchoSharer class extends the abstract class ASharer which is required for all Sharer handlers. The Sharer handler must implement the processMessages() method, which will be called by the framework when the task is scheduled to run.

Initialize

The initialize method will be called with the taskName string set to the value of <task_name> directive defined within the conf/agent.xml configuration file. Perform any configuration validation to ensure everything is setup prior to the processMessages() method being called.

...