3 Adapter and Event Bean Factories

You can use a single custom adapter or event bean implementation in multiple EPNs by implementing and configuring an adapter or event bean factory class. The factory class instantiates an adapter or event bean object to Oracle Stream Analytics applications that request one.

For detail on the APIs described here, see the Java API Reference for Oracle Stream Analytics.

This chapter includes the following sections:.

3.1 Create an Adapter Factory

  1. In your adapter class, implement the com.bea.wlevs.ede.api.Adapter interface so that the adapter can be returned by the factory.

    This is a marker interface, so there are no methods to implement.

    public class BusStopAdapter implements Adapter, StreamSource {
        // Adapter implementation code.
    }
    
  2. Create an adapter factory class that implements the com.bea.wlevs.ede.api.AdapterFactory interface.

    The create method creates and returns instances of the custom adapter.

    import com.oracle.cep.sample.spatial.BusStopAdapter;
    import com.bea.wlevs.ede.api.AdapterFactory;
    
    public class BusStopAdapterFactory implements AdapterFactory {
      public BusStopAdapterFactory() {}
      public synchronized BusStopAdapter create() throws IllegalArgumentException {
      // Your code might have a particular way to create the instance.
         return new BusStopAdapter();
      }
    }
    
  3. In an assembly file, use the wlevs:factory element to register the factory class.

    <wlevs:factory provider-name="busStopAdapterProvider" 
      class="com.oracle.cep.sample.spatial.BusStopAdapterFactory"/>
    
  4. Use the osgi:service element to register the factory as an OSGi service in the assembly file.

    The OSGi service registry scope is all of Oracle Stream Analytics. If more than one application deployed to a given server uses the same adapter factory, register the factory once as an OSGi service.

    1. Add an entry to register the service as an implementation of the com.bea.wlevs.ede.api.AdapterFactory interface. Provide a property, with the key attribute equal to type, and the name by which this adapter provider is referenced.

    2. Add a nested standard Spring bean element to register your specific adapter class in the Spring application context.

    <osgi:service interface="com.bea.wlevs.ede.api.AdapterFactory">
      <osgi:service-properties>
        <entry key="type" value="busStopAdapterProvider"</entry>
      </osgi:service-properties>
      <bean class="com.oracle.cep.sample.spatial.BusStopAdapterFactory" />
    </osgi:service>
    
  5. In applications that use instances of the adapter, configure the adapter by specifying the configured factory as a provider (rather than specifying the adapter by its class name), as shown in the following example:

    <wlevs:adapter id="BusStopAdapter"
      provider="busStopAdapterProvider">
      // ...
    </wlevs:adapter>
    

3.2 Create an Event Bean Factory

  1. In your event bean class, implement the com.bea.wlevs.ede.api.EventBean interface so that the bean can be returned by the factory.

    This is a marker interface, so there are no methods to implement.

    public class TradeListener implements EventBean, StreamSink {
      // Bean implementation code.
    }
    
  2. Create an event bean class that implements the com.bea.wlevs.ede.api.EventBeanFactory interface.

    The create method creates and returns instances of the event bean.

    import com.oracle.cep.example.tradereport.TradeListener;
    import com.bea.wlevs.ede.api.EventBeanFactory;
    
    public class TradeListenerFactory implements EventBeanFactory {
      public TradeListenerFactory() { }
      public synchronized TradeListener create() throws IllegalArgumentException {
      // Your code might have a particular way to create the instance.
        return new TradeListener();
      }
    }
    
  3. In an assembly file, use the wlevs:factory element to register the factory class.

    <wlevs:factory provider-name="tradeListenerProvider" 
      class="com.oracle.cep.example.tradereport.TradeListenerFactory"/>
    
  4. Use the osgi:service element to register the factory as an OSGi service in the assembly file.

    The OSGi service registry scope is all of Oracle Stream Analytics. If more than one application deployed to a given server uses the same adapter factory, register the factory once as an OSGi service.

    1. Add an entry to register the service as an implementation of the com.bea.wlevs.ede.api.EventBeanFactory interface. Provide a property, with the key attribute equal to type, and the name by which this adapter provider is referenced.

    2. Add a nested standard Spring bean element to register your specific adapter class in the Spring application context.

    <osgi:service interface="com.bea.wlevs.ede.api.EventBeanFactory">
      <osgi:service-properties>
        <entry key="type" value="tradeListenerProvider"</entry>
        </osgi:service-properties>
      <bean class="com.oracle.cep.example.tradereport.TradeListenerFactory" />
    </osgi:service>
    
  5. In applications will use instances of the event bean, configure the event bean by specifying the configured event bean factory as a provider (rather than specifying the bean by its class name), as shown in the following example:

    <wlevs:event-bean id="TradeListenerBean" provider="tradeListenerProvider">