Hi,
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(),
"[email protected]", "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.";
}
}