java中的doget,从Java类调用doGet Servlet方法

I have a scheduled job class, implements the Quartz Job class, from which I want to invoke a Servlet class that updates a flag in a DB table and then it sends emails to an appropriate emailing list.

I get a java.net.ConnectException. Though the servlet is properly invoked by either entering its URL in the browser or by JavaScript in a JSP page.

My Java class is the following:

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();

}

}

}

My servlet code is:

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);

}

}

The exception I get is:

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.(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)

I am using Tomcat 5.5.

I have seen lots of other relative posts but did not help, I also tried with HttpClient but the same exception.

Could anyone figure out what the problem is?

The servlet configuration on the web.xml is:

emailExpireServlet

emailExpireServlet

...

emailExpireServlet

/emailExpireServlet

解决方案

You should refactor your code to move this functionality out of the servlet and into a common class that can be reached both from your servlet and from your scheduled job.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值