前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot中使用esper入门

springboot中使用esper入门

作者头像
code4it
发布2018-09-17 14:41:53
1.6K0
发布2018-09-17 14:41:53
举报
文章被收录于专栏:码匠的流水账

esper是一个比较经典的CEP(Complex Event Processing)的开源实现(开源协议为GPL v2),这里简单介绍下如何在springboot中使用。

maven

代码语言:javascript
复制
        <dependency>
            <groupId>com.espertech</groupId>
            <artifactId>esper</artifactId>
            <version>6.1.0</version>
        </dependency>

config

代码语言:javascript
复制
    @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();
    }

旧版的话,需要epServiceProvider.initialize();新版已经不用了,在esper-6.1.0-sources.jar!/com/espertech/esper/client/EPServiceProviderManager.java已经初始化了

代码语言:javascript
复制
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;
    }

在esper-6.1.0-sources.jar!/com/espertech/esper/core/service/EPServiceProviderImpl.java的构造器中也调用了初始化

代码语言:javascript
复制
/**
     * 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);
    }

这里的调用了两个初始化,一个是EPServiceProviderImpl构造器的初始化doInitialize(null);一个是runtime.postInitialize();正好跟epServiceProvider.initialize();的两个初始化方法对应

代码语言:javascript
复制
    public void initialize() {
        initializeInternal(null);
    }

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

监听事件

代码语言:javascript
复制
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));
});

发送事件

代码语言:javascript
复制
epServiceProvider.getEPRuntime().sendEvent(new PersonEvent("Peter", 10));

doc

  • esper-reference
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-08-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • maven
  • config
  • 监听事件
  • 发送事件
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档