使用Timer - Performance在Server上自动触发方法

时间:2018-02-28 07:07:49

标签: java server

我在weblogic服务器上部署了一个web服务。每当任何客户端实例访问该Web服务时,我必须每24小时存储客户端详细信息并将邮件发送到客户端,直到客户端采取所需的操作。

因此,为了每24小时发送一次邮件,我已经写了一个定时器代码,应该调用该方法每24小时发送一次邮件。

我的问题是:由于代码在服务器上,并且可能有100个客户端会点击我的web服务并启动自己的计时器实例。 拥有这些多个计时器实例(例如100个或更多)会影响服务器性能吗?

如果是,请提供一个替代代码,我可以用它来触发网络服务被击中后每24小时发送一次邮件。

更新 - 我的代码

public static final Map<String, Timer> userDeactivationPendingMap = new HashMap();

    @GET
    @Path("/check-deactivation-status/{username}")
    @Produces(MediaType.APPLICATION_JSON)
    public String checkDeactivationStatus(@PathParam("username") final String username) {

        String returnJsonString = "Email has been sent to IProc team to take further actions on it.";

        Timer timer = new Timer();
        TimerTask dailyTask = new TimerTask() {
            @Override
            public void run() {
                hitEmailSendingApi(username, "abc@test.com");
            }
        };

        // schedule the task to run starting now and then every day...
        timer.schedule(dailyTask, 0l, 1000 * 60 * 60 * 24);

        userDeactivationPendingMap.put(username, timer);

        return returnJsonString;
    }

    @Path("/initializeUserDeactivationRequest")    @GET

    @Produces(MediaType.TEXT_HTML)
    public String deactivateUser(@QueryParam("username") String username) {

        //code to deactivate user

        //Cancelling timer
        Timer timer = userDeactivationPendingMap.get(username);

        timer.cancel();

        return "<h4>User Deactivated Successfully!!<h4>";
    }

    private void hitEmailSendingApi(String username, String iprocTeamMailAddress) {

        String domain = RequestFilter.getRequest().getRequestURL().toString();
        String contextPath = RequestFilter.getRequest().getContextPath();
        String serverURL = domain.substring(0, domain.indexOf(contextPath));

        String servletPath = RequestFilter.getRequest().getServletPath();
        serverURL = serverURL + contextPath + servletPath;

        EmailSender.sendEmail(iprocTeamMailAddress, "Action Required: Clear User's Pending Requisitions for Account Deactivation", "Hi Team"
                + ",\n\n A new request for user deactivation has been raised by " + username + ".\n\n"
                + "Once all the requisitions are cleared, please press on below link for deactivating the user.\n"
                + serverURL + "/deactivate-account/initializeUserDeactivationRequest?username=" + (username));
    }

0 个答案:

没有答案