Java EJB @Schedule 注释方法被调用了两次
Java EJB @Schedule annotation method is being called twice
我正在做一个 J2EE 项目,简而言之,它会在指定的时间向用户发送一封自动电子邮件,并允许用户从通过电子邮件发送给他们的网页上下载文件。效果很好。
但是,我使用 @Schedule
注释的计时器方法被调用了两次。该方法总是在 运行 时间(我不想要)立即执行,然后在指定时间稍后执行。我包含了部署我的应用程序时加载的 Servlet 的代码、计划 class 和我的 web.xml 文件。
DeployApplicationServlet
class:
package downloadsupport;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import scheduleTimer.ScheduleEmail;
/**
* Servlet implementation class InitializeApplicationServlet
*/
@WebServlet("/DeployApplicationServlet")
public class DeployApplicationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeployApplicationServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Web Application Started");
ScheduleEmail se = new ScheduleEmail();
se.sendAutomatedEmail();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
}
}
ScheduleEmail
class:
package scheduleTimer;
import java.util.Date;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import java.net.*;
import java.io.*;
@Stateless
public class ScheduleEmail {
@Schedule(second = "0", minute = "10", hour = "12", dayOfWeek = "Wed")
public void sendAutomatedEmail() {
// Print Time to console for testing purposes
System.out.println(new Date());
// Invoke the SendEmailServlet at the designated time
try {
URL emailServlet = new
URL("http://localhost:9081/downloadsupport/SendEmailServlet");
URLConnection servletConn = emailServlet.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
servletConn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
web.xml
配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>downloadsupport</display-name>
<servlet>
<servlet-name>SendEmailServlet</servlet-name>
<servlet-class>downloadsupport.SendEmailServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DeployApplicationServlet</servlet-name>
<servlet-class>downloadsupport.DeployApplicationServlet</servlet-class>
</servlet>
<welcome-file-list>
<welcome-file>DeployApplicationServlet</welcome-file>
<!-- <welcome-file>SendEmailServlet</welcome-file> -->
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>DownloadServet</servlet-name>
<servlet-class>downloadsupport.DownloadServlet</servlet-class>
</servlet>
<!--
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/downloadServlet</url-pattern>
</servlet-mapping>
-->
</web-app>
您的第一个不需要的电话是您自己造成的,而不是服务器不工作造成的。
您不需要实例化 ScheduleEmail
。从 servlet 中删除这两行,它将正常工作。
容器负责初始化你的bean并调用标有@Schedule
的方法
检查您的服务器实例。如果您在服务器中有 2 个节点。然后使它成为一个节点,而不是 ejb 计时器将按预期工作。
我正在做一个 J2EE 项目,简而言之,它会在指定的时间向用户发送一封自动电子邮件,并允许用户从通过电子邮件发送给他们的网页上下载文件。效果很好。
但是,我使用 @Schedule
注释的计时器方法被调用了两次。该方法总是在 运行 时间(我不想要)立即执行,然后在指定时间稍后执行。我包含了部署我的应用程序时加载的 Servlet 的代码、计划 class 和我的 web.xml 文件。
DeployApplicationServlet
class:
package downloadsupport;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import scheduleTimer.ScheduleEmail;
/**
* Servlet implementation class InitializeApplicationServlet
*/
@WebServlet("/DeployApplicationServlet")
public class DeployApplicationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeployApplicationServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Web Application Started");
ScheduleEmail se = new ScheduleEmail();
se.sendAutomatedEmail();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
}
}
ScheduleEmail
class:
package scheduleTimer;
import java.util.Date;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import java.net.*;
import java.io.*;
@Stateless
public class ScheduleEmail {
@Schedule(second = "0", minute = "10", hour = "12", dayOfWeek = "Wed")
public void sendAutomatedEmail() {
// Print Time to console for testing purposes
System.out.println(new Date());
// Invoke the SendEmailServlet at the designated time
try {
URL emailServlet = new
URL("http://localhost:9081/downloadsupport/SendEmailServlet");
URLConnection servletConn = emailServlet.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
servletConn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
web.xml
配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>downloadsupport</display-name>
<servlet>
<servlet-name>SendEmailServlet</servlet-name>
<servlet-class>downloadsupport.SendEmailServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DeployApplicationServlet</servlet-name>
<servlet-class>downloadsupport.DeployApplicationServlet</servlet-class>
</servlet>
<welcome-file-list>
<welcome-file>DeployApplicationServlet</welcome-file>
<!-- <welcome-file>SendEmailServlet</welcome-file> -->
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>DownloadServet</servlet-name>
<servlet-class>downloadsupport.DownloadServlet</servlet-class>
</servlet>
<!--
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/downloadServlet</url-pattern>
</servlet-mapping>
-->
</web-app>
您的第一个不需要的电话是您自己造成的,而不是服务器不工作造成的。
您不需要实例化 ScheduleEmail
。从 servlet 中删除这两行,它将正常工作。
容器负责初始化你的bean并调用标有@Schedule
检查您的服务器实例。如果您在服务器中有 2 个节点。然后使它成为一个节点,而不是 ejb 计时器将按预期工作。