Java WildFly-使用长时间运行的应用程序创建应用程序

Java WildFly-使用长时间运行的应用程序创建应用程序,java,wildfly,Java,Wildfly,我是JavaEE新手,正在尝试创建一个WAR,其中包含一个每30分钟执行一次任务并在部署应用程序时启动的应用程序,以及一个允许客户端使用WildFly连接并获取状态信息的servlet 这可能吗?如果是这样,我如何让WildFly启动长时间运行的流程,以及如何让它将该对象注入servlet 长时间运行的应用程序位于ProcessData类中,该类使用ScheduledExecutorService在计划上生成一个线程以执行数据管理任务,并且有一个getStatus方法返回有关处理的字符串 这是s

我是JavaEE新手,正在尝试创建一个WAR,其中包含一个每30分钟执行一次任务并在部署应用程序时启动的应用程序,以及一个允许客户端使用WildFly连接并获取状态信息的servlet

这可能吗?如果是这样,我如何让WildFly启动长时间运行的流程,以及如何让它将该对象注入servlet

长时间运行的应用程序位于ProcessData类中,该类使用ScheduledExecutorService在计划上生成一个线程以执行数据管理任务,并且有一个getStatus方法返回有关处理的字符串

这是servlet:

@WebServlet("/procStat")
public class processorServlet extends HTTPServlet {
  @Inject
  ProcessData processData;

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    resp.setContentType("text/html");
    PrintWriter writer = resp.getWriter();
    writer.println("<p>" + processData.getStatus() + "</p>");
    writer.close();
  }
}

你为什么要注射它?你能初始化它并用静态方法或其他方法使用它吗?以下是我的例子:

public class HelloWorld extends HttpServlet
{
    private static final long serialVersionUID = -1L;
    ProcessData processData;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
   {
        // Set response content type
        response.setContentType("text/html");

        // Actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println("<h1>" + ProcessData.getStatus() + "</h1>");
    }
}
web.xml添加servlet和ServletContextListener,如下所示:

<?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" id="WebApp_ID" version="3.1">
  <display-name>TestTask</display-name>

  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>servlet.HelloWorld</servlet-class>
  </servlet>

  <servlet-mapping>
     <servlet-name>HelloWorld</servlet-name>
     <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>

   <listener>
         <listener-class>schedule.ProcessData</listener-class>
   </listener>

    <welcome-file-list>
     <welcome-file>index.html</welcome-file>
   </welcome-file-list>
</web-app>

我之前问过你为什么需要注入它,因为如果你有一个任务没有连接到像你这样的特定用户,不管用户是否登录,它都会被执行,那么你最终会遇到许多用户需要访问单个数据的情况。您需要某种类型的单例,是的,您可以使用CDIBeans或其他注入技术来实现这一点,但您也可以让它在没有这种开销的情况下工作。在我的例子中,我使用静态方法和静态字段来实现它。

是的,这是绝对可能的。当您提到将对象注入servlet时,您可能正在考虑使用EJB。如果您确实想使用Jave EE的注入,我建议您看看CDI:下面有一篇文章可以帮助您:
<?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" id="WebApp_ID" version="3.1">
  <display-name>TestTask</display-name>

  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>servlet.HelloWorld</servlet-class>
  </servlet>

  <servlet-mapping>
     <servlet-name>HelloWorld</servlet-name>
     <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>

   <listener>
         <listener-class>schedule.ProcessData</listener-class>
   </listener>

    <welcome-file-list>
     <welcome-file>index.html</welcome-file>
   </welcome-file-list>
</web-app>