有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Tomcat管理后端进程?

我有一个网页,用户按下按钮并在一个适当的应用程序(驻留在Tomcat中)中启动一个操作

该操作是一个长时间运行的过程,我必须查看发生了什么的唯一方法是登录到服务器并查看日志文件

我编写了一个快速Java函数,用于读取日志文件并对发生的情况提供反馈。(基本上,它只是跟踪文件并解析出我需要的东西)

我希望能够添加一个jsp,以便在不登录服务器的情况下查看输出

===

从设计的角度来看,我理解JSP应该快速返回结果,而不仅仅是继续处理

因此,我的想法是创建一个简单的web页面,查询jsp以获取更新,并将最新信息写入屏幕。等待30秒,再次轮询服务器,并附加最新更新

我正在努力掌握的是如何让JSP与后端进程通信,以及如何生成/终止后端进程

这是一个非常偶然的事情(每两周一次,从开始到完成需要一两个小时),所以我不希望守护进程一直运行。我希望能够暂时打开和关闭它

如果我从一个简单的servlet中生成一个进程,那么当我完成时,如何结束该进程? 我如何与它沟通


共 (2) 个答案

  1. # 1 楼答案

    您应该考虑使用诸如{a1}这样的控件来控制后台进程。

    为了进行控制,前端代码可以发送启动/停止/检查流程的消息
    为了进行监控,您的流程可以发布到JMS主题,以便前端代码阅读

  2. # 2 楼答案

    您可以创建一个java.lan.Runnable开关,读取文件内容并将其保存到缓冲区中。Runnable使用while循环读取文件内容,因为中断条件可以从外部设置,执行Runnable的线程将在Runnable的run方法终止时终止

    在JSP中,您可以创建一个java.lang.Thread,并将Runnable的实例传递给它。将runable的安装保存在ServletContext中,以便您可以跨请求访问它。如果您想终止轮询,而不仅仅是从JSP设置Runnable的中断条件,rum方法将终止,因此线程也将终止

    您可以使用javascript setInterval()函数和XMLHttpRequest刷新页面

    下面是一个基本实现示例(我希望它能满足您的要求):

    轮询可运行

    package com.web;
    
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class FilePollingThread implements Runnable {
    
        private String filepath = null;
        private boolean polling = false;
        private StringBuffer dataWritenAfterLastPoll = null;
        private String error = null;
    
        public FilePollingThread(String filepath) {
            this.filepath = filepath;
        }
    
        @Override
        public void run() {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
                dataWritenAfterLastPoll = new StringBuffer();
                polling = true;
    
                String line = null;
    
                while(polling) {
                    try {
                        line = br.readLine();
                        while(line == null) {
                            try {
                                Thread.sleep(500L);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                error = e.toString();
                            }
                            line = br.readLine();
                        }
                        dataWritenAfterLastPoll.append(markUp(line));
                    } catch (IOException e) {
                        e.printStackTrace();
                        error = e.toString();
                    }
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                error = e.toString();
            } finally {
                if(br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        error = e.toString();
                    }
                }
            }
        }
    
        private String markUp(String line) {
            String markup = "";
            if(line != null) {
                markup = "<div style=\"height: 6px\"><span style=\"line-height: 1.1;\">" + line + "</span></div>\n";
            }
            return markup;
        }
    
        public synchronized void stopPolling() {
            polling = false;
        }
    
        public synchronized String poll() {
            String tmp = markUp(error == null ? "Not ready" : error);
            if(dataWritenAfterLastPoll != null) {
                tmp = dataWritenAfterLastPoll.toString();
                dataWritenAfterLastPoll = new StringBuffer();
            }
            return tmp;
        }
    }
    

    JSP开关启动轮询并继续检索数据

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ page import="com.web.FilePollingThread" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Poll file</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <link rel="stylesheet" type="text/css" href="style/default.css"></link>
    <script type="text/javascript">
        var c = 1;
        var ih;
        var polling = false;
        var filepath = null;
        function startPolling(interval) {
            ih = setInterval(function () {
                try {
                    var xmlHttp = new XMLHttpRequest();
                    xmlHttp.onreadystatechange = function () {
                        if(xmlHttp.readyState == 4) {
                            if(xmlHttp.status == 200) {
                                var w = getElementById('ajax_content');
                                w.innerHTML = w.innerHTML + xmlHttp.responseText;
                                getElementById('page_refresh').innerHTML = c++;
                                polling = true;
                                window.scrollTo(0, document.body.scrollHeight);
                            } else {
                                polling = false;
                                throw 'HTTP ' + xmlHttp.status;
                            }
                        }
                    };
                    xmlHttp.open('GET', 'pollfile.jsp?filepath=' + filepath + '&c=' + c, true);
                    xmlHttp.send();
                } catch(e) {
                    alert('Error at startPolling: ' + e);
                    clearInterval(ih);
                }
            }, interval);
        }
    
        function startStopPolling() {
            var orgPolling = polling;
            try {
                if(polling) {
                    polling = false;
                    clearInterval(ih);
                    doPolling();
                } else {
                    polling = true;
                    doPolling();
                    startPolling(1000);
                }
                flipStartStopButtonsLabel();
            } catch(e) {
                polling = orgPolling;
                flipStartStopButtonsLabel();
                alert('Error at startStopPolling: ' + e);
            }
        }
    
        function flipStartStopButtonsLabel() {
            var label;
            if(polling) {
                c = 1;
                label = 'Stop polling';
                getElementById('page_refresh').innerHTML = '0';
            } else {
                label = 'Sart polling';
                getElementById('page_refresh').innerHTML = 'stoped';
            }
            var buttons = document.getElementsByName('start_stop_polling');
            if(buttons) {
                for(var i = 0; i < buttons.length; i++) {
                    buttons[i].value = label;
                }
            }
        } 
    
        function doPolling() {
            var url = 'pollfile.jsp?polling=';
            if(polling) {
                filepath = getElementById('filepath');
                if(filepath && filepath.value && filepath.value.length > 0) {
                    url += 'true&filepath=' + encodeURIComponent(filepath.value);
                } else {
                    throw 'No filepath specified.';
                }
            } else {
                url += 'false';
            }
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.onreadystatechange = function () {
                if(xmlHttp.readyState == 4) {
                    if(xmlHttp.status != 200) {
                        throw 'HTTP ' + xmlHttp.status;
                    }
                }
            };
            xmlHttp.open('POST', url, false);
            xmlHttp.send();
        }
    
        function clearWindow() {
            var w = getElementById('ajax_content');
            if(w) {
                w.innerHTML = '';
            }
        }
    
        function getElementById(id) {
            try {
                if(id) {
                    elm = document.getElementById(id);
                    return elm;
                } 
            } catch(e) {
                alert('Error at getElementById: ' + e);
            }
            return null;
        }
    </script>
    </head>
    <body>
    
    
    <%
        String polling = request.getParameter("polling");
    
        if("true".equals(polling)) {
            String filepath = request.getParameter("filepath");
            if(filepath != null && filepath.length() > 0) {
                FilePollingThread pollingThread = new FilePollingThread(filepath);
                new Thread(pollingThread, "polling thread for file '" + filepath + "'").start();
                request.getServletContext().setAttribute("pollingThread", pollingThread);
            }
        } else if("false".equals(polling)) {
            FilePollingThread pollingThread = (FilePollingThread) request.getServletContext().getAttribute("pollingThread");
            if(pollingThread != null) {
                pollingThread.stopPolling();
            }
        } else {
            FilePollingThread pollingThread = (FilePollingThread) request.getServletContext().getAttribute("pollingThread");
            if(pollingThread != null) {
                response.getWriter().println(pollingThread.poll());
                response.getWriter().close();
                return;
            }
        }
    %>
    <div class="label">
            <span>Page polling:</span>
        </div>
        <div style="float: left;">
            <span id="page_refresh">0</span>
        </div>
    <div class="clear_both">&nbsp;</div>
    <form id="input_form" action="pollfile.jsp" method="get">
        <div>
            <div style="float: left;">
                <label>Filepath:
                    <input style="height: 24px;" id="filepath" type="text" size="120" value=""/>
                </label>
            </div>
            <div style="clear: both;"/>
            <div style="float: left;">
                <input style="height: 24px;" name="start_stop_polling" id="start_stop_polling_button" type="button" onclick="startStopPolling(); return false;" value="Start polling"/>
            </div>
            <div style="float: left;">
                <input style="height: 24px;" name="clear_window" id="clear_window_button" type="button" onclick="clearWindow(); return false;" value="Clear"/>
            </div>
            <div style="clear: both;">&nbsp;</div>
        </div>
    </form>
    <div id="ajax_content">
    </div>
    <div>
        <div style="float: left;">
            <input style="height: 24px;" name="start_stop_polling" id="start_stop_polling_button" type="button" onclick="startStopPolling(); return false;" value="Start polling"/>
        </div>
        <div style="float: left;">
            <input style="height: 24px;" name="clear_window" id="clear_window_button" type="button" onclick="clearWindow(); return false;" value="Clear"/>
        </div>
        <div style="clear: both;">&nbsp;</div>
    </div>
    </body>
    </html>
    

    编辑

    FilePollingThread中有一个错误:如果文件中没有可用的数据,线程可能会卡在内部while循环中。应该是的

    while(line == null && polling)
    

    此外,JSP将无法在IE上运行(在IE9上测试)。似乎写入行中响应的数据

    response.getWriter().println(pollingThread.poll());
    

    包含洞页面的HTML。如果添加到目标分区,IE似乎无法渲染它

    我用一个简单的静态HTML文件制作了另一个版本,作为一个servlet,因为它提供了对写入响应的更多控制

    如果你对代码感兴趣,请告诉我