springboot starts with a screen that resolves multithreaded [restarted Main]

Keywords: Java Selenium Google SpringBoot

Because the springboot project needs to add the function of startup waiting, that is, when the project is loaded and started, it needs to pop up a customized screen on the desktop, under which there is a progress bar.
1. Add file JWindows.java in the same place as Application

import com.CIDataCompare.application.controller.LoginController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.net.*;

//Program Startup Interface

public  class JWindowDemo extends JWindow implements Runnable {
  Thread splashThread;  //Progress bar update thread
  JProgressBar progress; //Progress bar
  public volatile boolean exit = false;
  private Logger logger= LoggerFactory.getLogger(this.getClass());
  public JWindowDemo() {
    Container container=getContentPane(); //Get container
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));  //Set cursor
    String url1=getClass().getResource("").getPath();
    logger.info("=================="+url1);
    URL url = getClass().getResource("login.png"); //Picture location
    if(url != null){
      container.add(new JLabel(new ImageIcon(url)),BorderLayout.CENTER);  //Add pictures
    }
    progress = new JProgressBar(1,500); //Instance progress bar
    progress.setStringPainted(true); //Descriptive text
    progress.setString("Loading.......");  //Setting Display Text
    progress.setBackground(Color.white);  //Setting Background Colors
    container.add(progress,BorderLayout.SOUTH);  //Add progress bar to container

    Dimension screen = getToolkit().getScreenSize();  //Get screen size
    pack(); //Window Adaptation Component Size
    setLocation((screen.width-getSize().width)/2,(screen.height-getSize().height)/2); //Setting window position
    splashThread=new Thread(this);  //Instantiated threads
    splashThread.start();  //Start running threads
  }

  public void start(){
    this.toFront();  //Front-end window display
    /*splashThread=new Thread(this);  //Instantiated threads
    splashThread.start();  //Start running threads*/
  }

  public void run(){
    setVisible(true); //Display window
    try {
      for (int i=0;i<1000;i++){
        if(splashThread.interrupted()){
          break;
        }else{
          splashThread.sleep(150); //Thread dormancy
          if(Application.num==100){
            progress.setValue(500); //Setting progress bar values
            splashThread.sleep(5000); //Thread dormancy
            dispose(); //release window
            exit=true;
            splashThread.interrupt();
            System.out.println("====111===="+Thread.currentThread().getName());
            Thread.currentThread().interrupt();
            break;
          }else{
              progress.setValue(progress.getValue()+1); //Setting progress bar values
              logger.info(progress.getValue()+1+"");
        }
        }
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    dispose(); //release window
    splashThread.interrupted();
  }

2. Add startup interface in Application.java, add main thread startup flag num=100

package com.CIDataCompare.application;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import javax.swing.*;
import java.awt.*;
import java.io.File;

@SpringBootApplication
public class Application {
   public static int num=0;
   public static void main(String[] args) {
      JWindowDemo splash = new JWindowDemo();
      //splash.start(); // Run Start Interface
      //SpringApplication.run(Application.class, args);
      SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
      builder.headless(false).web(true).run(args);
      try{
         File directory = new File("");
         String mydir = directory.getAbsolutePath();

         //Open with Google browser
         System.setProperty("webdriver.chrome.driver", mydir+"\\chromedriver.exe");
         ChromeOptions options=new ChromeOptions();
         options.addArguments("disable-infobars");
         //Instantiate the object of webdriver and start Google browser.
         WebDriver driver = new ChromeDriver(options);
         //Open a Web page by calling a specific get method from the object driver
         driver.get("http://localhost:60032/");
      }catch (Exception e){
         //Open with default browser
         try {
            //String url = "http://www.baidu.com";
            String url = "http://localhost:60032";
            java.net.URI uri = java.net.URI.create(url);
            // Get the current system desktop extension
            Desktop dp =Desktop.getDesktop();
            // Determine whether the system desktop supports the functionality to be performed
            if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
               //File file = new File("D:\\aa.txt");
               //dp.edit(file); // edit file
               dp.browse(uri);// Get the system default browser to open the link
               // dp.open(file); // Open the file by default
               // dp.print(file); / / / Print a document with a printer
            }
         } catch (java.lang.NullPointerException e1) {
            // This is an exception thrown when uri is space-time
            e.printStackTrace();
         } catch (java.io.IOException e2) {
            // This is unable to get the default browser for the system
            e.printStackTrace();
         }

      }
      System.out.println("===========Application=========================");
      num=100;

   }
}

3. Put the URL url = getClass().getResource("login.png"); and the image to be loaded in the location of // image into

  String url1=getClass().getResource("").getPath();
    logger.info("=================="+url1);

Under the printed address, that is, under the target classes com CIDataCompare application after the project is packaged


4. Start the project

5. A problem that arises after startup, when for loop, there are always two threads in the loop. When the main thread starts, the threads that want to exit the startup screen can not go out and start the printed log.

2019-08-12 13:27:55.979 [main] INFO  com.CIDataCompare.application.JWindowDemo - ==================/E:/workspace/CIDVS/target/classes/com/CIDataCompare/application/
2019-08-12 13:27:56.211 [restartedMain] INFO  com.CIDataCompare.application.JWindowDemo - ==================/E:/workspace/CIDVS/target/classes/com/CIDataCompare/application/

2019-08-12 13:27:56.728 [Thread-4] INFO  com.CIDataCompare.application.JWindowDemo - 6
2019-08-12 13:27:56.866 [Thread-8] INFO  com.CIDataCompare.application.JWindowDemo - 6
2019-08-12 13:27:56.881 [Thread-4] INFO  com.CIDataCompare.application.JWindowDemo - 7
2019-08-12 13:27:57.028 [Thread-8] INFO  com.CIDataCompare.application.JWindowDemo - 7

Later, it turned out that the dependency on hot deployment in pom.xml caused this problem.

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
</dependency>

Just leave this comment out.

Posted by JC99 on Sat, 05 Oct 2019 20:43:59 -0700