Run background process in different thread in Java

I am writing an application where user can add and remove other users as friends. My page has a list of diffrent users and each user is given with a button to add them in friend list. I am sending a AJAX request to Java servlet to add the selected user as a friend. I am showing alert message at UI to show the result of process. My problem is i have to sent a mail when user is added as friend this code is written in the same method in the servlet. Because of this piece of code my alert message comes very late. I need to run a separate pthread to run this send mail function so that once user is added i will get the result and mail will be send in separate process. My code in the Servlet is private void sendMail(long inviteeID) { User inviteeUser = null; try { inviteeUser = userHandler.getUser(inviteeID); } catch (DataException e) { sLog.error("User does not exist.", e); } catch (UserNotFoundException e) { sLog.error("User does not exist.", e); } MailUtility.send(inviteeUser.getUserEmailAddress().trim(), "vagarwal@q3tech.com", "add friend message", Utility .getAddFriendMessageBody(LoginHelper .getLoggedInUserEmail()), false); } private String inviteAsFriend(long inviteeID) { boolean result = false; if (LoginHelper.isUserLoggedIn()) { try { User user = userHandler.findUserByEmail(LoginHelper .getLoggedInUserEmail()); if (userHandler.isUserFriend(user.getUserId(), inviteeID)) { if (userHandler.addFriend(user, inviteeID)) { result = true; return "Member added successfully as your friend."; } else { return "Member could not be added as your friend. Please try again later."; } } else { return "Member is already your friend."; } } catch (DataException e) { return "User does not exist."; } catch (UserNotFoundException e) { return "User does not exist."; } catch (Exception e) { return "Member could not be added as your friend. Please try again later."; } finally { if (result) { sendMail(inviteeID); } } } else { return "User not logged in."; } }

以上就是Run background process in different thread in Java的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏