代码之家  ›  专栏  ›  技术社区  ›  Nick Fortescue

在JavaV3彭博API中启动bbcomm

  •  6
  • Nick Fortescue  · 技术社区  · 16 年前

    我仍在等待帮助。。。

    4 回复  |  直到 16 年前
        1
  •  4
  •   Nick Fortescue    16 年前

    与技术支持人员交谈后,发现在64位Windows上,在64位JVM bbcomm下运行不会自动启动。32位Java下不会发生这种情况-32位bbcomm下会自动运行。

    要检查具体情况,请执行以下操作:

    • os.arch )
    • 如果在64位JVM(系统属性)下运行 java.vm.name )
    • 然后尝试启动一个会话
    • 如果失败,那么假设 bbcomm.exe 他没有跑步。试着跑 使用 Runtime.exec()

    我还没有测试上面的内容。它可能与彭博社64位虚拟机的问题完全相同。

        2
  •  3
  •   assylias    14 年前

    • 在注册表中添加条目以在启动时自动启动bbcomm:in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run 添加一个名为 bbcomm C:\blp\API\bbcomm.exe -但这会打开一个命令窗口,该窗口保持可见,因此实际上不是一个选项(如果关闭该窗口,则会终止bbcomm进程)
    • START /MIN C:\blp\API\bbcomm.exe 并将注册表中的条目替换为(未测试)以静默调用bbcomm
    private final static Logger logger = LoggerFactory.getLogger(BloombergUtils.class);
    private final static String BBCOMM_PROCESS  = "bbcomm.exe";
    private final static String BBCOMM_FOLDER  = "C:/blp/API";
    
    /**
     * 
     * @return true if the bbcomm process is running
     */
    public static boolean isBloombergProcessRunning() {
        return ShellUtils.isProcessRunning(BBCOMM_PROCESS);
    }
    
    /**
     * Starts the bbcomm process, which is required to connect to the Bloomberg data feed
     * @return true if bbcomm was started successfully, false otherwise
     */
    public static boolean startBloombergProcessIfNecessary() {
        if (isBloombergProcessRunning()) {
            logger.info(BBCOMM_PROCESS + " is started");
            return true;
        }
    
        Callable<Boolean> startBloombergProcess = getStartingCallable();
        return getResultWithTimeout(startBloombergProcess, 1, TimeUnit.SECONDS);
    }
    
    private static Callable<Boolean> getStartingCallable() {
        return new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                logger.info("Starting " + BBCOMM_PROCESS + " manually");
                ProcessBuilder pb = new ProcessBuilder(BBCOMM_PROCESS);
                pb.directory(new File(BBCOMM_FOLDER));
                pb.redirectErrorStream(true);
                Process p = pb.start();
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.toLowerCase().contains("started")) {
                        logger.info(BBCOMM_PROCESS + " is started");
                        return true;
                    }
                }
                return false;
            }
        };
    
    }
    
    private static boolean getResultWithTimeout(Callable<Boolean> startBloombergProcess, int timeout, TimeUnit timeUnit) {
        ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
    
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r, "Bloomberg - bbcomm starter thread");
                t.setDaemon(true);
                return t;
            }
        });
        Future<Boolean> future = executor.submit(startBloombergProcess);
    
        try {
            return future.get(timeout, timeUnit);
        } catch (InterruptedException ignore) {
            Thread.currentThread().interrupt();
            return false;
        } catch (ExecutionException | TimeoutException e) {
            logger.error("Could not start bbcomm", e);
            return false;
        } finally {
            executor.shutdownNow();
            try {
                if (!executor.awaitTermination(100, TimeUnit.MILLISECONDS)) {
                    logger.warn("bbcomm starter thread still running");
                }
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    }
    

    ShellUtils.java

    public class ShellUtils {
    
        private final static Logger logger = LoggerFactory.getLogger(ShellUtils.class);
    
        /**
         * @return a list of processes currently running
         * @throws RuntimeException if the request sent to the OS to get the list of running processes fails
         */
        public static List<String> getRunningProcesses() {
            List<String> processes = new ArrayList<>();
    
            try {
                Process p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe");
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                int i = 0;
                while ((line = input.readLine()) != null) {
                    if (!line.isEmpty()) {
                        String process = line.split(" ")[0];
                        if (process.contains("exe")) {
                            processes.add(process);
                        }
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException("Could not retrieve the list of running processes from the OS");
            }
    
            return processes;
        }
    
        /**
         * 
         * @param processName the name of the process, for example "explorer.exe"
         * @return true if the process is currently running
         * @throws RuntimeException if the request sent to the OS to get the list of running processes fails
         */
        public static boolean isProcessRunning(String processName) {
            List<String> processes = getRunningProcesses();
            return processes.contains(processName);
        }
    }
    
        3
  •  0
  •   Art    7 年前

    我知道这是一篇老文章,但我想我会添加我的解决方案作为参考,以防有人需要帮助检查/启动

    这段代码是用C语言编写的,但我希望您可以轻松地将其翻译成Java。

    void Main()
    {
        var processes = Process.GetProcessesByName("bbcomm");
        if (processes.Any())
        {
            Console.WriteLine(processes.First().ProcessName + " already running");
            return;
        }
    
        var exePath = @"C:\blp\DAPI\bbcomm.exe";
        var processStart = new ProcessStartInfo(exePath);
        processStart.UseShellExecute = false;
        processStart.CreateNoWindow = true;
        processStart.RedirectStandardError = true;
        processStart.RedirectStandardOutput = true;
        processStart.RedirectStandardInput = true;  
        var process = Process.Start(processStart);
        Console.WriteLine(process.ProcessName + " started");
    }
    
        4
  •  -2
  •   G Sousa    16 年前

    推荐文章