I am working on a Springboot + Camel app wherein I am dynamically building the IMAP connection over Java DSL as shown below. My problem is that although I am able to read all the IMAP parameters dynamically yet I am not able to register the 'exchangeAuthenticator' bean afresh with the updated values. Camel refers to the old wrong values previously mal-configured (deliberately as part of testing) and fails the IMAP route re-creation with the error in the image.
Workaround: I have to restart the app after correcting the OAuth2 credentials and then the bean 'exchangeAuthenticator' gets refreshed with the latest values.
But restarting the app is not an option for me. Any suggestions please on where I am going wrong. I am using Spring 3 and Camel 4.7.0.
What I have already tried? I tried injecting the 'ExchangeAuthenticator' as a "prototype" bean that didn't help. I tried unbinding the bean from the Camel registry but that doesn't help either.
Exception:
Java snippet of the recreation of IMAP route:
- AuthenticatorService.java (responsible for re-registering the bean)
@Service
public class AuthenticatorService {
@Autowired
private CamelContext camelContext;
@Autowired
private EmailConfigBean emailConfigBean;
public void updateAuthenticator() {
String tenantId = emailConfigBean.getTenantId();
String clientId = emailConfigBean.getClientId();
String clientSecret = emailConfigBean.getClientSecret();
String myMailBox = emailConfigBean.getmyMailBox();
try {
// log here confirms the latest values are correctly received
MicrosoftExchangeOnlineOAuth2MailAuthenticator authenticator = new MicrosoftExchangeOnlineOAuth2MailAuthenticator(
tenantId, clientId, clientSecret, myMailBox);
Registry registry = camelContext.getRegistry();
registry.unbind("exchangeAuthenticator");
registry.bind("exchangeAuthenticator", authenticator);
} catch (Exception e) {
//do something
}
}
@Bean
public CamelContextConfiguration contextConfiguration() {
return new CamelContextConfiguration() {
@Override
public void beforeApplicationStart(CamelContext context) {
updateAuthenticator();
}
@Override
public void afterApplicationStart(CamelContext context) {
// No action needed after context start
}
};
}
}
- RouteUtil.java (deals with routing logic)
private void reCreateImapRoute(String imapMailId, String imapPort, String pollingInterval, String debugMode) {
try {
Route route = camelContext.getRoute(MyConstants.IMAP_ROUTE_ID);
if (route != null) {
camelContext.getRouteController().stopRoute(MyConstants.IMAP_ROUTE_ID);
camelContext.removeRoute(MyConstants.IMAP_ROUTE_ID);
}
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from(getImapConfig(imapMailId, imapPort, pollingInterval, debugMode))
.routeId(MyConstants.IMAP_ROUTE_ID)
.autoStartup(true)
.to("direct:gotosecondstep");
}
});
} catch (MsalServiceException e) {
//do something
} catch (Exception e) {
//do something
}
}
public String getImapConfig(String imapUrl, String imapPort, String pollingInterval, String debugMode) {
authenticatorService.updateAuthenticator();
return "imaps://" + imapUrl + ":" + imapPort
+ "?authenticator=#exchangeAuthenticator"
+ "&debugMode="+ debugMode
+ "&delete=false"
+ "&unseen=true"
+ "&delay=" + pollingInterval
+ "&mail.imaps.auth.mechanisms=XOAUTH2"
+ "&disconnect=true"
+ "&closeFolder=true";
}