Developing Apama Applications > Developing Apama Applications in EPL > Defining What Happens When Matching Events Are Found > Generating events > Sending events to com.apama.Channel objects
Sending events to com.apama.Channel objects
A com.apama.Channel object is particularly useful when writing services that can be used in both distributed and local systems. For example, by using a Channel object to represent the source of a request, you could write a service monitor so that the same code sends a response to a service request. You would not need to have code for sending responses to channels and separate code for sending responses to contexts.
Consider the following Request event and Service monitor definitions:
event Request {
...
Channel source;
}
 
monitor Service {
action onload {
monitor.subscribe('Requests');
Request req;
on all Request():req {
Response rep := Response(...);
send rep to req.source;
}
}
}
EPL code in a context in the same correlator as the Service monitor could send a Request event with the source field set to context.current() and would receive the Response event that the Service monitor sends. For example:
monitor LocalRequester {
action onload {
Request req := Request(...);
req.source := Channel(context.current());
send req to 'Requests';
 
Response rep;
on all Response():rep {
...
}
}
}
Now consider a monitor that is in a correlator that is connected to the Service monitor host correlator. For example, the correlators can be connected by means of engine_connect. The remote monitor could send a Request event with the source field set to a Channel object that contains the name of a channel that the remote monitor is subscribed to. For example:
monitor RemoteRequester {
action onload {
monitor.subscribe('Responses');
 
Request req := Request(...);
req.source := Channel('Responses');
send req to 'Requests';
 
Response rep;
on all Response():rep {
...
}
}
}
In this example, if the correlators are connected by means of engine_connect then the connections would need to be subscribed to the Requests channel and the Responses channel. As you can see, the service monitor does not require different code according to whether the request is coming from a local or remote context. The service monitor simply sends the response back to the source and it does not matter whether the source is a context or a channel.
You can send a Channel object from one Apama component to another Apama component only when the Channel object contains a string. You cannot send a Channel object outside a correlator when it contains a context.
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.