Java EJB @Schedule 注释方法被调用两次

标签 java jakarta-ee ejb schedule

我正在开发一个 J2EE 项目,简而言之,该项目会在指定时间向用户发送自动电子邮件,并允许用户从通过电子邮件发送给他们的网页下载文件。效果非常好。

但是,我使用 @Schedule 注释的计时器方法被调用两次。该方法总是在运行时立即执行(我不希望如此),然后在指定时间执行。我已经包含了部署应用程序时加载的 Servlet 代码、Schedule 类和 web.xml 文件。

DeployApplicationServlet 类:

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 类:

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的方法

关于Java EJB @Schedule 注释方法被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30627045/

相关文章:

java - 在 Windows 8 中 System.in.available() 始终返回零

java - 存储敏感信息

java - 将值传递给注入(inject)的 EJB

java - 如何识别请求中 EJB 的客户端或调用者?

java - EJB:没有接口(interface)的依赖注入(inject)

java - 并发访问 @Lock(LockType.WRITE) 方法

java - 使用 JDBC 3.0 实现对嵌套事务的支持

java - 一对多 SQL 关系

java - 使用Java Reflection初始化成员变量

web-services - JAX-WS Web 服务,使用 HTTPS 端点 URL 而不是 HTTP 的负载平衡器