在java中使servlet线程安全

时间:2015-09-04 07:25:24

标签: java multithreading servlets

我的工作代码如下

public class receive_meter_to_store extends HttpServlet {

    WSEMAMSTS EMAMService = new WSEMAMSTS();
    ItronEMAMStsBinding itronEMAM = EMAMService.getItronEMAMStsBinding();


    ItronAuthCredit lItronAuthCredit = new ItronAuthCredit();


    EANDeviceID lTerminalID = new EANDeviceID();


    EANDeviceID lClientID = new EANDeviceID();


    SimpleDateFormat itronDF = new SimpleDateFormat("yyyyMMddHHmmss");
    Date current_datetime = new Date();
    String s_current_datetime = itronDF.format(current_datetime);

    MsgID lMsgID = new MsgID();
    reuse_func gc_reuse_func = new reuse_func();
    curr_time gs_current_datetime = new curr_time("");

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String retdata = "Failure";

        try {
            retdata = add_meter_to_store(request, response);
        }
        finally {
            out.println(retdata);
            out.close();
        }
    } 

我想让它的线程安全,就像让它运行得更快一样。首先,我要删除所有全局变量,但是当我这样做时,我会收到错误

“发生了未处理的程序错误。请与支持服务部​​门联系并报告问题”

我已移动它们以便它们可以如下所示

public class receive_meter_to_store extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String retdata = "Failure";

        reuse_func lc_reuse_func = new reuse_func();

        try {

            WSECMPublic EMAMService = lc_reuse_func.getMeterWebService();

            ItronEMAMStsBinding itronEMAM = EMAMService.getItronEMAMStsBinding();
        }
        catch (Exception ex)
        {

            String ErrorMsg = ex.getMessage();

           out.println("Error" + ErrorMsg);
        }
        finally {
            out.close();
        }

        try {
            retdata = add_meter_to_store(request, response);
        }
        finally {
            out.println(retdata);
            out.close();
        }
    } 

我在这里做错了吗?

我正在调用add_meter

的类
public String add_meter_to_store(HttpServletRequest request, HttpServletResponse response)
    { 
        reuse_func lc_reuse_func = new reuse_func();

        try
        {

            WSECMPublic EMAMService = lc_reuse_func.getMeterWebService();

            ItronEMAMStsBinding itronEMAM = EMAMService.getItronEMAMStsBinding();


            ItronAuthCredit lItronAuthCredit = new ItronAuthCredit();

            EANDeviceID lTerminalID = new EANDeviceID();


            EANDeviceID lClientID = new EANDeviceID();


            SimpleDateFormat itronDF = new SimpleDateFormat("yyyyMMddHHmmss");
            Date current_datetime = new Date();
            String s_current_datetime = itronDF.format(current_datetime);


            MsgID lMsgID = new MsgID();

            curr_time ls_current_datetime = new curr_time("");

            // Declare MeterImportResponse Variable
            ItronMeterStsImportResp stsImportResp = new ItronMeterStsImportResp();

            // Call meterStsImport WebMethod
            stsImportResp = itronEMAM.meterStsImport(stsImportReq);



        }
        catch (Exception ex) {
            // TODO handle custom exceptions here
            String ErrorMsg = ex.getMessage();
            retdata = "Error : " + ErrorMsg;
        }

        return retdata;
    }

注意:我已经删除了第一部分中的全局变量并将它们放在类

1 个答案:

答案 0 :(得分:2)

问题(或至少一个问题)是,在第一个finally区块中,您关闭了out,但之后又尝试再次使用它。

这意味着您的out.println(retdata)语句始终在封闭的流上运行。