All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.openservices.ons.api.exactlyonce.ExactlyOnceConsumerBean Maven / Gradle / Ivy

There is a newer version: 1.9.3.Final
Show newest version
package com.aliyun.openservices.ons.api.exactlyonce;

import java.util.Map;
import java.util.Properties;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.MessageSelector;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.aliyun.openservices.ons.api.exception.ONSClientException;

/**
 * {@code ExactlyOnceConsumerBean}用于将{@link ExactlyOnceConsumer}集成至Spring Bean中
 */
public class ExactlyOnceConsumerBean implements ExactlyOnceConsumer {
    /**
     * 需要注入该字段,指定构造{@code ExactlyOnceConsumer}实例的属性,具体支持的属性详见{@link PropertyKeyConst}
     *
     * @see ExactlyOnceConsumerBean#setProperties(Properties)
     */
    private Properties properties;

    /**
     * 通过注入该字段,在启动{@code ExactlyOnceConsumer}时完成Topic的订阅
     */
    private Map subscriptionTable;

    private ExactlyOnceConsumer exactlyOnceConsumer;

    @Override
    public boolean isStarted() {
        return this.exactlyOnceConsumer.isStarted();
    }

    @Override
    public boolean isClosed() {
        return this.exactlyOnceConsumer.isClosed();
    }

    /**
     * 启动该{@code ExactlyOnceConsumer}实例,建议配置为Bean的init-method
     */
    @Override
    public void start() {
        if (null == this.properties) {
            throw new ONSClientException("properties not set");
        }

        if (null == this.subscriptionTable) {
            throw new ONSClientException("subscriptionTable not set");
        }

        this.exactlyOnceConsumer = ExactlyOnceONSFactory.createExactlyOnceConsumer(this.properties);

        for (final Map.Entry next : this.subscriptionTable.entrySet()) {
            this.subscribe(next.getKey().getTopic(), next.getKey().getExpression(), next.getValue());
        }

        this.exactlyOnceConsumer.start();
    }

    @Override
    public void updateCredential(Properties credentialProperties) {
        if (this.exactlyOnceConsumer != null) {
            this.exactlyOnceConsumer.updateCredential(credentialProperties);
        }
    }

    /**
     * 关闭该{@code ExactlyOnceConsumer}实例,建议配置为Bean的destroy-method
     */
    @Override
    public void shutdown() {
        if (this.exactlyOnceConsumer != null) {
            this.exactlyOnceConsumer.shutdown();
        }
    }

    @Override
    public void subscribe(String topic, String subExpression, MessageListener listener) {
        if (null == this.exactlyOnceConsumer) {
            throw new ONSClientException("subscribe must be called after ExactlyOnceConsumerBean started");
        }
        this.exactlyOnceConsumer.subscribe(topic, subExpression, listener);
    }

    @Override
    public void subscribe(String topic, MessageSelector selector, MessageListener listener) {
        if (null == this.exactlyOnceConsumer) {
            throw new ONSClientException("subscribe must be called after ExactlyOnceConsumerBean started");
        }
        this.exactlyOnceConsumer.subscribe(topic, selector, listener);
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public Map getSubscriptionTable() {
        return subscriptionTable;
    }

    public void setSubscriptionTable(Map subscriptionTable) {
        this.subscriptionTable = subscriptionTable;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy