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

org.wildfly.clustering.ejb.infinispan.timer.InfinispanTimer Maven / Gradle / Ivy

There is a newer version: 33.0.2.Final
Show newest version
/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.clustering.ejb.infinispan.timer;

import org.wildfly.clustering.ejb.timer.Timer;
import org.wildfly.clustering.ejb.timer.TimerManager;
import org.wildfly.clustering.ejb.timer.TimerRegistry;
import org.wildfly.clustering.ee.Remover;
import org.wildfly.clustering.ee.Scheduler;
import org.wildfly.clustering.ee.cache.tx.TransactionBatch;
import org.wildfly.clustering.ejb.timer.ImmutableTimerMetaData;
import org.wildfly.clustering.ejb.timer.TimeoutListener;

/**
 * @author Paul Ferraro
 */
public class InfinispanTimer implements Timer {

    private final TimerManager manager;
    private final I id;
    private final ImmutableTimerMetaData metaData;
    private final Scheduler scheduler;
    private final TimeoutListener listener;
    private final Remover remover;
    private final TimerRegistry registry;

    private volatile boolean canceled = false;

    public InfinispanTimer(TimerManager manager, I id, ImmutableTimerMetaData metaData, Scheduler scheduler, TimeoutListener listener, Remover remover, TimerRegistry registry) {
        this.manager = manager;
        this.id = id;
        this.metaData = metaData;
        this.scheduler = scheduler;
        this.listener = listener;
        this.remover = remover;
        this.registry = registry;
    }

    @Override
    public I getId() {
        return this.id;
    }

    @Override
    public ImmutableTimerMetaData getMetaData() {
        return this.metaData;
    }

    @Override
    public boolean isActive() {
        return this.scheduler.contains(this.id);
    }

    @Override
    public boolean isCanceled() {
        return this.canceled;
    }

    @Override
    public void cancel() {
        this.suspend();
        this.remove();
        this.canceled = true;
    }

    private void remove() {
        this.registry.unregister(this.id);
        this.remover.remove(this.id);
    }

    @Override
    public void invoke() throws Exception {
        this.listener.timeout(this.manager, this);
    }

    @Override
    public void suspend() {
        this.scheduler.cancel(this.id);
    }

    @Override
    public void activate() {
        if (!this.isActive()) {
            this.scheduler.schedule(this.id, this.metaData);
        }
    }

    @Override
    public int hashCode() {
        return this.id.hashCode();
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Timer)) return false;
        return this.id.equals(((Timer) object).getId());
    }

    @Override
    public String toString() {
        return this.id.toString();
    }
}