Skip Headers
Oracle® Complex Event Processing Developer's Guide
11g Release 1 (11.1.1.4.0) for Eclipse

Part Number E14301-04
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

16 Configuring Custom Spring Beans

This chapter describes how to code and register custom Spring beans, including:

16.1 Overview of Custom Spring Beans

A Spring bean is a Plain Old Java Objects (POJO) managed by the Spring framework. You register a Spring bean in the EPN assembly file using the standard bean element.

A Spring bean is not a type of stage: it cannot be monitored by the Oracle CEP monitoring framework, cannot use the configuration metadata annotations, and cannot be set to record and play-back events that pass through it.

To add a POJO to your Oracle CEP application:

16.1.1 Spring Bean Event Sources and Event Sinks

Standard Spring beans can be event sources, event sinks, or both. Event sources generate events, event sinks receive events.

16.1.1.1 Spring Beans as Event Sources

You specify that a standard Spring bean component in your EPN is an event source by implementing the com.bea.wlevs.ede.api.StreamSource or RelationSource API.

The bean may also optionally implement the various lifecycle interfaces, such as InitializingBean, DisposableBean, and the active interfaces, such as RunnableBean. If a Spring-bean implements Runnable but not RunnableBean, Oracle CEP does not run it in a thread. This is different behavior from an event bean.

The Spring bean event source can make use of the configuration metadata annotations, such as @Prepare, @Rollback, and @Activate.

You register the Spring bean in the EPN assembly file in the standard way using the bean element. For example:

<bean id="bean" class="com.acme.BeanSource" />

In this example, the Java class BeanSource.java implements the com.bea.wlevs.ede.api.StreamSource or RelationSource API.

16.1.1.2 Spring Beans as Event Sinks

You specify that a standard Spring bean component in your EPN is an event sink by implementing the com.bea.wlevs.ede.api.StreamSink or RelationSinkn API.

The bean may also optionally implement the various lifecycle interfaces, such as InitializingBean, DisposableBean, and the active interfaces, such as RunnableBean. If a Spring-bean implements Runnable but not RunnableBean, Oracle CEP does not run it in a thread. This is different behavior from an event bean.

The Spring bean event source can make use of the configuration metadata annotations, such as @Prepare, @Rollback, and @Activate.

You register the Spring bean in the EPN assembly file in the standard way using the bean element. You can then specify this bean as an event sink of some other stage in the EPN.

You reference event sinks in the EPN assembly file using the wlevs:listener element:

<wlevs:channel id="S2" advertise="true" event-type="StockEvent" >
    <wlevs:listener ref="bean"/>
    <wlevs:source ref="cacheProcessor"/>
</wlevs:channel>
 
<bean id="bean" class="com.bea.wlevs.example.cachecql.Bean" />

In this example, the Java class Bean.java implements the com.bea.wlevs.ede.api.StreamSink or RelationSink API.

16.2 Implementing a Custom Spring Bean

The following procedure describes the typical steps for creating a custom event bean.

To implement a custom event bean:

  1. Program the custom event bean Java class.

    If your custom event bean is an event source, see Section 16.2.1, "Implementing a Custom Spring Bean as an Event Source."

    If your custom event bean is an event sink, see Section 16.2.2, "Implementing a Custom Spring Bean as an Event Sink."

    If your custom event bean is both an event source and event sink, then see both sections.

  2. Update the EPN assembly file with custom event bean and custom event bean factory (if used) registration info.

    See Section 16.3, "Configuring the Custom Spring Bean EPN File."

  3. Optionally extend the configuration of the custom event bean if its basic one is not adequate.

    See Chapter 19, "Extending Component Configuration."

16.2.1 Implementing a Custom Spring Bean as an Event Source

The following example shows a custom Spring bean class as an event source ; see the explanation after the example for coding guidelines that correspond to the Java code in bold.

package com.acme;

import com.bea.wlevs.ede.api.StreamSender;
import com.bea.wlevs.ede.api.StreamSource;
import com.bea.wlevs.ede.api.RunnableBean;
 
public class SpringBeanSource implements RunnableBean, StreamSource {

    public void setEventSender (StreamSender streamSender) {
    ...
    }

    public void run() {
    ...
    }

    public synchronized void suspend() throws Exception {
    ...
    }
}

Follow these guidelines when programming the custom Spring bean Java class; code snippets of the guidelines are shown in bold in the preceding example:

  • Import the interfaces and classes of the Oracle CEP API:

    import com.bea.wlevs.ede.api.StreamSender;
    import com.bea.wlevs.ede.api.StreamSource;
    import com.bea.wlevs.ede.api.RunnableBean;
    

    Because the custom Spring bean is an event source it must implement the StreamSource interface. If you want the custom Spring bean to run in a thread, also implement RunnableBean. The StreamSender interface sends event types to the next component in your application network. For full details of these APIs, see Oracle Fusion Middleware Java API Reference for Oracle Complex Event Processing.

  • The custom Spring bean class must implement the StreamSource and RunnableBean interfaces because it is an event source and will run in its own thread:

    public class HelloWorldAdapter implements RunnableBean, StreamSource {
    

    The StreamSource interface provides the StreamSender that you use to send events.

  • Because the custom Spring bean implements the RunnableBean interface, your adapter must then implement the run method:

    public void run() {...
    

    This is where you should put the code that reads the incoming data, such as from a market feed, and convert it into an Oracle CEP event type, and then send the event to the next component in the network. Refer to the documentation of your data feed provider for details on how to read the incoming data. See Section 24.2.2.2, "Accessing Third-Party JAR Files" for information about ensuring you can access the vendor APIs if they are packaged in a third-party JAR file.

  • Because the custom Spring bean implements StreamSource, you must implement the setEventSender method, which passes in the StreamSender that you use to send events:

    public void setEventSender(StreamSender sender) { ...
    
  • If, as is typically the case, your custom Spring bean implements SuspendableBean, you must implement the suspend method that stops the adapter when, for example, the application is undeployed:

    public synchronized void suspend() throws Exception { ... 
    

16.2.2 Implementing a Custom Spring Bean as an Event Sink

The following sample code shows a Spring bean from HelloWorld application that acts as an event sink; see the explanation after the example for the code shown in bold:

package com.bea.wlevs.example.helloworld;
 
import com.bea.wlevs.ede.api.StreamSink;
import com.bea.wlevs.event.example.helloworld.HelloWorldEvent;
 
public class HelloWorldBean implements StreamSink {
 
    public void onInsertEvent(Object event) {
        if (event instanceof HelloWorldEvent) {
            HelloWorldEvent helloWorldEvent = (HelloWorldEvent) event;
            System.out.println("Message: " + helloWorldEvent.getMessage());
        }   
    }
 
}

The programming guidelines shown in the preceding example are as follows:

  • Your bean must import the event type of the application, which in the HelloWorld case is HelloWorldEvent:

    import com.bea.wlevs.event.example.helloworld.HelloWorldEvent;
    
  • Your bean must implement the com.bea.wlevs.ede.api.StreamSink interface:

    public class HelloWorldBean implements StreamSink {...
    
  • The StreamSink interface has a single method that you must implement, onInsertEvent(java.lang.Object), which is a callback method for receiving events. The parameter of the method is an Object that represents the actual event that the bean received from the component that sent it the event:

    public void onInsertEvent(Object event)
    
  • The data type of the events is determined by the event type you registered in the EPN assembly file of the application. In the example, the event type is HelloWorldEvent; the code first ensures that the received event is truly a HelloWorldEvent:

    if (event instanceof HelloWorldEvent) {
        HelloWorldEvent helloWorldEvent = (HelloWorldEvent) event;
    

    This event type is a JavaBean that was configured in the EPN assembly file as shown:

    <wlevs:event-type-repository>
        <wlevs:event-type type-name="HelloWorldEvent">
           <wlevs:class>
             com.bea.wlevs.event.example.helloworld.HelloWorldEvent
           </wlevs:class>
        </wlevs:event-type>
    </wlevs:event-type-repository>
    

    See Section 4.3, "Creating EPN Assembly Files" for procedural information about creating the EPN assembly file, and Appendix C, "Schema Reference: EPN Assembly spring-wlevs-v11_1_1_3.xsd" for reference information.

  • Events are instances of the appropriate JavaBean, so you access the individual properties using the standard getXXX methods. In the example, the HelloWorldEvent has a property called message. You access this property using method getMessage:

    System.out.println("Message: " + helloWorldEvent.getMessage());
    

For complete API reference information about the Oracle CEP APIs described in this section, see the Oracle Fusion Middleware Java API Reference for Oracle Complex Event Processing.

16.3 Configuring the Custom Spring Bean EPN File

The custom event bean and custom event bean factory (if used) must be registered in the EPN assembly file, as discussed in the following sections:

For a complete description of the configuration file, including registration of other components of your application, see Section 4.3, "Creating EPN Assembly Files."

16.3.1 Declaring the Custom Spring Bean Components in your Application

In the EPN assembly file, you use the bean element to declare a custom Spring bean as a component in the event processor network. For example:

<bean id="recplayEventSink"
                  class="com.bea.wlevs.example.recplayRecplayEventSink">
</bean>