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

com.payneteasy.superfly.client.SessionToucherImpl Maven / Gradle / Ivy

There is a newer version: 1.7-32
Show newest version
package com.payneteasy.superfly.client;

import com.payneteasy.superfly.api.SSOService;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Default session toucher implementation. Accumulates IDs of sessions
 * and sends them in bulks every minute (by default).
 *
 * @author Roman Puchkovskiy
 */
public class SessionToucherImpl implements SessionToucher {
    private boolean enabled = true;
    private SSOService ssoService;
    private int flushPeriodInSeconds;

    private final Queue queue = new ConcurrentLinkedQueue();
    private Timer timer;

    public SessionToucherImpl() {
        this(60); // every minute
    }

    public SessionToucherImpl(int flushPeriodInSeconds) {
        this.flushPeriodInSeconds = flushPeriodInSeconds;
    }

    public void setSsoService(SSOService ssoService) {
        this.ssoService = ssoService;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @PostConstruct
    public void startup() {
        if (ssoService == null) {
            throw new IllegalStateException("ssoService not set");
        }
        if (enabled) {
            timer = new Timer("session-toucher-timer");
            timer.scheduleAtFixedRate(new ToucherTask(),
                    getFlushPeriodInMillis(), getFlushPeriodInMillis());
        }
    }

    private long getFlushPeriodInMillis() {
        return flushPeriodInSeconds * 1000;
    }

    @PreDestroy
    public void shutdown() {
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public void addSessionId(long sessionId) {
        if (enabled) {
            queue.add(sessionId);
        }
    }

    private class ToucherTask extends TimerTask {
        @Override
        public void run() {
            Set sessionIds = new HashSet();
            Long sessionId;
            do {
                sessionId = queue.poll();
                if (sessionId != null) {
                    sessionIds.add(sessionId);
                }
            } while (sessionId != null);
            if (!sessionIds.isEmpty()) {
                ssoService.touchSessions(new ArrayList(sessionIds));
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy