Developing Apama Applications > Developing Correlator Plug-ins > Writing Correlator Plug-ins in Java > Sample plug-ins in Java > A plug-in in Java that sends events
A plug-in in Java that sends events
This sample plug-in shows how to pass contexts around and how to send events to specific contexts.
The Java class for the plug-in imports com.apama.epl.plugin.Context and com.apama.epl.plugin.Correlator and it declares a public method that sends an event to a channel and another public method that sends an event to a particular context:
import com.apama.epl.plugin.Context;
import com.apama.epl.plugin.Correlator;

/**
Demonstrates passing contexts around and sending events
*/
public class SendPlugin
{
public static void sendEventToChannel(String event, String channel)
{
// Send the event to the channel
Correlator.sendTo(event,channel);

public static void sendEventTo(String event, Context context)
{
// Get the current context we're running in
Context current = Context.getCurrent();

// For some reason we don't want to let you do this.
// An exception will terminate the monitor instance
if (current.equals(context))
{
throw new IllegalArgumentException("Please don't use this function
to send events to the current context!");
}
// Send the event to the other context
Correlator.sendTo(event, context);
}
}
The SendPlugin.xml deployment descriptor file contains the name, class, and description of the plug-in in the <plugin> stanza:
<application-classes>
<plugin>
<plugin-name>SendPlugin</plugin-name>
<plugin-class>SendPlugin</plugin-class>
<description>A test plugin</description>
</plugin>
</application-classes>
The Apama application SendPlugin.mon first imports the plug-in and then calls the plug-in's sendEventToChannel() method as well as its sendEventTo() method with a variety of contexts.
event A {
string str;
}

monitor SendPluginTest {
 
// Load the plugin
import "SendPlugin" as send_plugin;
 
action onload {
context c1 := context("receiver");
context c2 := context("receiver2");
context c3 := context("sender");
context c4 := context("sender2");
context c5 := context("sender3");
spawn receiver() to c1; // listen for events
spawn channelReceiver("SampleChannel") to c2; // listen for events
spawn sendto(c1) to c3; // this will work
spawn sendto(c4) to c4; // this won't work because both contexts are the same
spawn sendtoChannel("SampleChannel") to c5;
}
 
action channelReceiver(string chan)
{
monitor.subscribe(chan);
A a;
on all A(): a {
print "channel receiver got: "+a.toString();
}
}
 
action sendtoChannel(string chan)
{
on all A() {
print "sender really shouldn't get anything!";
}
send_plugin.sendEventToChannel(A("Hello, World").toString(), chan);
}
 
action receiver()
{
A a;
on all A(): a {
print "receiver got: "+a.toString();
}
}
 
action sendto(context c)
{
on all A() {
print "sender really shouldn't get anything!";
}
send_plugin.sendEventTo(A("Hello, World").toString(), c);
}
}
Copyright © 2013-2015 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.