Developing Apama Applications > Writing Correlator Plug-ins > Writing Correlator Plug-ins in Java > Sample plug-ins in Java > A plug-in using enqueue
A plug-in using enqueue
The enqueue sample plug-in illustrates how to pass contexts around and how to enqueue 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 static method enqueueEventTo:
import com.apama.epl.plugin.Context;
import com.apama.epl.plugin.Correlator;

/**
Demonstrates passing contexts around and enqueueing events
*/
public class enqueue_plugin
{
public static void enqueueEventTo(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.enqueueTo(event, context);
}
}
The enqueue_plugin.xml deployment descriptor file contains the name, class, and description of the plug-in in the <plugin> stanza:
<application-classes>
<plugin>
<plugin-name>enqueue_plugin</plugin-name>
<plugin-class>enqueue_plugin</plugin-class>
<description>A test plugin</description>
</plugin>
</application-classes>
The Apama application enqueue_plugin.mon first imports the plug-in and then calls the plug-in's enqueueEventTo method with a variety of contexts.
event A {
string str;
}

monitor EnqueuePluginTest {

// Load the plugin
import "enqueue_plugin" as enqueue_plugin;

action onload {
context c1 := context("receiver");
context c2 := context("sender1");
context c3 := context("sender2");
spawn receiver() to c1; // listen for events
spawn sendto(c1) to c2; // this will work
spawn sendto(c3) to c3; // this won't work because both contexts are the same
}

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!";
}
enqueue_plugin.enqueueEventTo(A("Hello, World").toString(), c);
}
}
Copyright © 2013 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or Terracotta Inc., San Francisco, CA, USA, and/or Software AG (Canada) Inc., Cambridge, Ontario, Canada, and/or, Software AG (UK) Ltd., Derby, United Kingdom, and/or Software A.G. (Israel) Ltd., Or-Yehuda, Israel and/or their licensors.