【问题标题】:How to execute a function at a specified time set dynamically by user in java如何在java中用户动态设置的指定时间执行函数
【发布时间】:2013-11-13 15:11:03
【问题描述】:

我在 java 中有一个函数,其中包含生成 pdf 文件并保存到系统本地磁盘的代码。现在根据我的要求,我必须制作一个 jsp 页面,其中包含一个表单,用户可以在其中动态设置日期以及他需要生成pdf的时间。现在应该根据给定的用户输入生成pdf,并且用户输入本质上是动态的,可以更改..

例如..

假设用户已将 pdf 设置为每月 15 日上午 10:00 创建。那么这一次它应该在 15 日上午 10:00 生成 pdf。

现在,如果他的需求发生变化,他可以将其设置为每月上午 10:00 的 10 次......等等......

我无法继续前进..

这是我在 POJO 文件中的 pdf 生成代码..

OutputStream file = new FileOutputStream(new File("D://timer.pdf"));
        Document document = new Document();
        PdfWriter.getInstance(document, file);

        //Inserting Table in PDF
        PdfPTable table = new PdfPTable(3);

        PdfPCell cell = new PdfPCell(new Paragraph("Java4s.com"));

        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPadding(10.0f);
        cell.setBackgroundColor(new BaseColor(140, 221, 8));

        table.addCell(cell);

        table.addCell("Name");
        table.addCell("Address");
        table.addCell("Country");
        table.addCell("Java4s");
        table.addCell("NC");
        table.addCell("United States");
        table.setSpacingBefore(30.0f);       // Space Before table starts, like margin-top in CSS
        table.setSpacingAfter(30.0f);        // Space After table starts, like margin-Bottom in CSS                                       

        //Inserting List in PDF
        List list = new List(true, 30);
        list.add(new ListItem("Java4s"));
        list.add(new ListItem("Php4s"));
        list.add(new ListItem("Some Thing..."));

        //Text formating in PDF
        Chunk chunk = new Chunk("Welecome To Java4s Programming Blog...");
        chunk.setUnderline(+1f, -2f);//1st co-ordinate is for line width,2nd is space between
        Chunk chunk1 = new Chunk("Php4s.com");
        chunk1.setUnderline(+4f, -8f);
        chunk1.setBackground(new BaseColor(17, 46, 193));

        //Now Insert Every Thing Into PDF Document
        document.open();//PDF document opened........                  
        document.add(Chunk.NEWLINE);   //Something like in HTML :-)
        document.add(new Paragraph("Dear Java4s.com"));
        document.add(new Paragraph("Document Generated On - " + new Date().toString()));
        document.add(table);
        document.add(chunk);
        document.add(chunk1);
        document.add(Chunk.NEWLINE);   //Something like in HTML :-)                             
        document.newPage();            //Opened new page
        document.add(list);            //In the new page we are going to add list
        document.close();

        file.close();

        System.out.println("Pdf created successfully..");

提前谢谢..

【问题讨论】:

    标签: java jsp servlets pdf timer


    【解决方案1】:

    使用java.util.Timer 类。 this site上有很多例子。

    正如 cmets 中有人提到的那样,使用 ScheduledExecutorService 会更好。 (javadoc 已经有关于该主题的教程。)

    【讨论】:

    • 我认为ScheduledExecutorService 在这里更适用,因为Future 允许您控制和监视任务。
    • @BoristheSpider 请提供链接或代码 sn-p 先生,我对此很陌生..
    【解决方案2】:

    您可以使用 java 的 Timer 和 Task 类

    【讨论】:

    • 我认为ScheduledExecutorService 在这里更适用,因为Future 允许您控制和监视任务。
    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 2013-11-05
    • 2010-09-05
    • 2011-04-16
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多