繁体   English   中英

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

[英]Java EJB @Schedule annotation method is being called twice

我正在从事一个 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 中删除这两行,它会正常工作。

Container 负责初始化你的 bean 并调用标有@Schedule方法

检查您的服务器实例。 如果服务器中有 2 个节点。 然后使它成为一个节点,而不是 ejb 计时器将按预期工作。

暂无
暂无

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

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