简体   繁体   English

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

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

I am working on a J2EE project that, in short, sends an automated email to a user at a designated time, and allows the user to download files from the webpage that is emailed to them.我正在从事一个 J2EE 项目,简而言之,它在指定时间向用户发送自动电子邮件,并允许用户从通过电子邮件发送给他们的网页下载文件。 It works pretty well.它工作得很好。

However, my timer method that uses the @Schedule annotation is invoked twice.但是,我使用@Schedule注释的计时器方法被调用了两次。 The method always executes immediately at run time (which I don't want), then later at the designated time.该方法总是在运行时立即执行(我不想要),然后在指定的时间执行。 I have included code for the Servlet that is loaded when my application is deployed, the Schedule class, and my web.xml file.我已经包含了在部署我的应用程序时加载的 Servlet、Schedule 类和我的 web.xml 文件的代码。

DeployApplicationServlet class: 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 class: 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 config: 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>

Your first, unwanted call is caused by you, not by not working server.您的第一个不受欢迎的呼叫是由您引起的,而不是由服务器不工作引起的。

You dont need to instantiate ScheduleEmail .您不需要实例化ScheduleEmail Remove these two lines form the servlet and it will work ok.从 servlet 中删除这两行,它会正常工作。

Container is responsible for initializing your bean and calling methods marked with @Schedule Container 负责初始化你的 bean 并调用标有@Schedule方法

Check your server instance.检查您的服务器实例。 in case you have 2 nodes in server.如果服务器中有 2 个节点。 then make it one node than ejb timer will work as expected.然后使它成为一个节点,而不是 ejb 计时器将按预期工作。

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

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