JavaServlets-Runnable(scheduled)函数在AWS中执行两次
我在JavaServletWebApp中设置了一个每天运行的计划任务,但问题是它每天运行两次。此问题仅在部署到AWS EC2时发生,当我通过Eclipse在计算机上本地运行相同的代码时,它只执行一次计划任务(如预期的) 还有一部分我不明白,自从我实现了下面的类之后,我必须在AWS上启动两次Tomcat,webapp才能在线并通过web访问。如果我只运行一次Tomcat,webapp将不会联机,并且在输入网站URL时,我的浏览器会出现“未找到网页”错误 我在这里搜索了有关堆栈溢出的类似问题,但没有找到有关导致此问题的原因的更多信息 为什么下面的代码在AWSEC2上运行两次,而在我的本地计算机上通过Eclipse只运行一次?如何在AWS上解决此问题 ScheduleManager类:JavaServlets-Runnable(scheduled)函数在AWS中执行两次,java,amazon-web-services,servlets,amazon-ec2,tomcat9,Java,Amazon Web Services,Servlets,Amazon Ec2,Tomcat9,我在JavaServletWebApp中设置了一个每天运行的计划任务,但问题是它每天运行两次。此问题仅在部署到AWS EC2时发生,当我通过Eclipse在计算机上本地运行相同的代码时,它只执行一次计划任务(如预期的) 还有一部分我不明白,自从我实现了下面的类之后,我必须在AWS上启动两次Tomcat,webapp才能在线并通过web访问。如果我只运行一次Tomcat,webapp将不会联机,并且在输入网站URL时,我的浏览器会出现“未找到网页”错误 我在这里搜索了有关堆栈溢出的类似问题,但没有
package com.schedule;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.schedule.task.NonSubmittedTimesheetNotificationSchedule;
@WebListener
public class ScheduleManager implements ServletContextListener
{
private ScheduledExecutorService scheduler;
@Override
public void contextInitialized(ServletContextEvent event)
{
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new NonSubmittedTimesheetNotificationSchedule(event.getServletContext()), 0, 7*24*60*60, TimeUnit.SECONDS);
}
@Override
public void contextDestroyed(ServletContextEvent event)
{
scheduler.shutdownNow();
}
}
package com.schedule.task;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import com.process.EmailModule;
public class NonSubmittedTimesheetNotificationSchedule implements Runnable
{
private ServletContext context;
public NonSubmittedTimesheetNotificationSchedule(ServletContext context)
{
this.context = context;
}
@Override
public void run()
{
EmailModule.sendEmailToManagers();
EmailModule.sendEmailToUsers();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>MyApp</display-name>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<listener>
<listener-class>com.session.ActiveSessionListener</listener-class>
</listener>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>com.filter.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CredentialsFilter</filter-name>
<filter-class>com.filter.CredentialsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CredentialsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
<context-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>admin</param-value>
</context-param>
</web-app>
未提交的时间表通知类:
package com.schedule;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.schedule.task.NonSubmittedTimesheetNotificationSchedule;
@WebListener
public class ScheduleManager implements ServletContextListener
{
private ScheduledExecutorService scheduler;
@Override
public void contextInitialized(ServletContextEvent event)
{
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new NonSubmittedTimesheetNotificationSchedule(event.getServletContext()), 0, 7*24*60*60, TimeUnit.SECONDS);
}
@Override
public void contextDestroyed(ServletContextEvent event)
{
scheduler.shutdownNow();
}
}
package com.schedule.task;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import com.process.EmailModule;
public class NonSubmittedTimesheetNotificationSchedule implements Runnable
{
private ServletContext context;
public NonSubmittedTimesheetNotificationSchedule(ServletContext context)
{
this.context = context;
}
@Override
public void run()
{
EmailModule.sendEmailToManagers();
EmailModule.sendEmailToUsers();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>MyApp</display-name>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<listener>
<listener-class>com.session.ActiveSessionListener</listener-class>
</listener>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>com.filter.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CredentialsFilter</filter-name>
<filter-class>com.filter.CredentialsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CredentialsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
<context-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>admin</param-value>
</context-param>
</web-app>
web.xml文件:
package com.schedule;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.schedule.task.NonSubmittedTimesheetNotificationSchedule;
@WebListener
public class ScheduleManager implements ServletContextListener
{
private ScheduledExecutorService scheduler;
@Override
public void contextInitialized(ServletContextEvent event)
{
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new NonSubmittedTimesheetNotificationSchedule(event.getServletContext()), 0, 7*24*60*60, TimeUnit.SECONDS);
}
@Override
public void contextDestroyed(ServletContextEvent event)
{
scheduler.shutdownNow();
}
}
package com.schedule.task;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import com.process.EmailModule;
public class NonSubmittedTimesheetNotificationSchedule implements Runnable
{
private ServletContext context;
public NonSubmittedTimesheetNotificationSchedule(ServletContext context)
{
this.context = context;
}
@Override
public void run()
{
EmailModule.sendEmailToManagers();
EmailModule.sendEmailToUsers();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>MyApp</display-name>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<listener>
<listener-class>com.session.ActiveSessionListener</listener-class>
</listener>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>com.filter.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CredentialsFilter</filter-name>
<filter-class>com.filter.CredentialsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CredentialsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
<context-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>admin</param-value>
</context-param>
</web-app>
MyApp
login.html
120
com.session.ActiveSessionListener
身份验证过滤器
com.filter.AuthenticationFilter
身份验证过滤器
/*
证书过滤器
com.filter.CredentialsFilter
证书过滤器
/*
404
/error.html
用户名
管理
密码
管理
我认为您不应该将@WebListener
与web.xml
混合使用我认为您不应该将@WebListener
与web.xml