开发者

Close resources before exiting JFrame and TCP communication in Java

开发者 https://www.devze.com 2023-02-25 20:43 出处:网络
1. I\'m writing a chat based application on TCP communication. I\'m using NetBeans and I want to add functionality to the default EXIT_ON_CLOSE when exiting JFrame.

1. I'm writing a chat based application on TCP communication. I'm using NetBeans and I want to add functionality to the default EXIT_ON_CLOSE when exiting JFrame. The reason of course is because I want to clean resources and end threads safely. How can I call a method tha开发者_如何学Got clear resources and only then close the JFrame safely and end the process.

2. I need to implement the server side. The server has List/HashMap/Queue of 'Socket' with their chat nick-names. Is there any simple design pattern to do it correctly because I don't want to re-invent the wheel.

thanks.


After attempting Andrew's suggestion in trying to solve this problem myself, here's what I did. In my case, I'm saving data to a database, while you're closing tcp communication. The idea remains the same: we have work to do between when we close the window and when the program actually closes.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowListener() {            
    @Override
    public void windowOpened(WindowEvent e) {
        // TODO Load all data here                
    }
    @Override public void windowClosing(WindowEvent e) {
        // TODO Save the data
    }

    @Override public void windowIconified(WindowEvent e) {}            
    @Override public void windowDeiconified(WindowEvent e) {}            
    @Override public void windowDeactivated(WindowEvent e) {}            
    @Override public void windowActivated(WindowEvent e) {}
    @Override public void windowClosed(WindowEvent e) {}
});


You can use the WindowAdapter class to save implementing all the methods of the WindowListener interface (it includes an empty implementation of each method so you can override the ones you care about).

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter()
{
    @Override
    public void windowClosing(WindowEvent we)
    {
        //Code goes here
    }
});


For point 1), set the JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);, add a WindowListener to the frame. In the close method, add the functionality to end the Threads & clear the resources.

It might also pay to implement Runtime.addShutdownHook(Thread). Note that a shutdown hook should complete quickly.


For point 2: When I wrote a similar program I created a class called "Connection" which included as members a socket and a string nickname. The class extended the Thread class.

So, whenever a new socket was created it would be inserted into such an object which would then run as a separate thread, listening for new messages. These objects were inserted to into a list whenever one was created and then deleted as appropriate when the user signed off.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号