Getting Started with esper in springboot

  springboot

Order

Esper is a relatively classical CEP (Complex Event ProcessingOpen source implementation of (The open source protocol is GPL v2.), here is a brief introduction to how to use it in springboot.

maven

        <dependency>
            <groupId>com.espertech</groupId>
            <artifactId>esper</artifactId>
            <version>6.1.0</version>
        </dependency>

config

    @Bean
    public EPServiceProvider epServiceProvider() {
        com.espertech.esper.client.Configuration config = new com.espertech.esper.client.Configuration();

        //add event type
        config.addEventType(PersonEvent.class);

        EPServiceProvider epServiceProvider = EPServiceProviderManager.getDefaultProvider(config);
        // epServiceProvider.initialize();
        return epServiceProvider;
    }

    @Bean
    public EPAdministrator epAdministrator() {
        return epServiceProvider().getEPAdministrator();
    }

In previous versions, epServiceProvider.initialize () is required. The new version is no longer in use. It is available on esper-6.1.0-sources.jar! /com/espertech/esper/client/epserviceprovidermanager.java has been initialized

public static EPServiceProvider getProvider(String providerURI, Configuration configuration) throws ConfigurationException {
        String providerURINonNull = (providerURI == null) ? EPServiceProviderSPI.DEFAULT_ENGINE_URI : providerURI;

        if (runtimes.containsKey(providerURINonNull)) {
            EPServiceProviderSPI provider = runtimes.get(providerURINonNull);
            if (provider.isDestroyed()) {
                provider = getProviderInternal(configuration, providerURINonNull);
                runtimes.put(providerURINonNull, provider);
            } else {
                provider.setConfiguration(configuration);
            }
            return provider;
        }

        // New runtime
        EPServiceProviderSPI runtime = getProviderInternal(configuration, providerURINonNull);
        runtimes.put(providerURINonNull, runtime);
        runtime.postInitialize();

        return runtime;
    }

On esper-6.1.0-sources.jar! Initialization was also called in the constructor of/com/espertech/esper/core/service/epserviceproviderimpl.java.

/**
     * Constructor - initializes services.
     *
     * @param configuration is the engine configuration
     * @param engineURI     is the engine URI or "default" (or null which it assumes as "default") if this is the default provider
     * @param runtimes      map of URI and runtime
     * @throws ConfigurationException is thrown to indicate a configuraton error
     */
    public EPServiceProviderImpl(Configuration configuration, String engineURI, Map<String, EPServiceProviderSPI> runtimes) throws ConfigurationException {
        if (configuration == null) {
            throw new NullPointerException("Unexpected null value received for configuration");
        }
        if (engineURI == null) {
            throw new NullPointerException("Engine URI should not be null at this stage");
        }
        this.runtimes = runtimes;
        this.engineURI = engineURI;
        verifyConfiguration(configuration);

        serviceListeners = new CopyOnWriteArraySet<EPServiceStateListener>();

        configSnapshot = takeSnapshot(configuration);
        statementListeners = new CopyOnWriteArraySet<EPStatementStateListener>();
        doInitialize(null);
    }

The call here has two initializations, one is the initialization doInitialize(null) of the EPServiceProviderImpl constructor; One is runtime.postInitialize (); Initialize (); Corresponding to the two initialization methods of

    public void initialize() {
        initializeInternal(null);
    }

    private void initializeInternal(Long currentTime) {
        doInitialize(currentTime);
        postInitialize();
    }

Monitoring event

EPStatement statement = epAdministrator.createEPL(epl);
statement.addListener( (newData, oldData) -> {
  String name = (String) newData[0].get("name");
  int age = (int) newData[0].get("age");
  System.out.println(String.format("Name: %s, Age: %d", name, age));
});

Send event

epServiceProvider.getEPRuntime().sendEvent(new PersonEvent("Peter", 10));

doc