繁体   English   中英

从 java 类调用 doGet Servlet 方法

[英]Invoke the doGet Servlet method from java class

我有一个预定的作业类,实现了 Quartz 作业类,我想从中调用一个 Servlet 类,该类更新数据库表中的标志,然后将电子邮件发送到适当的电子邮件列表。

我得到一个java.net.ConnectException 尽管通过在浏览器中输入其 URL 或在 JSP 页面中通过 JavaScript 正确调用 servlet。

我的 Java 类如下:

public class ExpirationJob implements Job {
    org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ExpirationJob.class");

    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            URL serv = new URL("http://localhost:8080/app/emailExpirationServlet");
            URLConnection sr = serv.openConnection();
            sr.connect();
            InputStream is = sr.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader in = new BufferedReader(isr);

            String inputLine;
            int i =0;
            while ((inputLine = in.readLine()) != null)
                i = i +1;
            log.debug("Input line: " + inputLine);
            in.close();
        } catch (MalformedURLException e) {
            log.debug("Error while calling the emailExpirationServlet -- MalformedURLException: "+ e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.debug("Error while calling the emailExpirationServlet -- IOException: "+ e.getMessage());
            e.printStackTrace();
        }
    }
}

我的 servlet 代码是:

public class emailExpireServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
    org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("emailExpireServlet.class");

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        log.debug("Initializing emailExpireServlet");
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        log.debug("Starting emailExpireServlet");

        //do some business logic here - I have commented it out to rule out any other blocking issue
        response.setStatus(200);
        response.getOutputStream().print("Invoked emailExpireServlet! Status is OK");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

我得到的例外是:

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:525)
    at java.net.Socket.connect(Socket.java:475)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
    at schedulers.ExpirationJob.execute(Unknown Source)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)

我正在使用 Tomcat 5.5。

我看过很多其他相关帖子但没有帮助,我也尝试过HttpClient但同样的例外。

谁能弄清楚问题是什么?

web.xml上的 servlet 配置是:

<servlet>
    <servlet-name>emailExpireServlet</servlet-name>
    <servlet-class>emailExpireServlet</servlet-class>
</servlet>
...
<servlet-mapping>
    <servlet-name>emailExpireServlet</servlet-name>
    <url-pattern>/emailExpireServlet</url-pattern>
</servlet-mapping>

您应该重构您的代码,将这个功能从 servlet 移到一个公共类中,该类可以从您的 servlet 和预定的作业中访问。

URLConnection打开指向 URL 引用的资源的通信链接(如果尚未建立此类连接) URL.openConnection()返回一个URLConnection对象,该对象表示到 URL 引用的远程对象的连接。 通过调用此 URL 的协议处理程序的openConnection方法,每次都会打开一个新连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM