栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中限制一个应用程序的多个实例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中限制一个应用程序的多个实例

编辑:我尝试使用Win200864b(版本不重要)和Jframe并通过Jframe在SystemTray中移动到Front()或图标化。DO_NOTHING_ON_CLOSE

    public interface ApplicationStartedListener {        void applicationStarted();        void foreignApplicationStarted(String name);        void messageArrived(Object obj);    }//    import java.io.Serializable;    public class ClassCheck implements Serializable {        private static final long serialVersionUID = 1L;        private String className = null;        public ClassCheck() {        }        public ClassCheck(String className) { setClassName(className);        }        @Override        public String toString() { return this.className;        }        public String getClassName() { return this.className;        }        public void setClassName(String className) { this.className = className;        }    }//    import java.awt.AWTException;    import java.awt.BorderLayout;    import java.awt.frame;    import java.awt.MouseInfo;    import java.awt.Point;    import java.awt.Robot;    import java.awt.event.InputEvent;    import java.io.File;    import javax.swing.Jframe;    import javax.swing.JTextField;    import javax.swing.SwingUtilities;    public class RunonceFromFile {        private SingleInstanceController sic = null;        private Jframe frame;        private Robot r;        private JTextField tf;        public RunonceFromFile() { try {     r = new Robot(); } catch (AWTException ex) {     ex.printStackTrace(); } sic = new SingleInstanceController(new File(System.getProperty("java.io.tmpdir") + "Example.file"), "sic_example_application"); if (sic.isOtherInstanceRunning()) {     sic.sendMessageToRunningApplication("toFront");     System.exit(0); } else {     frame = new Jframe("TEST");     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.setSize(400, 300);     tf = new JTextField("JTextFiled");     frame.add(tf, BorderLayout.NORTH);     frame.setExtendedState(frame.ICONIFIED);     frame.setExtendedState(frame.NORMAL);     frame.setExtendedState(frame.getExtendedState() | Jframe.ICONIFIED);     frame.setExtendedState(frame.getExtendedState() & (~Jframe.ICONIFIED));     frame.setLocationRelativeTo(null);     frame.setVisible(true);     sic.registerApplication();     sic.addApplicationStartedListener(new ApplicationStartedListener() {         public void applicationStarted() {  Runnable doRun = new Runnable() {      public void run() {          frame.toFront();      }  };  SwingUtilities.invokeLater(doRun);         }         public void foreignApplicationStarted(final String name) {  Runnable doRun = new Runnable() {      public void run() {          frame.toFront();      }  };  SwingUtilities.invokeLater(doRun);         }         public void messageArrived(final Object obj) {  Runnable doRun = new Runnable() {//activateWindow(frame);      public void run() {          frame.toFront();      }  };  SwingUtilities.invokeLater(doRun);         }         private void activateWindow(Jframe frame) {  frame.setExtendedState(frame.ICONIFIED);  frame.setExtendedState(frame.NORMAL);  frame.setAlwaysonTop(true);  frame.setAlwaysonTop(false);  Point location = MouseInfo.getPointerInfo().getLocation();  Point locationOnScreen = frame.getLocationOnScreen();  r.mouseMove(locationOnScreen.x + 100, locationOnScreen.y + 10);  r.mousePress(InputEvent.BUTTON1_MASK);  r.mouseRelease(InputEvent.BUTTON1_MASK);  r.mouseMove(location.x, location.y);         }     }); }        }        public static void main(String[] args) { RunonceFromFile roff = new RunonceFromFile();        }    }//    import java.io.BufferedReader;    import java.io.BufferedWriter;    import java.io.File;    import java.io.FileReader;    import java.io.FileWriter;    import java.io.IOException;    import java.io.ObjectInputStream;    import java.io.ObjectOutputStream;    import java.net.ServerSocket;    import java.net.Socket;    import java.util.ArrayList;    public class SingleInstanceController {        private String appname = null;        private Socket client = null;        private File file = null;        private ArrayList<ApplicationStartedListener> listener = null;        private ObjectInputStream ois = null;        private ObjectOutputStream oos = null;        private boolean result = false;        private ServerSocket server = null;        public SingleInstanceController(String appname) { this(new File(System.getProperty("java.io.tmpdir") + "/923jhakE53Kk9235b43.6m7"), appname);        }        public SingleInstanceController(File file, String appname) { this.file = file; this.appname = appname; this.listener = new ArrayList<ApplicationStartedListener>();        }        public void addApplicationStartedListener(ApplicationStartedListener asl) { this.listener.add(asl);        }        public void removeApplicationStartedListener(ApplicationStartedListener asl) { this.listener.remove(asl);        }        public boolean isOtherInstanceRunning() { if (!this.file.exists()) {     return false; } return sendMessageToRunningApplication(new ClassCheck(this.appname));        }        public boolean sendMessageToRunningApplication(final Object obj) { this.result = false; try {     this.client = new Socket("localhost", getPortNumber());     new Thread(new Runnable() {         public void run() {  try {      SingleInstanceController.this.oos = new ObjectOutputStream(SingleInstanceController.this.client.getOutputStream());      SingleInstanceController.this.ois = new ObjectInputStream(SingleInstanceController.this.client.getInputStream());      SingleInstanceController.this.oos.writeObject(obj);      SingleInstanceController.this.oos.flush();      SingleInstanceController.this.result = SingleInstanceController.this.ois.readBoolean();  } catch (IOException e) {      SingleInstanceController.this.result = false;  }         }     }).start();     for (int i = 0; i < 10; i++) {         if (this.result == true) {  break;         }         try {  Thread.sleep(500);         } catch (InterruptedException e) {  e.printStackTrace();         }     }     this.client.close();     return this.result; } catch (IOException e) {     return false; }        }        public boolean registerApplication() { try {     if (!this.file.exists()) {         if (!this.file.getParentFile().mkdirs() && !this.file.getParentFile().exists()) {  return false;         }         if (!this.file.createNewFile()) {  return false;         }     }     BufferedWriter wuffy = new BufferedWriter(new FileWriter(this.file));     int port = getFreeServerSocket();     if (port != -1) {         startServer();     }     wuffy.write(String.valueOf(port));     wuffy.close();     return true; } catch (IOException e) {     return false; }        }        protected void messageArrived(Object obj) { for (ApplicationStartedListener asl : this.listener) {     asl.messageArrived(obj); }        }        protected void applicationStartet() { for (ApplicationStartedListener asl : this.listener) {     asl.applicationStarted(); }        }        protected void foreignApplicationStarted(String name) { for (ApplicationStartedListener asl : this.listener) {     asl.foreignApplicationStarted(name); }        }        private int getPortNumber() { try {     BufferedReader buffy = new BufferedReader(new FileReader(this.file));     int port = Integer.parseInt(buffy.readLine().trim());     buffy.close();     return port; } catch (Exception e) {     return -1; }        }        private void startServer() { new Thread(new Runnable() {     public void run() {         while (true) {  try {      SingleInstanceController.this.client = SingleInstanceController.this.server.accept();      if (SingleInstanceController.this.client.getInetAddress().isLoopbackAddress()) {          new Thread(new Runnable() {   public void run() {       try {SingleInstanceController.this.oos = new ObjectOutputStream(SingleInstanceController.this.client.getOutputStream());SingleInstanceController.this.ois = new ObjectInputStream(SingleInstanceController.this.client.getInputStream());Object obj = SingleInstanceController.this.ois.readObject();if (obj instanceof ClassCheck) {    if (obj.toString().equals(SingleInstanceController.this.appname)) {        SingleInstanceController.this.oos.writeBoolean(true);        applicationStartet();    } else {        SingleInstanceController.this.oos.writeBoolean(false);        foreignApplicationStarted(obj.toString());    }} else {    messageArrived(obj);    SingleInstanceController.this.oos.writeBoolean(true);}SingleInstanceController.this.oos.flush();SingleInstanceController.this.client.close();       } catch (IOException e) {e.printStackTrace();       } catch (ClassNotFoundException e) {e.printStackTrace();       }   }          }).start();      }  } catch (IOException e) {      e.printStackTrace();  }         }     } }).start();        }        private int getFreeServerSocket() { for (int i = 2000; i < 10000; i++) {     try {         this.server = new ServerSocket(i);         return i;     } catch (IOException ignore) {     } } return -1;        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569389.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号