简体   繁体   中英

How to set the session timeout in a standalone Swing application?

I have a stand alone swing application. In that I want to invalidate the session as how it does in web applications.

How to set the session timeout in a standalone Swing application?

If in standalone swing application you have no server communication, you have only one criteria to check whether the session valid: user provides mouse or key events in your application. Here is my example, how to use this criteria:

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public class SessionHandlerExample implements ActionListener {

    private static final int SESSION_TIMEOUT = 30 * 1000; // 30 sec timeout for testing purposes
    private final Timer invalidationTimer = new Timer(SESSION_TIMEOUT, this);
    private final JFrame frm = new JFrame("Session test frame");
    /**
     * 
     */
    public SessionHandlerExample() {
        initUI();
    }

    /**
     * 
     */
    private void initUI() {
        final JTextArea text = new JTextArea(20, 80);
        frm.add(new JScrollPane(text));
        frm.pack();
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setVisible(true);
        invalidationTimer.setRepeats(false);
        invalidationTimer.restart();

        // register listener to get all mouse/key events
        final AWTEventListener l = new AWTEventListener() {

            @Override
            public void eventDispatched(AWTEvent event) {
                // if any input event invoked - restart the timer to prolong the session
                invalidationTimer.restart();
            }
        };
        Toolkit.getDefaultToolkit().addAWTEventListener(l, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        // provide session invalidation here (show login dialog or do something else)
        JOptionPane.showMessageDialog(frm, "Your session is invalide");
        invalidationTimer.restart();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SessionHandlerExample();
            }
        });
    }
}

If you have server interaction in your application, you can bind the invalidation timer to your server interaction framework (restart timer when any request is sent to server).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM