Reputation: 3347
I've done a user registration email confirmation in my Spring app, based on this tutorial http://www.baeldung.com/registration-verify-user-by-email
Having a strange problem. for unknown reason, my custom event always fired 2 times.
Here is the code: event:
public class OnRegistrationCompleteEvent extends ApplicationEvent {
private final String appUrl;
private final Locale locale;
private final User user;
public OnRegistrationCompleteEvent(User user, Locale locale, String appUrl) {
super(user);
this.user = user;
this.locale = locale;
this.appUrl = appUrl;
}
public String getAppUrl() {
return appUrl;
}
public Locale getLocale() {
return locale;
}
public User getUser() {
return user;
}
}
listener:
@Component
public class RegistrationListener implements ApplicationListener<OnRegistrationCompleteEvent> {
private final static Logger logger = LogManager.getLogger(RegistrationListener.class.getName());
@Autowired
private JavaMailSender mailSender;
@Override
public void onApplicationEvent(OnRegistrationCompleteEvent event) {
this.confirmRegistration(event);
}
private void confirmRegistration(OnRegistrationCompleteEvent event) {
logger.info("OnRegistrationCompleteEvent confirmSocialPosting fired");
final User user = event.getUser();
String recipientAddress = user.getEmail();
String subject = "Registration Confirmation";
String from = "[email protected]";
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
String htmlMsg = "Your account was successfully registered! <br><br>" +
"username: "+user.getUsername()+"<br>"+
"password: "+user.getPassword()+"<br>";
mimeMessage.setContent(htmlMsg, "text/html");
helper.setTo(new InternetAddress(recipientAddress));
helper.setSubject(subject);
helper.setFrom(new InternetAddress(from));
mailSender.send(mimeMessage);
} catch (MessagingException e) {
logger.error("Message creation exception: "+e.getMessage());
}
}
}
controller:
@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public JsonResponse AddUser(@RequestBody @Valid User user, WebRequest request) throws SQLException {
String result = userService.RegisterUser(user);
if(result.equals("done")) {
try {
String appUrl = request.getContextPath();
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(user, request.getLocale(), appUrl));
} catch (Exception me) {
return new JsonResponse("FAIL", "Unknown on event publishing: "+ me.getMessage());
}
return new JsonResponse("OK", "");
} else if(result.equals("duplicate")) {
return new JsonResponse("FAIL", "duplicate");
}
return new JsonResponse("FAIL", "Unknown");
}
So, in my log, i have OnRegistrationCompleteEvent confirmSocialPosting fired
2 times. And 2 emails send. What is the problem can be?
Upvotes: 0
Views: 601
Reputation: 46
This is cause because it to register twice. You are using anotation in your application i.e base-package="com.example" thats why it registering twise
Remove anotation @component from your listener class and define bean defination in .xml file
this wokrs!!
Upvotes: 1