In addition to the default receiver types, you can define your own custom receiver, which gives more flexibility to receive events that are sent to WSO2 products. Since each event receiver implementation is an OSGI bundle, you can deploy/undeploy it easily on the WSO2 product. To create a custom event receiver, import org.wso2.carbon.event.input.adaptor.core package with the provided skeleton classes/interfaces required by a custom receiver implementation.
Implementing InputEventAdapter Interface
org.wso2.carbon.event.input.adapter.core.InputEventAdapter interface contains the event receiver logic that will be used to receive events. You should override the below methods when implementing your own custom receiver.
void init(InputEventAdapterListener eventAdaptorListener) throws InputEventAdapterException
This method is called when initiating event receiver bundle. Relevant code segments which are needed when loading OSGI bundle can be included in this method.
void testConnect() throws TestConnectionNotSupportedException, InputEventAdapterRuntimeException, ConnectionUnavailableException
This method checks whether the receiving server is available.
void connect() throws InputEventAdapterRuntimeException, ConnectionUnavailableException
Method connect() will be called after calling the init() method. Intention is to connect to a receiving end and if it is not available "ConnectionUnavailableException" will be thrown.
void disconnect()
disconnect() method can be called when it is needed to disconnect from the connected receiving server.
void destroy()
The method can be called when removing an event receiver. The cleanups that has to be done when removing the receiver can be done over here.
boolean isEventDuplicatedInCluster() (To be edited)
Returns a boolean output stating whether an event is duplicated in a cluster or not.
boolean isPolling() (To be edited)
Checks whether events get accumulated at the adapter and clients connect to it to collect events.
Implementing InputEventAdapterFactory Class
org.wso2.carbon.event.input.adapter.core. InputEventAdapterFactory class can be used as the factory to create your appropriate event receiver type. You should override the below methods when extending your own custom receiver.
public String getType()
This method returns the receiver type as a String.
public List<String> getSupportedMessageFormats()
Specify supported message formats for the created receiver type.
public List<Property> getPropertyList()
Here the properties have to be defined for the receiver. When defining properties you can implement to configure property values from the management console.
public String getUsageTips()
Specify any hints to be displayed in the management console.
public InputEventAdapter createEventAdapter(InputEventAdapterConfiguration eventAdapterConfiguration, Map<String, String> globalProperties)
This method creates the receiver by specifying event adapter configuration and global properties which are common to every adapter type.
Exposing Custom Event Receiver as an OSGI Service
Apart from above, you can maintain a service class under internal\ds\ directory to expose the custom event receiver implementation as an OSGI service. When exposing the service, it needs to expose as “InputEventAdaptorFactory” type. Below is a sample implementation for a service class for a custom defined receiver:
/** * @scr.component name="input.CustomReceiverService.component" immediate="true" */ public class CustomReceiverServiceDS { private static final Log log = LogFactory.getLog(CustomReceiverServiceDS.class); protected void activate(ComponentContext context) { try { InputEventAdaptorFactory customInputEventAdaptorFactory = new CustomInputEventAdaptorFactory(); context.getBundleContext().registerService(InputEventAdaptorFactory.class.getName(), customInputEventAdaptorFactory, null); log.info("Successfully deployed the Custom Event Receiver Service"); } catch (RuntimeException e) { log.error("Can not create the Custom Event Receiver Service ", e); } } }
Furthermore you can have a utility directory as internel\util\ where you can place utility classes required for the custom receiver implementation.
Deploying Custom Event Receiver
Deploying a custom event receiver is very simple in WSO2 CEP 4.0.0. Simply implement the custom event receiver type, build the project and copy the created OSGI bundle that is inside the "target" folder into the <CEP_HOME>/repository/components/dropins. In CEP server startup, you can see the newly created event receiver type service in the server startup logs. The newly created custom event receiver type will also be visible in the UI with necessary properties. Now you can create several instances of this event receiver type.