I'll rephrase my question here.
Is it possible to just kick out the old bean from the Camel Context (and in
turn from the Spring context) and inject a new bean while using the same to
build a route dynamically as shown below? I don't see any explicit '
*unbind()*' method with the *ApplicationContextBeanRepository*.
*Code snippet:*
public void updateAuthenticator() {
String tenantId = emailConfigBean.getTenantId();
String clientId = emailConfigBean.getClientId();
String clientSecret = emailConfigBean.getClientSecret();
String wsAdapterMailBox = emailConfigBean.getWSAdapterMailBox();
try {
MicrosoftExchangeOnlineOAuth2MailAuthenticator newExchangeAuthenticator =
new MicrosoftExchangeOnlineOAuth2MailAuthenticator(
tenantId, clientId, clientSecret, wsAdapterMailBox);
Registry registry = camelContext.getRegistry(Registry.class);
MicrosoftExchangeOnlineOAuth2MailAuthenticator existingAuthenticatorBean =
registry.lookupByNameAndType("exchangeAuthenticator",
MicrosoftExchangeOnlineOAuth2MailAuthenticator.class);
// Unbind the existing 'exchangeAuthenticator' bean
if (null != existingAuthenticatorBean) {
registry.unbind("exchangeAuthenticator");
}
// Bind the new 'exchangeAuthenticator' bean
registry.bind("exchangeAuthenticator", newExchangeAuthenticator);
} catch (Exception e) {
}
}
On Tue, Mar 4, 2025 at 10:40 AM Dipak Rai <[email protected]>
wrote:
> Hi,
>
> In the below snippet I am trying to dynamically build the IMAP route by
> refreshing my 'exchangeAuthenticator' bean. Camel seemingly supports only
> bean by name injection into the route. Although I am able to instantiate
> the bean with new values yet the route refers to the old bean values and
> fails with error.
>
> public void updateAuthenticator() {
> String tenantId = emailConfigBean.getTenantId();
> String clientId = emailConfigBean.getClientId();
> String clientSecret = emailConfigBean.getClientSecret();
> String wsAdapterMailBox = emailConfigBean.getWSAdapterMailBox();
> try {
> MicrosoftExchangeOnlineOAuth2MailAuthenticator
> newExchangeAuthenticator = new MicrosoftExchangeOnlineOAuth2MailAuthenticator(
> tenantId, clientId, clientSecret, wsAdapterMailBox);
> // Get the registry
> DefaultRegistry defaultRegistry = (DefaultRegistry)
> camelContext.getRegistry();
> // Unbind the existing 'exchangeAuthenticator' bean
> MicrosoftExchangeOnlineOAuth2MailAuthenticator exchangeAuthenticator =
> (MicrosoftExchangeOnlineOAuth2MailAuthenticator) defaultRegistry
> .lookupByName("exchangeAuthenticator");
> if (null != exchangeAuthenticator) {
> defaultRegistry.unbind("exchangeAuthenticator");
> }
> defaultRegistry.bind("exchangeAuthenticator",
> newExchangeAuthenticator);
> defaultRegistry.setCamelContext(camelContext);
> } catch (Exception e) {
> log.error("Exception in updating authenticator bean: {}",
> e.getMessage());
> }
> }
>
> //building the route dynamically through refreshed values of bean
> 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";
> }
>
> Regards,
>
> Dipak.
>
>
>